Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 DATA STRUCTURES

2 2 LINKED LIST

3 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 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

5 5 1051 1052 1055 1059 1060 1061 1062 1063 1064 1056 1057 1058 1053 10542 6 8 7 1 1051 1063 1057 1060 0 head 1054 1063 26871 head 1065 ACTUAL PICTURE IN MEMORY

6 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 26871 head current size=5 6 9 newNod e 1 3 2 OPERATION

7 7 26871 head SINGLY LINKED LIST 2681 head DOUBLY LINKED LIST 26871 head current CIRCULAR LINKED LIST TYPES

8 8 SINGLY LINKED LIST

9 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 10 30 START Create Node With DATA (30) 30 START Create Node With DATA (40) at END 40 10 START Create Node With DATA (10) at BEGINING 30 40 10 START Create Node With DATA (20) at SECOND Position 20 4030 10 START Create Node With DATA (20) at LAST Position 20 503040

11 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 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 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 + 1 4.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 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 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 + 1 7.If (TEMP = NULL) DATA Not Found in LIST 8. Exit. ALGORITHM SEARCHING NODE

16 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 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 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 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 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 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 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 23 DOUBLY LINKED LIST

24 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 25 Need to be more careful when adding or removing a node. Need to be more careful when adding or removing a node. size=5 26871 head current Insert Node

26 26 1.NewNode->Next = current->Next size=5 2687 head current 1 9 newNode 1 Insert Node

27 27 size=5 2687 head current 1 9 newNode 1 2 2.NewNode->prev = current Insert Node

28 28 size=5 2687 head current 1 9 newNode 1 23 3.current->next->prev = NewNode Insert Node

29 29 size=5 2687 head current 1 9 newNode 1 23 4 4.current->next = NewNode Insert Node

30 30 Size=6 2687 head current 1 9 newNode 1 23 4 5.current = NewNode Insert Node

31 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 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 33 CIRCULAR LINKED LIST

34 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

35 35 26871 head current size=5 2 8 7 1 head current size=5 6 View of CIRCULAR QUEUE

36 36 2 6871 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.

37 37 2 6871 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)

38 38 2 6871 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

39 39 2 6871 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

40 40 2 6871 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


Download ppt "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."

Similar presentations


Ads by Google