Below is an example structure that represents a linked list node:
struct Node { // Example data field int data; // Example of the next pointer Node* next; }
2. Creating a Node of Linked List
We can create the structure variables i.e. nodes using dynamic memory allocation
and structure pointer. Although we can also do it statically, dynamic allocation is
preferred to make full use of the abilities of the linked list.
struct Node* node2 = (struct Node*)malloc(sizeof(struct Node));Shortening the Node Declaration
If you feel that using the struct Node* every time makes the statement
unnecessarily complex, you can use the typedef to define an alias for it.
typedef struct node { int data; struct node* next; }Node;
Then we can create the Node as:
Node* node2 = (Node*) malloc(sizeof(Node));Code:
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node *next; }Node; int main() { Node *first = (Node *)malloc(sizeof(Node)); first->data = 10; Node *second = (Node *)malloc(sizeof(Node)); second->data = 20; Node *third = (Node *)malloc(sizeof(Node)); third->data = 30; first->next = second; second->next = third; third->next = NULL; printf("Linked List: "); Node* temp = first; while(temp) { printf("%d ", temp->data); temp = temp->next; } return 0; }Creation of New Node using Function
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node *next; } Node; Node *createNode(int data) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; return newNode; } int main() { Node *first = createNode(10); first->next = createNode(20); first->next->next = createNode(30); printf("Linked List: "); Node *temp = first; while (temp) { printf("%d ", temp->data); temp = temp->next; } return 0; }In JAVA// Linked list implementation in Java class LinkedList { // Creating a node Node head; static class Node { int value; Node next; Node(int d) { value = d; next = null; } } public static void main(String[] args) { LinkedList linkedList = new LinkedList(); // Assign value values linkedList.head = new Node(1); Node second = new Node(2); Node third = new Node(3); // Connect nodess linkedList.head.next = second; second.next = third; // printing node-value while (linkedList.head != null) { System.out.print(linkedList.head.value + " "); linkedList.head = linkedList.head.next; } } }Insertion
#include <stdio.h> #include <stdlib.h> // Define the node structure typedef struct Node { int data; struct Node* next; } Node; Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); if (!newNode) { printf("Memory error\n"); return NULL; } newNode->data = data; newNode->next = NULL; return newNode; } void insertAtBeginning(Node** head, int data) { Node* newNode = createNode(data); newNode->next = *head; *head = newNode; } void insertAtEnd(Node** head, int data) { Node* newNode = createNode(data); if (*head == NULL) { *head = newNode; return; } Node* temp = *head; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; } void insertInMiddle(Node** head, int data, int position) { Node* newNode = createNode(data); if (position == 1) { newNode->next = *head; *head = newNode; return; } Node* temp = *head; for (int i = 1; i < position - 1 && temp != NULL; i++) { temp = temp->next; } if (temp == NULL) { printf("Position is out of bounds\n"); return; } newNode->next = temp->next; temp->next = newNode; } void printList(Node* head) { Node* temp = head; while (temp != NULL) { printf("%d -> ", temp->data); temp = temp->next; } printf("NULL\n"); } int main() { Node* head = NULL; insertAtEnd(&head, 10); insertAtEnd(&head, 20); insertAtEnd(&head, 30); printf("Initial List: "); printList(head); insertAtBeginning(&head, 5); printf("After inserting 5 at beginning: "); printList(head); insertInMiddle(&head, 25, 3); printf("After inserting 25 at position 3: "); printList(head); insertAtEnd(&head, 40); printf("After inserting 40 at end: "); printList(head); return 0; }
Comments
Post a Comment