Singly Linked List Data Structures PDF

Summary

This document provides examples and code snippets on various operations of singly linked lists in C, including insertion at the beginning, insertion at the end, insertion after a given location, deletion from the beginning, and searching for a node in the list. It also discusses traversal and operations on a doubly linked list.

Full Transcript

# Insertion in Singly Linked List ## Insert at Beginning * **Code:** ```c struct node { int data; struct node *next; }; struct node *head, *newnode; newnode = (struct node *)malloc(sizeof(struct node)); printf("Enter data u want to insert: "); scanf("%d", &new_node->data); ne...

# Insertion in Singly Linked List ## Insert at Beginning * **Code:** ```c struct node { int data; struct node *next; }; struct node *head, *newnode; newnode = (struct node *)malloc(sizeof(struct node)); printf("Enter data u want to insert: "); scanf("%d", &new_node->data); newnode->next = head; head = newnode; ``` * **Example:** - Before: `head 500 -> 7/200 -> 1/300 -> 9/0` - After: `head 400 -> 7/200 -> 1/300 -> 9/0 ` - Newnode `2/100 -> 500` ## Insert at the End * **Code:** ```c struct node { int data; struct node *next; }; struct node *head, *newnode, *temp; newnode = (struct node *)malloc(sizeof(struct node)); printf("Enter data u want to insert: "); scanf("%d", &newnode->data); newnode->next = 0; temp = head; while(temp->next != 0) { temp = temp->next; } temp->next = newnode; ``` * **Example:** - Before: `head 100 -> 7/200 -> 1/300 -> 9/300` - After: `head 100 -> 7/200 -> 1/300 -> 9/300 -> 5/400` - Newnode `5/400 -> 0'` - The code `newnode->next = 0` ensures that the new node now points to null - We must traverse the entire list so we use `temp`. ## Insertion after a given Location * **Code:** ```c int pos; struct node { int data; struct node *next; }; struct node *head, *newnode, *temp; newnode = (struct node *)malloc(sizeof(struct node)); printf("Enter the position: "); scanf("%d", &pos); if(pos > count) { printf("Invalid Position"); } else { temp = head; while(i < pos) { temp = temp->next; i++; } printf("Enter data"); scanf("%d", &newnode->data); newnode->next = temp->next; temp->next = newnode; } ``` * **Example:** - Before: `head 100 -> 7/200 -> 1/300 -> 9/300` - After: `head 100 -> 7/200 -> 1/300 -> 5/200 -> 9/300 ` - Newnode `5/200 -> 9/300` # Deletion from Beginning * **Code:** ```c head temp { free(temp); temp = head; head = head->next; free(temp); } ``` * **Example:** - Before: `head 100 -> 5/200 -> 3/300 -> 9/400 -> 1/100` - After: `head 5/200 -> 3/300 -> 9/400 -> 1/100` # Deletion from End * **Code:** ```c // initially prevnode and temp point to head while(temp->next !=0) { prevnode = temp; temp = temp->next; } prevnode -> next = 0; free(temp); ``` * **Example:** - Before: `head 200 -> 3/300 -> 9/400 -> 1/100` - After: `head 200 -> 3/300 -> 9/400` # Searching a Node in Singly Linked List * **Code:** ```c int flag = 0; printf("Enter key element "); scanf("%d", &key); temp = head; while(temp != NULL) { if(key == temp->data) { flag = 1; break; } temp = temp->next; } if (flag == 1) { printf("Key elements found"); } else { prinf("Key element nor found"); } ``` * **Example:** - Before: `head 100 -> 5/200 -> 6/300 -> 7/100` - Search for key = 6; - Result: Key element found # Traversing in a Linked List * **Code:** ```c struct node { int data; struct node *next; }; struct node *head, *temp; traverse() { if(head == NULL) { printf("L.L. is Empty"); } struct node *temp = NULL; temp = head; while(temp != NULL) { printf("%d", temp->data); temp = temp->next; } } ``` * **Example:** - Before: `head 100 -> 5/200 -> 6/300 -> 7/100` - Result: Print the following: 100, 200, 300, 100 # Doubly Linked List - **Features:** - **Forward and Backward:** Can traverse in both directions - **Temp Pointer:** Can look forward and backward - **Drawback:** Stores two pointers, which requires more memory space.

Use Quizgecko on...
Browser
Browser