Implementation of Linked Lists

Shresth Dubey Email: [email protected]

Website: accmp.co.in Email: [email protected] Objectives: • Creating a . • Traversing a linked list. • Inserting a node at a specific position in a linked list. • Inserting a node at beginning of a linked list. • Inserting a node at the end of a linked list. • Loop detection. • Interview Questions.

Website: accmp.co.in 2 Email: [email protected] Linked List:

struct Node { int data; Node* next; };

Website: accmp.co.in 3 Email: [email protected] Creating a Linear Linked List

#include #include struct Node { int data; struct Node* next; };

Website: accmp.co.in 4 Email: [email protected] int main() { struct Node* head = NULL; struct Node* lptr = NULL; struct Node* rptr = NULL;

lptr = (struct Node*)malloc(sizeof(struct Node));

printf("\nEnter first element"); scanf("%d", &lptr->data);

head = lptr; char ch;

Website: accmp.co.in 5 Email: [email protected] while(ch!='x'){ rptr = (struct Node*)malloc(sizeof(struct Node)); lptr->next = rptr; printf("\nEnter next element"); scanf("%d", &rptr->data); lptr = rptr; printf("press any key to continue. and press x to stop"); ch = getchar(); }

lptr->next = NULL;

return 0; }

Website: accmp.co.in 6 Email: [email protected] Traversing a Linked List struct Node* ptr; ptr = head; while (ptr!=NULL) { printf(“%d”, ptr->data;) ptr = ptr -> next; }

Website: accmp.co.in 7 Email: [email protected] Insert after a specific node in a linked list.

Step 1 : Create a new Node

Website: accmp.co.in 8 Email: [email protected] Step 2 : Traverse till the data node

Website: accmp.co.in 9 Email: [email protected] Step 3 : Connect the new node to the very next node of the data node.

Website: accmp.co.in 10 Email: [email protected] Step 4 : Connect the data node to the new node.

Website: accmp.co.in 11 Email: [email protected] CODE void insert(struct Node* head,int d, int newData){ struct Node* ptr = head; if (head == NULL){ printf("\nList empty"); return; }

Website: accmp.co.in 12 Email: [email protected] while(ptr->data!=d){ ptr = ptr->next; if (ptr == NULL){ printf("\ndata not found"); return; } }

Website: accmp.co.in 13 Email: [email protected] struct Node *newNode; newNode = (struct Node*)malloc(sizeof(struct Node)); newNode -> data = newData; newNode->next = ptr->next; ptr->next = newNode; }

Website: accmp.co.in 14 Email: [email protected] Insert at Beginning

Website: accmp.co.in 15 Email: [email protected] Steps

1.Make a new node 2.Point the ‘next’ of the new node to the ‘head’ of the linked list. 3.Mark new node as ‘head’.

Website: accmp.co.in 16 Email: [email protected] CODE void insertAtBeginning(struct node *head, int value) { struct node *p; p=malloc(sizeof(struct node)); p->data=value; p->next=head; }

Website: accmp.co.in 17 Email: [email protected] Insertion at End

Website: accmp.co.in 18 Email: [email protected] Steps

1.Make a new node. 2.Traverse till the last node of the linked list. 3.Point the last node of the linked list to the new node.

Website: accmp.co.in 19 Email: [email protected] CODE insertAtEnd(struct node *head, int value) { struct node *p,*q; p=malloc(sizeof(struct node)); p->data=value; p->next=NULL; q=head; while(q->next!=NULL) { q = q->next; } q->next = p; }

Website: accmp.co.in 20 Email: [email protected] Loop Detection in Linked List

Website: accmp.co.in 21 Email: [email protected] Floyd’s Cycle detection

Floyd's cycle-finding algorithm is a pointer algorithm that uses only two pointers, which move through the at different speeds. It is also called the "tortoise and the hare algorithm“.

Website: accmp.co.in 22 Email: [email protected] Floyd’s Cycle detection algorithm

• In this algorithm we will create two pointers • Fast pointer (hare pointer) • Slow pointer (tortoise pointer) • We will start from head and try to treverse the linked list with these two pointers. • If the two pointers meet at some point • It means the linked list has a loop

Website: accmp.co.in 23 Email: [email protected] CODE int detectLoop(struct Node* head) { struct Node *slow_p = head, *fast_p = head;

while (slow_p && fast_p && fast_p->next) { slow_p = slow_p->next; fast_p = fast_p->next->next;

Website: accmp.co.in 24 Email: [email protected] if (slow_p == fast_p) { /* Return 1 to indicate that loop is found */ return 1; } } /* Return 0 to indeciate that ther is no loop*/ return 0;

Website: accmp.co.in 25 Email: [email protected] QUESTIONS

• Mention what is the difference between Linear Array and Linked List?

Website: accmp.co.in 26 Email: [email protected] Linear Array Linked List

•Deletion and Insertions are difficult. •Deletion and Insertions can be done easily.

•For insertion and deletion, it needs •For insertion and deletion, it does not movements require movement of nodes •In it space is wasted •In it space is not wasted •It is expensive •It is not expensive •It cannot be reduced or extended •It can be reduced or extended according to according to requirements requirements •To avail each element same amount of •To avail each element different amount of time is required. time is required. •In consecutive memory locations •Elements may or may not be stored in elements are stored. consecutive memory locations

•To reach a particular node, you need to go •We can reach there directly if we have through all those nodes that come before to go to a particular element that node.

Website: accmp.co.in 27 Email: [email protected] Mention what is the biggest advantage of linked lists?

Website: accmp.co.in 28 Email: [email protected] The biggest benefit of linked lists is that you do not specify a fixed size for your list. The more elements you add to the chain, the bigger the chain gets.

Website: accmp.co.in 29 Email: [email protected] Thank You For Your Time!

Website: accmp.co.in 30 Email: [email protected]