doubly linked list lesson xx
Download
Skip this Video
Download Presentation
Doubly Linked List Lesson xx

Loading in 2 Seconds...

play fullscreen
1 / 33

Doubly Linked List Lesson xx - PowerPoint PPT Presentation


  • 112 Views
  • Uploaded on

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 {

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Doubly Linked List Lesson xx' - virgo


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
objectives
Objectives
  • Doubly linked list concept
  • Node structure
  • Insertion sort
  • Insertion sort program with a doubly linked list
node structure for a doubly linked list node
Node Structure for a Doubly Linked List Node

struct node

{

node * prev;

intval;

node * next;

};

7

program description
Program Description

Ask 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

program illustration
Program Illustration

Fig. 1

Head

Head

0

0

2

2

3

5

7

5

8

3

7

8

0

0

Fig. 2

insertion sort logic
Insertion 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 it’s time to move out, 1 node to the left and repeat steps 2-5.

insertion sort illustration 1
Insertion Sort Illustration 1

We’ll use the insertion sort to rearrange the following list of #s in descending order:

5 2 7 3 8

Place a pointer called out on the next to the last #

Place a pointer called in, one node to the right of out

5 2 7 3 8

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

5 2 7 8 3

out in

insertion sort illustration 2
Insertion Sort Illustration 2

After swapping the #s, move in one node to the right

5 2 7 8 3

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

5 2 7 8 3

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.

5 2 8 7 3

out in

insertion sort illustration 3
Insertion Sort Illustration 3

11. .Compare 7 and 3. 7 is > 3 so we have inserted 7 in to the correct position in the list.

5 2 8 7 3

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.

5 2 8 7 3

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.

program listing part 1
Program Listing Part 1

#include <iostream> using std::cin; using std::cout; using std::flush; using std::endl;

#include <cstdlib>

struct node { node* prev;int value;   node* next; };

void printList(const node*);

program listing part 2
Program Listing Part 2

int main() {   char str[15];

  node* head = new node;   node* tail = head; head‑>prev = 0;

cout<<"enter a number";

cin>>str;

while(str[0]!=\'s\')

{

tail->value=atoi (str);

tail‑>next=new node;

tail‑>next‑>prev=tail;

tail=tail‑>next;

cout<<"enter a number";

cin>>str;

}

tail‑>next=0;

printList(head);//print unsorted list

program listing part 3
Program 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&&temp<in‑>value)

{

in‑>prev‑>value=in‑>value;

in‑>value=temp;

in=in‑>next;

}

out=out‑>prev;

}

printList(head); // print list   return 0; }

program listing part 4
Program Listing Part 4

void printList(const node* h) {   for (const node* p = h; p; p = p->next)   {

cout << "node address: " << p << “ prev “ <<

p->prev   << " value " << p->value       << " next " << p->next << endl;   } }

preprocessor directives node definition function prototype
Preprocessor Directives, Node Definition & Function Prototype

#include <iostream> using std::cin; using std::cout; using std::flush; using std::endl;

#include <cstdlib>

struct node { node* prev;int value;   node* next; };

void printList(const node*);

declarations initialization and priming read
Declarations, Initialization and Priming Read

int main() { char str[15];

node* head = new node; node* tail = head; head‑>prev = 0;

cout<<"enter a number";

cin>>str;

head

0

str

“5”

tail

start building the doubly linked list
Start Building the Doubly Linked List

while(str[0]!=\'s\')

{

tail->value=atoi (str);

tail‑>next=new node;

tail‑>next‑>prev=tail;

tail=tail‑>next;

cout<<"enter a number";

cin>>str;

}

tail‑>next=0;

printList(head);//print unsorted list

head

0

5

str

“5”

tail

connect 2 nd node to 1st
Connect 2nd Node to 1st

while(str[0]!=\'s\')

{

tail->value=atoi (str);

tail‑>next=new node;

tail‑>next‑>prev=tail;

tail=tail‑>next;

cout<<"enter a number";

cin>>str;

}

tail‑>next=0;

printList(head);//print unsorted list

head

0

5

str

“5”

tail

advancing tail pointer
Advancing tail Pointer

while(str[0]!=\'s\')

{

tail->value=atoi (str);

tail‑>next=new node;

tail‑>next‑>prev=tail;

tail=tail‑>next;

cout<<"enter a number";

cin>>str;

}

tail‑>next=0;

printList(head);//print unsorted list

head

0

5

str

“2”

tail

completed doubly linked list
Completed Doubly Linked List

while(str[0]!=\'s\')

{

tail->value=atoi (str);

tail‑>next=new node;

tail‑>next‑>prev=tail;

tail=tail‑>next;

cout<<"enter a number";

cin>>str;

}

tail‑>next=0;

printList(head);//print unsorted list

0

5

2

7

3

8

0

tail

Head

insertion sort logic1
Insertion 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 it’s time to move out, 1 node to the left and repeat steps 2-5.

basic code outline
Basic 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

}

set up pointers for insertion sort
Set Up Pointers for Insertion Sort

node* in;

node* out;

int temp;

out=tail‑>prev‑>prev;

Head

0

5

2

7

3

8

0

tail

out

set up in temp
Set Up in & temp

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;

}

temp

3

0

5

2

7

3

8

0

tail

Head

out

in

see if is in correct position
See If # is in Correct Position

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;

}

temp

3

0

5

2

7

3

8

0

tail

Head

out

in

slide26
Swap

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;

}

temp

3

0

5

2

7

8

3

0

tail

Head

in

out

insert next 7 into list
Insert Next # 7 into List

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;

}

temp

7

0

5

2

7

8

3

0

tail

Head

out

in

inner loop the 2 nd time
Inner Loop the 2nd Time

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;

}

temp

7

0

5

2

8

7

3

0

tail

Head

out

in

insert next 2 into list
Insert Next # 2 into List

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;

}

temp

2

0

5

2

8

7

3

0

tail

Head

out

in

slide30

2

temp

Head

1

Head

2

0

0

0

2

2

7

3

5

7

2

8

5

3

5

8

7

3

8

0

0

0

tail

tail

tail

3

out

out

out

in

in

in

slide31

5

temp

Head

4

Head

5

0

0

0

7

5

8

7

3

8

2

2

3

2

8

7

5

3

5

0

0

0

tail

tail

tail

Head

6

out

out

in

out

in

in

slide32

5

temp

Head

0

8

7

5

3

2

0

tail

out= 0

in

summary
Summary
  • Doubly linked list concept
  • Node structure
  • Insertion sort
  • Insertion sort program with a doubly linked list
ad