- Home
- Documents
- Linked List 3 Stack 33 Queue 40 - By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list in

prev

next

out of 63

Published on

27-May-2018View

216Download

1

Embed Size (px)

Transcript

Devloped By Dhaval S Sheth(9033271276) 1

Linked List .................................................................... 3 Singly Linked List .............................................................. 3 Doubly Linked List ............................................................. 6 Explain Doubly Linked list: - ............................................... 6 Circular Singly Linked List ................................................. 10 Order Linked List: ............................................................ 15 Application of Linked list: ................................................. 16 Graphical Representation of Linked List .............................. 19 Graphical Representation of Doubly Linked List ................... 25 Graphical Representation of Circular Linked List .................. 28

Stack ......................................................................... 33 Application of the stack: ................................................... 34 Algorithm for push operation ............................................ 35 Operation: POP I from the stack. ....................................... 36 Operation: PEEP 2nd element from the stack. .................... 37

Queue ........................................................................ 40 What is queue? ............................................................... 40 Implementation of Queue: ................................................ 40 Algorithm for simple queue:- ............................................ 42 Insert function: .................................................................... 42 Delete Function .................................................................... 43 Print function ....................................................................... 44 Search Function .................................................................... 44 Update function .................................................................... 44

Circular Queue ................................................................ 45 Advantages of circular queue over simple queue ....................... 45 Disadvantages of Circular queue ............................................. 46

Algorithm for Circular Queue: - ......................................... 46 Insert function: .................................................................... 46 Delete Function: ................................................................... 47 Print Function: ...................................................................... 47

Application of Queue: ....................................................... 48 Tree .......................................................................... 49 Definition:- ..................................................................... 49 Explain Tree:- ................................................................. 49 Binary Tree:- .................................................................. 50 Representation OR Implementation of Binary Tree. .............. 51 Operation Of Binary Tree ....................................................... 53 Algorithm for preorder:- ........................................................ 54 Inorder:- ............................................................................. 54 Algorithm For Inorder Traversal of Binary Tree:- ....................... 56

Devloped By Dhaval S Sheth(9033271276) 2

Postorder Traversal:- ............................................................ 56 Algorithm for Postorder Traversal of Binary Tree. :- .................. 57

Application of Binary Tree:- .............................................. 57 Graphs ....................................................................... 59

Devloped By Dhaval S Sheth(9033271276) 3

Linked List

Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list in which each node contains only one link field pointing the next node in the list. Each node is divided in two parts.

1. Information part. 2. Contains address of next node.

For Example: - Head

NULL Head = Pointer Variable Points to first element (node) of in the list. NULL=It indicates the end of the list. 1. Algorithm for the Creation of the Simple Linked List Function CREATE(X, FIRST) [Given X, a new element, and FISRT, a pointer to the first element of a Linked linear list whose typical node contains INFO and LINK fields as in above fig, this function inserts X.]

Devloped By Dhaval S Sheth(9033271276) 4

1. [Repeat thru step 5] Repeat while Choice! = n 2. [Allocate the New node] NEW NODE 3. [Initialize the fields of new node] INFO (NEW) = X LINK (FIRST) = NEW 4. [Want to insert another node] Read (Choice) 5. [Set the LINK field of Last inserted element] LINK (FIRST) = NULL 6. [Finished] Return Algorithm for the Inserting the element in the Simple Linked List

Function INSERT (TEMPHEAD,KEY) [This Function Insert the element after the node, which have the information field equal to the X. And HEAD is the pointer variable, which points to the first element of the list] 1. [Allocate the Memory for the NEW node] NEW NODE 2. [Set fields of the NEW node] INFO (NEW) = X LINK (NEW) = NULL 3. [Insertion as the first node] LINK (NEW) = TEMPHEAD TEMPHEAD = NEW Return (HEAD) 4. [Save the address of the first element of the list] SAVE = TEMPHEAD 5. [Find the element after which we want to insert the element] Repeat while INFO (LINK (SAVE) ) != NULL 6. [Insert the element] LINK (NEW) = LINK (SAVE) LINK (SAVE) = NEW 7. [Return the address of the first element] Return (TEMPHEAD)

Devloped By Dhaval S Sheth(9033271276) 5

