More on Linked Lists, Stacks, and Queues

Slides:



Advertisements
Similar presentations
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Advertisements

COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
Review of Lists, Stacks, and Queues CS 400/600 – Data Structures.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Review Array Array Elements Accessing array elements
What is a Queue? Queue is a linear data structure in which the insertion and deletion operations are performed at two different ends. In a queue data structure,
Stacks.
CSCE 210 Data Structures and Algorithms
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Data Structures and Algorithms
Dr. Bernard Chen Ph.D. University of Central Arkansas
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Homework 4 questions???.
Queue data structure.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Algorithms and Data Structures
CMSC 341 Lecture 5 Stacks, Queues
Chapter 19: Stacks and Queues.
C++ Plus Data Structures
Stacks, Queues, and Deques
Popping Items Off a Stack Lesson xx
Linked List (Part I) Data structure.
ADT list.
Data Structures and Algorithms
CSC 143 Stacks [Chapter 6].
Abstract Data Type Abstract Data Type as a design tool
Stacks Data structure Elements added, removed from one end only
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Pointers & Dynamic Data Structures
Stacks and Queues.
Stacks CS-240 Dick Steflik.
LAB#3 Stacks Nora Albabtin nora albabtin.
CSI 1340 Introduction to Computer Science II
Stacks, Queues, and Deques
Abstract Data Types Stacks CSCI 240
Data Structures & Programming
Presentation transcript:

More on Linked Lists, Stacks, and Queues Anwar Alhenshiri

Array Limitations Arrays Simple, Fast but Must specify size at construction time Murphy’s law Construct an array with space for n n = twice your estimate of largest collection Tomorrow you’ll need n+1 More flexible system?

Linked Lists Linked list Flexible space use Dynamically allocate space for each element as needed Include a pointer to the next item Linked list Each node of the list contains the data item (an object pointer in our ADT) a pointer to the next node Data Next object

Linked Lists Collection structure has a pointer to the list head Initially NULL Add first item Allocate space for node Set its data pointer to object Set Next to NULL Set Head to point to new node Collection node Head Data Next object

Linked Lists Add second item Allocate space for node Set its data pointer to object Set Next to current Head Set Head to point to new node Collection Head node node Data Next object2 Data Next object

Linked Lists Example Code in C++

Declaring a List Using struct #include<iostream.h> #include<conio.h> #include<stdlib.h> class list { struct node int data; node *link; }*p; public: void inslast(int); void insbeg(int); void insnext(int,int); void delelement(int); void delbeg(); void dellast(); void disp(); int seek(int); list(){p=NULL;} ~list(); };

