Presentation is loading. Please wait.

Presentation is loading. Please wait.

Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.

Similar presentations


Presentation on theme: "Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked."— Presentation transcript:

1

2 Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked list and may be declared as shown. struct dblNode { int info; dblNode* prev; dblNode * next; }; dblNode* start; In this example, note that prev of the first node is set to NULL.

3 1.Consider doubly linked lists. a. What are the advantages that this structure offers? b. What are the disadvantages? c. Compare doubly linked lists with singly linked lists. When would you choose to use one rather then the other? 2.Write a search function for a doubly linked list. 3. Write a fragment of code to delete the node containing 190 from the doubly linked list shown above. How would the code change if you were asked to delete the node containing 90 or 290 instead?

4

5 Circularly Linked Lists

6

7 Exercises Circular Linked Lists Directions: Use the type declarations at the right as you write each of the following functions. Each function should contain pre- and post- conditions and should work for empty lists, lists containing only one node, and lists containing many nodes struct node { int info; node* next; node(int D, node *N) : info(D),next(N) { } }; node* list; 1.Given a singly-linked linear list, write a function to convert it to a circular list. 2.Write a function to print out the information stored in a circular list. 3.Write a function to count the nodes in a circular list. 4.Given two circular lists A and B, write an O(1) function to join them into one circular list A, leaving the other list B empty.

8 Linked List Variants

9 Data structure struct dnode{ int Data; struct dnode *right,*left;}; typedef struct dnode *NodePointer; void main() { NodePointer Head= NULL; } void InsertElement (NodePointer &Head,int number) { NodePointer NewNode; NewNode=(NodePointer)malloc(sizeof(struct dnode)); NewNode->Data=number; NodePointer here=Head; If (Head != NULL){go through right link of ‘here’ and add NewNode at the end. Connect the last node(‘here’) to the left link of NewNode. Right Link of NewNode will be NULL } If (Head==NULL){ Make NewNode as Head. Both ‘right’ and ‘left’ link of Head will be NULL } }

10 Try SORT a double linked list: Each node contains 1)Data 2) total no. of occurrence 3) Right link & 4)left link.  Insert each new element to the left of ‘Head’ if its data is less than Head and to right if its data is higher than ‘head’. If equal, increase ‘total no. of occurrence's count.

11 Linked List Variants

12 Sparse Matrix

13

14


Download ppt "Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked."

Similar presentations


Ads by Google