Algorithm for the delete the element from the list Function DEL (TEMPHEAD, KEY) [This Function Delete the Node whose information fields equals to the KEY. And TEMPHEAD is the pointer which points the first element of the list and function returns the address of the first node] 1. [Check for the empty list] If TEMPHEAD = NULL Then write (Empty List) 2. [Deletion of the first node] SAVE = TEMPHEAD TEMPHEAD = LINK (TEMPHEAD) Free (SAVE) Return (TEMPHEAD) 3. [Save the address of the first node of the list] SAVE = TEMPHEAD 4. [Find the Node which to be deleted] Repeat while INFO (LINK (SAVE))! = KEY 5. [Delete the node] TEMP = LINK (SAVE) LINK (SAVE) = LINK (LINK (SAVE)) Free (TEMP) 6. [Finished] Return (TEMPHEAD) Algorithm for the print the list

Procedure PRINT (HEAD) [This Procedure print the information field of the list and HEAD is the first element of the list] 1. [Repeat step thru] Repeat while LINK (HEAD) != NULL 2. [Print the Information] Write (INFO (HEAD))

Devloped By Dhaval S Sheth(9033271276) 6

3. [Finished] Return Doubly Linked List Explain Doubly Linked list Ans. The Linked list in which each node has two pointers, one to store address of forward link & second to store address of backward link, is called Doubly Linked list. The Backward link for point out left most node. & The forward link for point out right most node. Reason for use of doubly linked list. OR Disadvantages of Singly linked list. Ans. Suppose we have singly linked list in which we want to insert a node A pointed to by POINT 1 just before A node B pointed to by POINT 2. We can change the link field of A to point out B, but we dont know the address of the node preceding B. Therefore this required time consuming sequential searching in singly linked list & this is In-efficient. To avoid this problem we used doubly linked list. Representation:

Generally, doubly linked list is represented as shown below. Head NULL Backward Address Forward Address

Devloped By Dhaval S Sheth(9033271276) 7

NULL: - It indicates the end of the list in each direction. Each node having three fields.

1. Pointer to previous node. 2. Information field. 3. Pointer to next node.

Advantage: -

Inserting node in to or Deleting one node from the list is much easier

task because we dont have to search the list sequentially to locate the preceding node. Algorithm for the Creation of the Doubly linked list Procedure CRETE(TEMPHEAD) [This Procedure Create the Doubly linked list TEMPHEAD is the pointer variable which point to the first element of the list and LPTR and RPTR is the pointer field of the NODE which points the Previous and new Node of the list respectively.] 1. [Repeat thru step] Repeat while choice! = n 2. [Allocate the new Node] NEW NODE 3. [Set field of new Node] INFO (NEW) = X LPTR (NEW) = RPTR (RPTR) = NULL 4. [Insert the element] RPTR (TEMPHEAD) = NEW LPTR (RPTR (TEMPHEAD)) = TEMPHEAD TEMPHEAD = RPTR (TEMPHEAD) 5. [Read the Choice] Read (choice) 6. [Finished] Return

Devloped By Dhaval S Sheth(9033271276) 8

Insertion in the middle of a doubly linked list

A Left-most insertion in a doubly linked list

Devloped By Dhaval S Sheth(9033271276) 9

Algorithm for the insert an element in the doubly list

Function INSERT (TEMPHEAD, KEY) [This Function inserts an element after the node which the info filed equals to the KEY and the returns the address of the first node] 1. [Allocate the memory for the new node] NEW NODE INFO (NEW) = X 2. [Insertion as the first node] RPTR (NEW) = TEMPHEAD LPTR (NEW) = NULL RPTR (TEMPHEAD) = NEW TEMPHEAD = NEW Return (TEMPHEAD) 3. [Save address of the first node] SAVE = TEMPHEAD 4. [Find the element after which we want to insert the element] Repeat thru step while RPTR (SAVE)! = NULL 5. [Check for the desire position] If INFO (RPTR (SAVE)) = KEY Then RPTR (NEW) = RPTR (SAVE) LPTR (NEW) = SAVE LPTR (RPTR (SAVE)) = NEW RPTR (SAVE) = NEW 6. [Finished] Return (TEMPHEAD) Algorithm for the deleting an element from the doubly linked list Function DELETE (TEMPHEAD, KEY) [This Function delete an element from the doubly list and returns the address of the first element TEMPHEAD is pointer which points the first element of the list and KEY specify info of the node which is to be deleted] 1. [Check for the empty list] If TEMPHEAD = NULL Then write (Empty list) Return

Devloped By Dhaval S Sheth(9033271276) 10

