+ All Categories
Home > Documents > Doubly Linked List Lesson xx

Doubly Linked List Lesson xx

Date post: 24-Feb-2016
Category:
Upload: virgo
View: 57 times
Download: 0 times
Share this document with a friend
Description:
Doubly Linked List Lesson xx. Objectives. Doubly linked list concept Node structure Insertion sort Insertion sort program with a doubly linked list. Illustration of a Doubly Linked List. Head. 0. a. b. c. d. e. 0. Node Structure for a Doubly Linked List Node. struct node { - PowerPoint PPT Presentation
Popular Tags:
33
Doubly Linked List Lesson xx
Transcript

Slide 1

Doubly Linked ListLesson xx

In this presentation, well talk about doubly linked list.1ObjectivesDoubly linked list conceptNode structureInsertion sortInsertion sort program with a doubly linked list

Our goal is to show you what a linked list looks like pictorially and introduce you to the structure a node in a doubly linked list. Well then talk about the insertion sort and how to implement it using a doubly linked list.2Illustration of a Doubly Linked ListHead0abcde0

A doubly linked list consists of a series of records that are joined together using pointers both in the forward and the backward direction. Looking at the picture, record b points forward to the next record which is record c. Record b also points backwards to record a. Record a is the 1st node of the list so there is no node previous to it. Therefore we use a null to indicate that fact. Record e is the last node so there is no node following it. Again, we use a null to indicate that. Doubly linked list are used when you need to move forwards and backwards in the list. In a singly linked list, you can only move forward.3Node Structure for a Doubly Linked List Node7struct node { node * prev; int val; node * next; };

Shown here is one node of a doubly linked list. We have a pointer that points to the previous node, the contents of the node and a pointer that points to the next node. The code on the right is an example of how to set up the structure for the node. node* prev; means that we have a pointer to a structure of the form node. In our node, we have an integer data member called val. node *next; indicates that we have another pointer to a structure of the form node.4Program DescriptionAsk the use to enter an integer

Store the # in one node of a doubly linked list

Repeat steps 1 & 2 until the user enters s to terminate input

Use the insertion sort to sort the #s that are in the doubly linked list

Print out the sorted list

Here are the specifications for the program that we are going to write. 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 list5Program IllustrationHead05273800875320HeadFig. 1Fig. 2

Here is an illustration of the program that we will write. 1) Well read in an integer and put it in one node of a doubly linked list. 2) When the user is through with input, well have a doubly linked list as shown in Fig. 1. 3) We want to use the insertion sort to rearrange the list so that the numbers are in descending order as shown in Fig. 2.6Insertion Sort Logic 1) Place a pointer called out on the node that we want to insert. (When we 1st 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.

Lets look at the general logic of the insertion sort before we begin. Well illustrate it with an example in the next slides. 1) Place a pointer called out on the node that we want to insert. (When we 1st start, out is placed on the next to the last node. 2) a pointer called in is placed on the 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, we 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 > that 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.

7Insertion Sort Illustration 1Well use the insertion sort to rearrange the following list of #s in descending order: 52 7 38

Place a pointer called out on the next to the last #Place a pointer called in, one node to the right of out

52 7 38 out in

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

Lets look at a specific example. 1) Well demonstrate the insertion sort using the unsorted list of numbers shown: 5, 2, 7, 3, 8. 2) Place a pointer called out on the next to the last # in the list (in this case 3). 3) place a pointer called in, one node to the right of out. 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.

8Insertion Sort Illustration 2After 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 position8.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 list10.Since 7 is < 8, we need to swap the numbers and also move in one node to the right.

52 873 out in

6) After swapping the #s, move in one node to the right. 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 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.9Insertion Sort Illustration 311. .Compare 7 and 3. 7 is > 3 so we have inserted 7 in to the correct position in the list.

52 873 out in12. 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 in13. 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.

11)Compare 7 and 3. Since 7 is > 3 so we have inserted 7 in to the correct position in the list. 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. 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.

10Program Listing Part 1#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*);

This is part 1 of the program which contains the preprocessor directives, node definition and function declaration.11Program Listing Part 2int 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

