Presentation is loading. Please wait.

Presentation is loading. Please wait.

More on Linked Lists, Stacks, and Queues

Similar presentations


Presentation on theme: "More on Linked Lists, Stacks, and Queues"— Presentation transcript:

1 More on Linked Lists, Stacks, and Queues
Anwar Alhenshiri

2 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?

3 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

4 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

5 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

6 Linked Lists Example Code in C++

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

8 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();

9 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(); }

10 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

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

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

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

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

15 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();

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

17 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.

18 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 */

19 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!

20 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

21 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.

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

23 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(); }

24 Queues Implementation in C++

25 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.

26 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.

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

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

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


Download ppt "More on Linked Lists, Stacks, and Queues"

Similar presentations


Ads by Google