Doubly linked list concept Node structure Insertion sort Insertion sort program with a doubly linked...

33
Doubly Linked List Lesson xx

Transcript of Doubly linked list concept Node structure Insertion sort Insertion sort program with a doubly linked...

  • Slide 1

Slide 2 Slide 3 Doubly linked list concept Node structure Insertion sort Insertion sort program with a doubly linked list Slide 4 Head 0abcde0 Slide 5 7 struct node { node * prev; int val; node * next; }; Slide 6 1.Ask the use to enter an integer 2.Store the # in one node of a doubly linked list 3.Repeat steps 1 & 2 until the user enters s to terminate input 4.Use the insertion sort to sort the #s that are in the doubly linked list 5.Print out the sorted list Slide 7 Head 052738 0 087532 0 Fig. 1 Fig. 2 Slide 8 1) Place a pointer called out on the node that we want to insert. (When we 1 st start, out is placed on the next to the last node. 2) Place a pointer called in one node to the right of out. 3) Compare the # to be inserted with the contents of what is pointed to by in. 4) If the # to be sorted is less than what is pointed to by in, swap the contents and move in, one node to the right 5) When in is at the end of the list or the # to be sorted is > than what is pointed to in, we have inserted the # into the correct position and its time to move out, 1 node to the left and repeat steps 2-5. Slide 9 1.Well use the insertion sort to rearrange the following list of #s in descending order: 52 7 38 2.Place a pointer called out on the next to the last # 3.Place a pointer called in, one node to the right of out 52 7 38 out in 4.Consider the last # (8) to be the sorted list & 3 is the # we want to insert into the sorted list. 5.If the # pointed to by out (3) < the # pointed to by in, swap them. Now you get the following picture 52 783 out in Slide 10 6.After swapping the #s, move in one node to the right 52 783 out in 7. When in is off the list, this means that we have inserted the # 3 in the correct position 8.Move out, 1 node to the left and place in 1 node to the right of out 52 783 out in 9.All the nodes to the right of out are sorted in descending order. Now we are going to insert 7 into the list 10.Since 7 is < 8, we need to swap the numbers and also move in one node to the right. 52 873 out in Slide 11 11..Compare 7 and 3. 7 is > 3 so we have inserted 7 in to the correct position in the list. 52 873 out in 12. The #s from out and to the right are now sort in descending order. 8, 7,3. Next step is to move out 1 node to the left and place in 1 node to the right of out. 52 873 out in 13. We are going to insert 2 in to the sorted list. You can see that in keeps moving to the right until the # is in the correct position or in is off the list. Out always moves to the left and points to the # we want to insert into the list. This procedure is continues until out points to a null. Then, the list is in descending order. Slide 12 #include using std::cin; using std::cout; using std::flush; using std::endl; #include struct node { node* prev; int value; node* next; }; void printList(const node*); Slide 13>str; while(str[0]!='s') { tail->valu"> >str; while(str[0]!='s') { tail->value=atoi (str); tail >next=new node; tail >next >prev=tail; tail=tail >next; coutstr; } tail >next=0; printList(head);//print unsorted list"> >str; while(str[0]!='s') { tail->valu" title="int main() { char str[15]; node* head = new node; node* tail = head; head >prev = 0; coutstr; while(str[0]!='s') { tail->valu"> int main() { char str[15]; node* head = new node; node* tail = head; head >prev = 0; coutstr; while(str[0]!='s') { tail->value=atoi (str); tail >next=new node; tail >next >prev=tail; tail=tail >next; coutstr; } tail >next=0; printList(head);//print unsorted list Slide 14 node* in; node* out; int temp; out=tail >prev >prev; while(out!=0) { temp=out >value; in=out >next; while(in >next!=0&&temp value) { in >prev >value=in >value; in >value=temp; in=in >next; } out=out >prev; } printList(head); // print list return 0; } Slide 15prev value next next) { coutprev value next next) { cout prev = 0; cout >str; head 0 tail str 5"> >str; head 0 tail str 5" title="int main() { char str[15]; node* head = new node; node* tail = head; head >prev = 0; coutstr; head 0 tail str 5"> int main() { char str[15]; node* head = new node; node* tail = head; head >prev = 0; coutstr; head 0 tail str 5 Slide 18>str; } tail >ne"> >str; } tail >next=0; printList(head);//print unsorted list head 05 tail str 5"> >str; } tail >ne" title="while(str[0]!='s') { tail->value=atoi (str); tail >next=new node; tail >next >prev=tail; tail=tail >next; coutstr; } tail >ne"> while(str[0]!='s') { tail->value=atoi (str); tail >next=new node; tail >next >prev=tail; tail=tail >next; coutstr; } tail >next=0; printList(head);//print unsorted list head 05 tail str 5 Slide 19>str; } tail >ne"> >str; } tail >next=0; printList(head);//print unsorted list head 05 tail str 5"> >str; } tail >ne" title="while(str[0]!='s') { tail->value=atoi (str); tail >next=new node; tail >next >prev=tail; tail=tail >next; coutstr; } tail >ne"> while(str[0]!='s') { tail->value=atoi (str); tail >next=new node; tail >next >prev=tail; tail=tail >next; coutstr; } tail >next=0; printList(head);//print unsorted list head 05 tail str 5 Slide 20>str; } tail >ne"> >str; } tail >next=0; printList(head);//print unsorted list head 05 tail str 2"> >str; } tail >ne" title="while(str[0]!='s') { tail->value=atoi (str); tail >next=new node; tail >next >prev=tail; tail=tail >next; coutstr; } tail >ne"> while(str[0]!='s') { tail->value=atoi (str); tail >next=new node; tail >next >prev=tail; tail=tail >next; coutstr; } tail >next=0; printList(head);//print unsorted list head 05 tail str 2 Slide 21>s"> >str; } tail >next=0; printList(head);//print unsorted list tail"> >s" title="Head 052738 0 while(str[0]!='s') { tail->value=atoi (str); tail >next=new node; tail >next >prev=tail; tail=tail >next; couts"> Head 052738 0 while(str[0]!='s') { tail->value=atoi (str); tail >next=new node; tail >next >prev=tail; tail=tail >next; coutstr; } tail >next=0; printList(head);//print unsorted list tail Slide 22 1) Place a pointer called out on the node that we want to insert. (When we 1 st start out is placed on the next to the last node. 2) Place a pointer called in one node to the right of out. 3) Compare the # to be inserted with the contents of what is pointed to by in. 4) If the # to be sorted is less than what is pointed to by in, swap the contents and move in, one node to the right 5) When in is at the end of the list or the # to be sorted is > than what is pointed to in, we have inserted the # into the correct position and its time to move out, 1 node to the left and repeat steps 2-5. Slide 23 while(out!=0) {... while(# to be inserted is in the wrong spot) {... in=in >next; //move in one node to the right } out=out >prev; //move out one node to the left } Slide 24 Head node* in; node* out; int temp; out=tail >prev >prev; 052738 0 tail out Slide 25 Head while (out!=0) { temp=out >value; in=out >next; while(in >next!=0&&temp value) { in >prev >value=in >value; in >value=temp; in=in >next; } out=out >prev; } 052738 0 tail out in 3 temp Slide 26 Head while (out!=0) { temp=out >value; in=out >next; while(in >next !=0 && temp value) { in >prev >value=in >value; in >value=temp; in=in >next; } out=out >prev; } 052738 0 tail out in 3 temp Slide 27 Head while (out!=0) { temp=out >value; in=out >next; while(in >next !=0 && temp value) { in >prev >value=in >value; in >value=temp; in=in >next; } out=out >prev; } 052783 0 tail out in 3 temp Slide 28 Head while (out!=0) { temp=out >value; in=out >next; while(in >next !=0 && temp value) { in >prev >value=in >value; in >value=temp; in=in >next; } out=out >prev; } 052783 0 tail outin 7 temp Slide 29 Head while (out!=0) { temp=out >value; in=out >next; while(in >next !=0 && temp value) { in >prev >value=in >value; in >value=temp; in=in >next; } out=out >prev; } 052873 0 tail outin 7 temp Slide 30 Head while (out!=0) { temp=out >value; in=out >next; while(in >next !=0 && temp value) { in >prev >value=in >value; in >value=temp; in=in >next; } out=out >prev; } 052873 0 tail outin 2 temp Slide 31 Head 058273 0 tail outin 058723 0 tail out in 058732 0 tail outin 1 2 3 Head 2 temp Slide 32 Head 058732 0 tail out in 085732 0 tail out in 087532 0 tail out in Head 4 5 5 temp 6 Slide 33 Head 087532 0 tail out= 0 in 5 temp Slide 34 Doubly linked list concept Node structure Insertion sort Insertion sort program with a doubly linked list