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

Slides:



Advertisements
Similar presentations
Slide 1 Insert your own content. Slide 2 Insert your own content.
Advertisements

COMP171 Fall 2005 Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
CSCE 3110 Data Structures & Algorithm Analysis
Linked Lists: deleting...
Linked Lists Chapter 4.
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Linear Lists – Linked List Representation
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
LINKED LIST, STACKS AND QUEUES Saras M Srivastava, PGT – Comp. Sc. Kendriya Vidyalaya TengaValley.
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
LIST PROCESSING.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Linked List Variations
1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.
1 Pointers and Strings Section 5.4, , Lecture 12.
Data Structure Lecture-5
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
1 Writing a Good Program 8. Elementary Data Structure.
+ struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
Abstract Data Types and a review of C++ programming concepts CS 400/600 – Data Structures.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
Algorithms and Data Structures
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
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.
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Doubly / Two Way Linked List It is linear collection of data elements, called nodes, where each node N is divided into three parts: – An information field.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
Programming Circular Linked List.
Popping Items Off a Stack Using a Function Lesson xx
Lectures linked lists Chapter 6 of textbook
Sequences 6/18/2018 8:51 PM C201: Linked List.
Doubly Linked List Review - We are writing this code
Linked lists.
CSCE 210 Data Structures and Algorithms
Sequences 9/18/ :20 PM C201: Linked List.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
Popping Items Off a Stack Lesson xx
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Pass by Reference.
Linked List and Selection Sort
List Iterator Implementation
Linked lists.
An Introduction to STL.
Data Structures & Programming
Presentation transcript:

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

Head 0abcde0

7 struct node { node * prev; int val; node * next; };

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

Head Fig. 1 Fig. 2

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.

1.Well use the insertion sort to rearrange the following list of #s in descending order: Place a pointer called out on the next to the last # 3.Place a pointer called in, one node to the right of out 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 out in

6.After swapping the #s, move in one node to the right 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 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 out in

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

#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*);

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

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; }

void printList(const node* h) { for (const node* p = h; p; p = p->next) { cout << "node address: " << p << prev << p->prev value next << endl; } }

#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*);

int main() { char str[15]; node* head = new node; node* tail = head; head >prev = 0; cout<<"enter a number"; cin>>str; head 0 tail str 5

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 05 tail str 5

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 05 tail str 5

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 05 tail str 2

Head 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 tail

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.

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 }

Head node* in; node* out; int temp; out=tail >prev >prev; tail out

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; } tail out in 3 temp

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; } tail out in 3 temp

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; } tail out in 3 temp

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; } tail outin 7 temp

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; } tail outin 7 temp

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; } tail outin 2 temp

Head tail outin tail out in tail outin Head 2 temp

Head tail out in tail out in tail out in Head temp 6

Head tail out= 0 in 5 temp

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