Part 2 of the program reads in the users input and builds the doubly linked list.12Program Listing Part 3 node* in; node* out; int temp; out=tail>prev>prev; while(out!=0) { temp=out>value; in=out>next; while(in>next!=0&&tempvalue) { in>prev>value=in>value; in>value=temp; in=in>next; } out=out>prev; } printList(head); // print list return 0; }

In part 3 of the program, we have the code that implements the insertion sort.13Program Listing Part 4void printList(const node* h) { for (const node* p = h; p; p = p->next) { cout prev=tail; tail=tail>next; coutstr; } tail>next=0; printList(head);//print unsorted list head05tailstr5

This part of the code in red starts building the doubly linked list. Outside of the while loop, in the previous slide, we did a priming read and read our input into str as a string. In while loop, we are checking to see if the 1st character of the string is the letter s. If it is, the user wants to quit and we exit the loop. Inside the while loop, tail->value = atoi (str); converts the string in str into the # 5 and stores it in tail->value. tail->next = new node; allocates a new node and makes tail->next point to it. At this point, we have the picture shown. Head and tail both point to the 1st node which contains our 1st input of the # 5. tail->next points to the 2nd node.17Connect 2nd Node to 1st 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 head05tailstr5

Lets continue on with the code in the while loop. Now, we must make the 2nd node point back to the 1st node. Thats what tail->next->prev = tail; does. We store the pointer called tail in tail->next->prev.18Advancing tail Pointer 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 head05tailstr2

In the previous slide, the pointer called tail was pointing to the 1st node. It should always point to the last node, so have the statement: tail = tail->next. Now, tail is pointing to the 2nd node. The cout and the cin statement prompts and reads in the 2nd and subsequent items of input. If the user enter the letter s, we exit the loop. If they dont, we repeat the loop.19Completed Doubly Linked ListHead0527380 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

When we exit the while loop, tail is pointing to the last node which is a dummy node We mark the end of the list with the statement tail->next = 0; We call the printList() function to print the contents of our unsorted list. Our doubly linked list looks like the picture shown.20Insertion Sort Logic 1) Place a pointer called out on the node that we want to insert. (When we 1st 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.

Before we look at the code for the insertion sort, lets review the general logic. 1) We place a pointer called out on the node that we want to insert 2) a pointer called in is placed on the node to the right of out. 3) we 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, we 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 > that 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.

21Basic Code Outline 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 }

We will need 2 loops to implement our logic. The outer loop will advance out 1 node to the left until we hit the null. The inner loop will advance in one node to the right until the # is in the correct position or in is off the list. Hey, do you see why we need a doubly linked list? We need to go forwards and backwards in the list and we cant go backwards in a singly linked list22Set Up Pointers for Insertion SortHead node* in; node* out; int temp; out=tail>prev>prev;0527380tailout

When we get to this point in the code, the doubly linked list is built and we are ready to write the insertion sort to rearrange the nodes so that they are in descending order. in is another pointer that well use in a moment. temp is used to temporarily hold the # that is being inserted into the list. The algorithm calls for a pointer called out to be pointing to the next the last node. Since the last node is a dummy node, out needs to be pointing to the 3rd node from the end which is what the statement: out = tail->prev->prev does.23Set Up in & tempHead while (out!=0) { temp=out>value; in=out>next; while(in>next!=0&&tempvalue) { in>prev>value=in>value; in>value=temp; in=in>next; } out=out>prev; }

0527380tailoutin3temp

The # pointed to by out (3) is the item we need to insert into the list. We store the #3 in temp with the statement: temp= out->value; The algorithm calls for a pointer called in to be pointing one node to the right of out. That is done with: in = out->next. After we execute the 3 lines in red, we get the picture shown: out is pointing to the next to the last node, in is pointing one node to the right of out and temp contains a copy of the # we want to insert into the list.24See If # is in Correct PositionHead while (out!=0) { temp=out>value; in=out>next; while(in>next !=0 && temp < in>value) { in>prev>value=in>value; in>value=temp; in=in>next; } out=out>prev; }

0527380tailoutin3temp

