1 DATA STRUCTURES
2 LINKED LIST
3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be done at specified location Many complex applications can be carried out using Linked List CONS Occupy more memory Random access of data is some what cumbersome & Time consuming WHY & WHY NOT ?
4 A linked list is a linear collection of specially designed data elements, called NODE, linked to one another by means of Pointer. Each NODE is divided In two parts, first part contains the information and second part contains the address of the next node. Need a head to point to the first node of the list. Otherwise we wont know where the start of the list is. The next field in the last node points to nothing. We will place the memory address NULL Struct Node { }; TERMINOLOGY int DATA Struct node *Next DATANext
head head 1065 ACTUAL PICTURE IN MEMORY
6 add(9): Create a new node in memory to hold 9 Node* newNode = new Node(9); Link the new node into the list 9newNod e head current size=5 6 9 newNod e OPERATION
head SINGLY LINKED LIST 2681 head DOUBLY LINKED LIST head current CIRCULAR LINKED LIST TYPES
8 SINGLY LINKED LIST
9 headNodesize=0 List list; 2 headNode currentNode size=1 lastcurrentNode list.add(2); 26 headNode currentNode size=2 lastcurrentNode list.add(6); list.add(8); list.add(7); list.add(1); 2671 headNode currentNode size=5 lastcurrentNode 8
10 30 START Create Node With DATA (30) 30 START Create Node With DATA (40) at END START Create Node With DATA (10) at BEGINING START Create Node With DATA (20) at SECOND Position START Create Node With DATA (20) at LAST Position
11 Insert node at BEGINING 1.Input DATA to be inserted 2.Create a NewNode 3.NewNode -> DATA = DATA 4.If (START == NULL) NewNode -> Next = NULL else NewNode -> Next = START 5.START = NewNode 6.Exit ALGORITHM
12 1.Input DATA to be inserted 2.Create a NewNode 3.NewNode - > DATA = DATA 4.NewNode -> Next = NULL 4.If (START == NULL) START = NewNode else TEMP = START while (TEMP -> NEXT != NULL) TEMP = TEMP -> NEXT 5.TEMP -> NEXT = NewNode 6.Exit ALGORITHM Insert node at END
13 1.Input DATA & POS to be inserted 2.Initialize TEMP = START & K =0 3.Repeat step 3 while K < POS (a) TEMP = TEMP -> NEXT (b) if (TEMP == NULL) Node Is Not In The List Exit (c) K = K Create a New Node 5.NewNode -> DATA = DATA 6.NewNode -> Next = TEMP -> NEXT 7.TEMP -> Next = NewNode 8.EXIT. ALGORITHM Insert node at SPECIFIED LOCATION
14 1.Input DATA to be deleted 2.If (START-> DATA = = DATA) (a) TEMP = START (b) START = START -> Next (c) free (TEMP) (d) Exit 3. HOLD = START 4. While (HOLD -> Next -> Next != NULL) (a) if (HOLD -> Next ->DATA == DATA) (i) TEMP = HOLD -> Next (ii) HOLD -> Next = TEMP -> Next (iii) free (TEMP) (iv) Exit. (b) HOLD = HOLD -> Next 5. if (HOLD -> Next -> DATA == DATA ) (a) TEMP = HOLD -> Next (b) free (TEMP) (c) HOLD -> Next = NULL (d) Exit. 6. DATA not Found 7. Exit. 1 st Location Ith Location Last Location ALGORITHM DELETE NODE
15 1.Input DATA to be searched 2.Initialize TEMP = START & POS = 1 3.Repeat Steps 4, 5 & 6 until TEMP = NULL 4.If (TEMP -> DATA == DATA) (a) DATA found at POS (b) Exit. 5. TEMP = TEMP -> Next 6. POS = POS If (TEMP = NULL) DATA Not Found in LIST 8. Exit. ALGORITHM SEARCHING NODE
16 1.If (START == NULL) (a) LIST is Empty (b) Exit. 2.Initialize TEMP =START 3.Repeat Step 4 and 5 Until TEMP = NULL 4.Display TEMP -> DATA 5.TEMP = TEMP -> Next 6.Exit. ALGORITHM DISPLAY NODES
17 10NULL Push (10) TOP 20 Push (20) 10NULL TOP 30 Push (30) 20 TOP 10NULL 20 Pop() 10NULL TOP 40 Push (40) 20 TOP 10NULL STACK – Using LINKED LIST
18 PUSH 1.Input DATA to be pushed 2.Create a New Node 3.NewNode -> DATA = DATA 4.NewNode -> Next = TOP 5.TOP = NewNode 6.Exit. ALGORITHM
19 ALGORITHM POP 1.If ( TOP == NULL ) STACK Is Empty 2.Else (a). TEMP = TOP (b). Poped Data : TOP -> DATA (c). TOP = TOP -> Next (d). TEMP -> Next = NULL (e). Free (TEMP) 3.Exit.
20 10NULL Push (10) Front 10 Push (20) 20NULL 10 Push (30) 2030NULL 20 Pop() 30NULL 20 Push (40) 3040NULL Rear Front Rear Front Rear NULL QUEUE – Using LINKED LIST
21 PUSH 1.Input DATA to be pushed 2.Create a New Node 3.NewNode -> DATA = DATA 4.NewNode -> Next = NULL 5.If ( REAR != NULL ) REAR -> Next = NewNode 5.REAR = NewNode 6.Exit. ALGORITHM
22 ALGORITHM POP 1.If ( FRONT == NULL || FRONT > REAR ) QUEUE Is Empty 2.Else (a). Popped Data : FRONT -> DATA (b). If (FRONT != REAR ) FRONT = FRONT -> Next Else FRONT = NULL 3.Exit.
23 DOUBLY LINKED LIST
24 Moving forward in a singly-linked list is easy; moving backwards is not so easy. To move back one node, we have to start at the head of the singly- linked list and move forward until the node before the current. To avoid this we can use two pointers in a node: one to point to next node and another to point to the previous node Struct node { } TERMINOLOGY int DATA; Struct node *next; Struct node *prev; DATAnext prev
25 Need to be more careful when adding or removing a node. Need to be more careful when adding or removing a node. size= head current Insert Node
26 1.NewNode->Next = current->Next size= head current 1 9 newNode 1 Insert Node
27 size= head current 1 9 newNode NewNode->prev = current Insert Node
28 size= head current 1 9 newNode current->next->prev = NewNode Insert Node
29 size= head current 1 9 newNode current->next = NewNode Insert Node
30 Size= head current 1 9 newNode current = NewNode Insert Node
31 1.Input DATA & POS 2.Initialize TEMP = START & i=1 3.while ( i < POS ) & (TEMP != NULL) TEMP = TEMP -> Next 4.If (TEMP != NULL) & I = POS a). Create a NewNode b). NewNode -> DATA = DATA c). NewNode -> next = TEMP -> next d). NewNode -> prev = TEMP e). TEMP -> next - > prev = NewNode f). TEMP - > next = NewNode 5.Else Position Not Found 6.Exit INSERT NODE
32 1.Input DATA to be deleted 2.Initialize TEMP = START 3.while (TEMP -> next -> DATA != DATA) TEMP = TEMP -> next 5.HEAD = TEMP TEMP = TEMP -> next 6.HEAD -> next = TEMP -> next 7.TEMP -> next -> prev = HEAD 8.Exit. DELETE NODE
33 CIRCULAR LINKED LIST
34 The next field in the last node in a singly-linked list is set to NULL. Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node. A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list. TERMINOLOGY
head current size= head current size=5 6 View of CIRCULAR QUEUE
start size=5 Traversing a CIRCULAR QUEUE Display(Start) 1.If Start = NULL Print List is empty!! End If 2.Set TEMP = Start 3.Do Print TEMP -> DATA Set TEMP = TEMP -> next While TEMP != Start 4. Exit.
start Insertion in the Begining Insert_beg(Start) 1.If Start = NULL Set Start=nptr Set Start -> next = Start Else Set TEMP = Start While TEMP ->next !=Start Set TEMP = TEMP -> next End While Set nptr -> next = Start Set Start = nptr Set TEMP -> next = Start End If 2.Exit. nptr 4 4 start (a) (b)
start Insertion at the End Insert_beg(Start) 1.If Start = NULL Set Start=nptr Set Start -> next = Start Else Set TEMP = Start While TEMP ->next !=Start Set TEMP = TEMP -> next End While Set TEMP -> next = nptr Set nptr -> next = Start End If 2.Exit. nptr (b) 4 temp
start Deletion from Begining delete_beg(Start) 1. If Start = NULL Print Underflow: List is empty! End If 2.Set TEMP = Start 3.Set ptr = TEMP 4.While ptr ->next !=Start Set ptr = ptr -> next End While 5.Set Start = Start -> next 6.Set ptr -> next = Start 7.Deallocate TEMP 8.Exit. (b) ptrtemp
start Deletion from the End delete_end(Start) 1. If Start = NULL Print Underflow: List is empty! End If 2.Set TEMP = Start 3.If temp -> next = start Set Start = NULL Else While TEMP ->next !=Start Set save = TEMP Set TEMP = TEMP -> next End While Set save -> next = Start End If 4.Deallocate TEMP 5.Exit. (b) tempsave