2. [Deletion of the first node] TEMP = TEMPHEAD RPTR (TEMPHEAD) = TEMPHEAD PRV (TEMPHEAD) = NULL Free (TEMP) Return (TEMPHEAD) 3. [Save the address of the first node] SAVE = TEMPHEAD 4. [Search for the desire node] Repeat while thru step 5 RPTR (SAVE)! = NULL 5. [Check for the information field] If INFO (RPTR (SAVE)) = KEY Then TEMP = RPTR (SAVE) RPTR (SAVE) = RPTR (RPTR (SAVE)) LPTR (RPTR (SAVE)) = SAVE Free (TEMP) 6. [Finished] Return (TEMPHEAD) Algorithm for the print the doubly list Procedure PRINT (TEMPHEAD) [This procedure print the element of the node in the LIFO and FIFO format and TEMPHEAD points the first element of the list] 1. [Check for the empty list] If TEMPHEAD = NULL Then write (Empty list) Return 2. [First in first out] Repeat while RPTR (TEMPHEAD)! = NULL Write (INOF (TEMPHEAD)) 3. [Last in first out] Repeat while TEMPHEAD! = NULL Write (INFO (TEMPHEAD)) 4. [Finished] Return Circular Singly Linked List

Devloped By Dhaval S Sheth(9033271276) 11

Explain Singly Circular Linked List: Ans. A singly circular linked list is a linked list in which the last node of the list point to the first node in the list. In Circular linked list, we can start at any node in the list & travel the whole list. For this reason we can make our external pointer to the list pointer to any node & still access all the node in the list. Representation of Circular Linked list:

Head

Head Advantage of Circular List over Singly linked list.

Ans. 1. It is concerned with the accessibility of a node. 2. In Circular list every node is accessible from given node. i.e.: -

From this given node all nodes can be reached by many changing through the list.

3. It concerns the deletion operation. In singly linked list to delete desired node, it is necessary to give the address of first node of the list.

4. This necessity result from the fact that in order to delete desired node. The predecessor of this node has to be found.

5. To find the predecessor required that a search could be carried out by changing through node from the first node of the list such requirement doesnt exist for circular list.

Disadvantage:

Devloped By Dhaval S Sheth(9033271276) 12

Ans. It is possible that without some care in processing, it is possible to get in to an infinite loop. Solution of Disadvantage: Ans. In processing a circular list, it is important that we are able to delete the end of list. This deletion of end is achieved by placing special node, which can be easily identified in the circular list. This special node is often called the list head of the circular list. Representation of circular list with list head is given as in following figure. Head Algorithm for the Creation of the Circular list

Procedure CREATE (TEMPHEAD) [This procedure creates the circular list and TEMPHEAD is the pointer variable which points the first element of the list] 1. [Save the address of the first element] SAVE = TEMPHEAD 2. [Repeat thru step 5] Repeat while Choice! = n 3. [Allocate the New node] NEW NODE 4. [Initialize the fields of new node] INFO (NEW) = X LINK (SAVE) = NEW SAVE = NEW 5. [Want to insert another node] Read (Choice) 6. [Set the LINK field of Last inserted element] LINK (SAVE) = TEMPHEAD

Devloped By Dhaval S Sheth(9033271276) 13

7. [Finished] Return Algorithm for the insertion of the node in the circular list Function INSERT (TEMPHEAD, KEY) [This Function inserts an element after the node which have the info field equal to the KEY variable and TEMPHEAD is the pointer which points the first element of the list and SAVE is the temp variable for the store address of the first element] 1. [Allocate the Memory for the NEW node] NEW NODE 2. [Set fields of the NEW node] INFO (NEW) = X LINK (NEW) = NULL 3. [Save address of the first node] FIRST = TEMPHEAD 4. [Insertion as first node and find last element of the list] Repeat while LINK (TEMPHEAD)! = NULL 5. [Insert the node] LINK (TEMPHEAD) = NEW LINK (NEW) = FIRST FIRST = NEW Return (FIRST) 6. [Insert in the list other than the first node] Repeat while INFO (LINK (TEMPHEAD)) = KEY 7. [Set the link for the NEW node] LINK (NEW) = LINK (TEMPHEAD) LINK (TEMPHEAD) = NEW 8. [Finished] Return (FIRST) Algorithm for the Deletion an element from the circular list

Devloped By Dhaval S Sheth(9033271276) 14

Function DELETE (TEMPHEAD, KEY) [This Function deletes an element from the circular list] 1. [Check for the empty list] If TEMPHEAD = NULL Then write (Empty List) 2. [List contain Single node] if LINK (TEMPHEAD) = TEMPHEAD Return NULL Free (TEMPHEAD) 3. [Save the address of...