Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI387 Data Structure- 2012 Fall Doubly Linked List Sep. 3, 2012 Sung Hee Park Computer Science Virginia State University.

Similar presentations


Presentation on theme: "CSCI387 Data Structure- 2012 Fall Doubly Linked List Sep. 3, 2012 Sung Hee Park Computer Science Virginia State University."— Presentation transcript:

1 CSCI387 Data Structure- 2012 Fall Doubly Linked List Sep. 3, 2012 Sung Hee Park Computer Science Virginia State University

2 Assignment 03- Linked List Programming http://people.cs.vt.edu/~shpark/VSU/CSCI387 -DataStructure/assignments/assignment03- LinkedList/assignment03-LinkedList.pdf http://people.cs.vt.edu/~shpark/VSU/CSCI387 -DataStructure/assignments/assignment03- LinkedList/assignment03-LinkedList.pdf Due: 9/6/2012 11:55pm

3 What is the output? a.cout item b.cout item c.cout next->item d.cout next->next->item Item next 23 44 23 32231655873544 list AB

4 What is the value of the following relational expressions? a.list->item >= 18 b.list->next->next== A c.A->next->item == 55 d.B->next == NULL e.list->item == 18 True False 2332231655873544 list AB

5 2332231655873544 list AB Mark each of the following statements as valid or invalid? If a statement is invalid, explain why. a.A = B; b.list->next = A->next; c.list->next->item = 45; d.*list =B; e.*A = *B; f.B = A ->next ->item; g.A ->item = B->item; h.list = B->next->next; i.B = B ->next->next -> next; a. Valid b. Valid c. Valid d. Invalid; B is a pointer whereas *List is a struct. e. Valid f. Invalid; B is a pointer whereas A->next->item is an int. g. Valid h. Valid i. Invalid, B->next->next is null, which does not have a ‘next’ pointer.

6 Write C++ statements to do the following: a.Make A point to the node containing item 23. b.Make list point to the node containing item 16. c.Make B point to the last node in the list. d.Make list point to any empty list. e.Set the value of the node containing 55 to 35. a.A = List; b.List = A->next; c.B = B->next; d.List = NULL; e.A->next->next->item = 35; 2332231655873544 list AB

7 Doubly Linked List Conceptual picture list head tail

8 Doubly Linked List - Node typedef struct dNode { int data;// will store information node *next; // the reference to the next node node *prev; // the reference to the previous node };

9 Insert a new node at position curr preNode = curr->prev; newNode->prev = prevNode; // statement (1) prevNode->next = newNode; // statement (2) curr->prev = newNode; // statement (3) New->next = curr;// statement (4) (1)(2) (3) (4) newNode prevNode = curr->prevcurr

10 Delete node curr preNode = curr->prev; succNode = curr->next; preNode->next = succNode;// statement (1) succNode->prev = prevNode;// statement (2) newNode prevNode = curr->prevcurrsuccNode = curr->next (1) (2)

11 Circular Doubly Linked Lists Conceptual picture list head

12 Circular Doubly Linked Lists Example list 2 head 349 last=head->prev;first=head->next;

13 Declaring a Doubly Linked List Empty Doubly Linked List next prev head null head->next = head; head->prev = head; Empty Singly Linked List head = null;

14 In Class Activity – Doubly Linked List 1) Implement addBefore(dNode curr, int item) in C++ dNode addBefore(dNode curr, int item) { // }

15 (1)(2) (3) (4) newNode prevNode = curr->prevcurr

16 In Class Activity – Doubly Linked List 2) Implement remove(dNode curr, int item) in C++ dNode addBefore(dNode curr) { // }

17 newNode prevNode = curr->prevcurrsuccNode = curr->next (1) (2)

18 In Class Activity – Circular Doubly Linked List Depict each statement in a circular doubly linked list with a starting pointer head. – addBefore(head->next, a); – remove(head->next) – addBefore(head, a); – remove(head->prev);


Download ppt "CSCI387 Data Structure- 2012 Fall Doubly Linked List Sep. 3, 2012 Sung Hee Park Computer Science Virginia State University."

Similar presentations


Ads by Google