In our example, we are trying to insert the # 3 into the correct position. The statement: while (in >next !=0 && temp < in>value) checks to see if in is not off the list and 3 < 8.Since 3 is in the wrong position, we enter the body of the loop which will swap the two #s.25SwapHead while (out!=0) { temp=out>value; in=out>next; while(in>next !=0 && temp< in>value) { in>prev>value=in>value; in>value=temp; in=in>next; } out=out>prev; }

0527830tailoutin3temp

In the body of the loop which is in red, we swap the #s that are in the wrong position. In our example, we put the 8 where the 3 was and vice versa. in->prev->value = in->value; takes the # 8 and stores it where the 3 was. temp contains the # to be inserted into the list. in->value = temp ; takes the # 3 and stores it where the 8 was. The last statement in the body, in= in->next; moves in, one node to the right. The picture shown is the memory after we have executed the body of the inner while loop.26Insert Next # 7 into ListHead while (out!=0) { temp=out>value; in=out>next; while(in>next !=0 && temp< in>value) { in>prev>value=in>value; in>value=temp; in=in>next; } out=out>prev; }

0527830tailoutin7temp

From the previous slide, in->next was pointing to the null so we exit the inner while loop and execute the last statement in red: out = out->prev; This moves out 1 node to the left and it is now pointing to the node that contains the # 7. We go back up to the top of the outer while loop and out !=0. We execute temp = out->value; which puts the # 7 in temp. Remember, temp always contains the # we want to insert into the list. in= out->next; places in, one node to the right of out. Now we enter the inner loop again and we are inserting 7 into the list.27Inner Loop the 2nd TimeHead while (out!=0) { temp=out>value; in=out>next; while(in>next !=0 && temp< in>value) { in>prev>value=in>value; in>value=temp; in=in>next; } out=out>prev; }

0528730tailoutin7temp

When we enter the inner while loop for the 2nd time, we are going to insert 7 into the sorted list. 7 is < 8 and therefore in the wrong place so we swap it. After we have executed the code in red, we get the picture that is shown. When we repeat the inner while loop, we are checking to see if temp < in->value. In other words, is 7 < 3. Since it is not, we exit the inner loop.28Insert Next # 2 into ListHead while (out!=0) { temp=out>value; in=out>next; while(in>next !=0 && temp< in>value) { in>prev>value=in>value; in>value=temp; in=in>next; } out=out>prev; }

0528730tailoutin2temp

When we exit the inner while loop the 2nd time, out moves to the left with the statement, out = out-> prev; Now, out is pointing to the node that contains the # 2. The numbers to the right of out are sorted in descending order. We go back to the top of the outer while loop and out !=0. We put the # 2 in temp with the statement: temp = out->value; Remember, temp contains the # we wish to insert into the list. We point in one node to the right of out with the statement: in = out->next;29Head0582730tailoutin0587230tailoutin0587320tailoutin123Head

2tempBy now, you get the general idea: out moves 1 node to the left and in moves to the right until the # to be inserted is in the correct position or in->next contains a null. Continuing from the previous slide, 2< 8 so we swap the nodes and move in to the right. After the swap, our linked list looks like figure 1. Since 2 < 7, we do another swap and move in producing figure 2. Now, 2 < 3, we do another exchange and advance in thus we have figure 3. in->next now contains a null and the numbers starting from out are sorted in order.30Head0587320tailoutin0857320tailoutin0875320tailoutinHeadHead455temp6

In figure 4, out moves 1 node to the left and in is place one node to the right of out. We are now going to insert 5 into the list using the inner while loop. Since 5 < 8, we swap # and we have figure 5 and in is pointing to 7. 5 < 7 so we swap and move in which produces figure 6.31Head0875320tailout= 0in

5temp Now, we compare 5 and 3. Since 5 is > 3, we have inserted 5 into the correct spot in the doubly linked list. We execute the statement: out = out->prev; which puts a null in out and we are through with the insertion sort. The last thing we do is to call the printList() function to print our sorted list.32SummaryDoubly linked list conceptNode structureInsertion sortInsertion sort program with a doubly linked list

In this module, we talked about and illustrated a doubly linked list. We showed you what the structure of a node looks like in a doubly linked list. We talked about the insertion sort and then wrote a program using the insertion sort.33


Recommended