Inserting an Item to the End of the List void list::inslast(int x) { node *q,*t; if(p==NULL) // list is empty p=new node; p->data=x; p->link=NULL; } else { q=p; while(q->link!=NULL) q=q->link; t=new node; t->data=x; t->link=NULL; q->link=t; } cout<<" Inserted successfully at the end.. "; disp();

Inserting an Item at the Beginning of the List void list:: insbeg(int x) { node *q; q=p; p=new node; p->data=x; p->link=q; cout<<" Inserted successfully at the begining.. "; disp(); }

Deleting an Item from the List void list::delelement(int x) { node *q,*r; q=p; if(q->data==x) // item is at beginning p=q->link; delete q; return; } r=q; while(q!=NULL) { if(q->data==x) r->link=q->link; delete q; return; } r=q; q=q->link; cout<<“ Element u entered "<<x<<" is not found.."; } // end of function delelement

Deleting an Item at the Beginning void list:: delbeg() { cout<<“ The list before deletion: "; disp(); node *q; q=p; if(q==NULL) cout<<“ No data is present.."; return; } p=q->link; delete q;

Deleting the Last Item in the List void list:: dellast() { cout<<“ The list before deletion: "; disp(); node *q,*t; q=p; if(q==NULL) cout<<“ There is no data in the list.."; return; } if(q->link==NULL) { p=q->link; delete q; return; } while(q->link->link!=NULL) q=q->link; q->link=NULL;

List Destructor list::~list() { node *q; if(p==NULL) return; while(p!=NULL) q=p->link; delete p; p=q; }

Displaying Items of the List void list::disp() { node *q; q=p; if(q==NULL) cout<<“ No data is in the list.."; return; } cout<<“ The items present in the list are :"; while(q!=NULL) { cout<<" "<<q->data; q=q->link; }

Inserting an Item in the List at a Certain Position void list :: insnext(int value,int position) { node *temp,*temp1; temp=p; if(temp1==NULL) temp1= new node; temp1->data=value; temp1->link=NULL; p=temp1; return; } for(int i=0;((i<position)&&(temp->link!=NULL)) ;i++) { if(i==(position-1)) temp1= new node; temp1->data= value; temp1->link=temp->link; temp->link=temp1; } temp=temp->link; //cout<<“ Inserted successfully at the position.."<<position; disp();

Returning the Position of an Item in the List int list::seek(int value) { node *temp; temp=p; int position=0; while(temp!=NULL) if(temp->data==value) return position+1; else temp=temp->link; position=position+1; } cout<<“ Element "<<value<<" not found"; return 0; }

Function MAIN() You can write it yourself. Try to display choices for doing what you want to do. Try all kinds of operations on the list.

Stacks Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add item to the top of the stack void *pop( Stack s ); - remove an item from the top of the stack Like a plate stacker Other methods int IsEmpty( Stack s ); /* Return TRUE if empty */ void *Top( Stack s ); /* Return the item at the top, without deleting it */

Stacks - Implementation Arrays Provide a stack capacity to the constructor Flexibility limited but matches many real uses Capacity limited by some constraint Memory in your computer Size of the plate stacker, etc push, pop methods Variants of AddToC…, DeleteFromC… Linked list also possible Stack: basically a Collection with special semantics!

Stacks - Relevance Stacks appear in computer programs Stack frame Key to call / return in functions & procedures Stack frame allows recursive calls Call: push stack frame Return: pop stack frame Stack frame Function arguments Return address Local variables

Stacks (Declaration, Constructor, Destructor) Stack::Stack(int size) { top=-1; length=size; if(size == 0) p = 0; else p=new int[length]; } Stack::~Stack() if(p!=0) delete [] p; #include <iostream> using namespace std; class Stack { private: int *p; int top,length; public: Stack(int = 0); ~Stack(); void push(int); int pop(); void display(); }; A Stack is a data structure which works on the principle LIFO(Last In, First Out).The basic operations in a Stack are : 1. Push : In which we push the data into the stack. 2. Pop : In which we remove an element from the Stack. All insertions and removals are done only from one side of the Stack , which is called the 'top' of the Stack. A stack is generally used in function calls where the local variables are pushed onto the Stack and when the function returns , it pops the variables from the Stack.

Stacks (Push, Pop, Display) int Stack::pop() { if(p==0 || top==-1) { cout<<"Stack empty!"; return -1; } int ret=p[top]; top--; return ret; } void Stack::display() for(int i = 0; i <= top; i++) cout<<p[i]<<" "; cout<<endl; void Stack::push(int elem) { if(p == 0) //If the stack size is zero, allow user to mention it at runtime { cout<<"Stack of zero size"<<endl; cout<<"Enter a size for stack : "; cin >> length; p=new int[length]; } if(top==(length-1)) //If the top reaches to the maximum stack size cout<<"\nCannot push "<<elem<<", Stack full"<<endl; return; else top++; p[top]=elem; }

Main Function and Some Operations int main() { Stack s1; //We are creating a stack of size 'zero' s1.push(1); s1.display(); s1.push(2); s1.push(3); s1.push(4); s1.push(5); s1.pop(); }

Queues Implementation in C++

Queues Queue is a first-in, first-out (FIFO) data structure. i.e. the element added first to the queue will be the one to be removed first. Elements are always added to the back of the queue and removed from the front of the queue. Typical use cases of queues include:- (1) In Inter-Process Communication (IPC) message queues is a common mechanism for communication between processes.

Queues, Cont’d Some of the common terminology associated with queues include: ADD/ PUSH and DELETE/ POP of elements to the queue. ADD/ PUSH refers to adding an element to the queue. DELETE/ POP refers to removing an element from the queue.

Queue Example in C++ using a Linked List #include <iostream> using namespace std; class QueueEmptyException { public: QueueEmptyException() cout << "Queue empty" << endl; } }; class Node int data; Node* next; class ListQueue { private: Node* front; Node* rear; int count; public: ListQueue() front = NULL; rear = NULL; count = 0; }

Example, cont’d void Enqueue(int element) { Node* tmp = new Node(); // Create a new node tmp->data = element; tmp->next = NULL; if ( isEmpty() ) { // Add the first element front = rear = tmp; } else { // Append to the list rear->next = tmp; rear = tmp; count++; int Dequeue() { if ( isEmpty() ) throw new QueueEmptyException(); int ret = front->data; Node* tmp = front; // Move the front pointer to next node front = front->next; count--; delete tmp; return ret; }

Main Function // Size of queue int Front() { if ( isEmpty() ) throw new QueueEmptyException(); return front->data; } int Size() return count; bool isEmpty() return count == 0 ? true : false; }; int main() { ListQueue q; try { if ( q.isEmpty() ) cout << "Queue is empty" << endl; } // Enqueue elements q.Enqueue(100); q.Enqueue(200); q.Enqueue(300); // Size of queue cout << "Size of queue = " << q.Size() << endl; // Front element cout << q.Front() << endl; // Dequeue elements cout << q.Dequeue() << endl; } catch (...) { cout << "Some exception occured" << endl;