Docsity

Implementation of Stacks and Queues using Linked Lists in C++, Lab Reports of Data Structures and Algorithms

Toggle sidebar
bg1
18-EE-21
Lab # 08
Title:
Implementation of different operations on Stacks and Queue using Linked List
Objective:
To perform different operations on Stacks and Queue using Linked List
Equipment Required:
PC/ Laptop
Dev C++ software
Stack using Linked List:
The major problem with the stack implemented using array is, it works only for fixed number of data
values. That means the amount of data must be specified at the beginning of the implementation itself.
Stack implemented using array is not suitable, when we don't know the size of data which we are going
to use. A stack data structure can be implemented by using linked list data structure. The stack
implemented using linked list can work for unlimited number of values. That means, stack
implemented using linked list works for variable size of data. So, there is no need to fix the size at the
beginning of the implementation. The Stack implemented using linked list can organize as many data
values as we want.
In linked list implementation of a stack, every new element is inserted as 'top' element. That means
every newly inserted element is pointed by 'top'. Whenever we want to remove an element from the
stack, simply remove the node which is pointed by 'top' by moving 'top' to its next node in the list.
The next field of the first element must be always NULL.
Example
In above example, the last inserted node is 99 and the first inserted node is 25. The order of elements
inserted is 25, 32, 50 and 99.
Operations:
Push (value) - Inserting an element into the Stack
We can use the following steps to insert a new node into the stack...
Step 1: Create a newNode with given value.
Step 2: Check whether stack is Empty (top == NULL)
Step 3: If it is Empty, then set newNode → next = NULL.
Step 4: If it is Not Empty, then set newNode → next = top.
Step 5: Finally, set top = newNode.
Pop () - Deleting an Element from a Stack
We can use the following steps to delete a node from the stack...
Step 1: Check whether stack is Empty (top == NULL).
Step 2: If it is Empty, then display "Stack is Empty!!! Deletion is not possible!!!" and terminate the function
Step 3: If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'.
Step 4: Then set 'top = top → next'.
Step 7: Finally, delete 'temp' (free(temp)).
Display () - Displaying stack of elements
We can use the following steps to display the elements (nodes) of a stack...
Step 1: Check whether stack is Empty (top == NULL).
Step 2: If it is Empty, then display 'Stack is Empty!!!' and terminate the function.
1 | P a g e
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Implementation of Stacks and Queues using Linked Lists in C++ and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

Lab # 08

 Title:

Implementation of different operations on Stacks and Queue using Linked List

 Objective:

 To perform different operations on Stacks and Queue using Linked List

 Equipment Required:

 PC/ Laptop

 Dev C++ software

 Stack using Linked List:

The major problem with the stack implemented using array is, it works only for fixed number of data

values. That means the amount of data must be specified at the beginning of the implementation itself.

Stack implemented using array is not suitable, when we don't know the size of data which we are going

to use. A stack data structure can be implemented by using linked list data structure. The stack

implemented using linked list can work for unlimited number of values. That means, stack

implemented using linked list works for variable size of data. So, there is no need to fix the size at the

beginning of the implementation. The Stack implemented using linked list can organize as many data

values as we want.

In linked list implementation of a stack, every new element is inserted as 'top' element. That means

every newly inserted element is pointed by 'top'. Whenever we want to remove an element from the

stack, simply remove the node which is pointed by 'top' by moving 'top' to its next node in the list.

The next field of the first element must be always NULL.

Example

In above example, the last inserted node is 99 and the first inserted node is 25. The order of elements

inserted is 25, 32, 50 and 99.

 Operations:

Push (value) - Inserting an element into the Stack

We can use the following steps to insert a new node into the stack...

Step 1: Create a newNode with given value.

Step 2: Check whether stack is Empty (top == NULL)

Step 3: If it is Empty, then set newNode → next = NULL.

Step 4: If it is Not Empty, then set newNode → next = top.

Step 5: Finally, set top = newNode.

Pop () - Deleting an Element from a Stack

We can use the following steps to delete a node from the stack...

Step 1: Check whether stack is Empty (top == NULL).

Step 2: If it is Empty, then display "Stack is Empty!!! Deletion is not possible!!!" and terminate the function

Step 3: If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'.

Step 4: Then set 'top = top → next'.

Step 7: Finally, delete 'temp' (free(temp)).

Display () - Displaying stack of elements

We can use the following steps to display the elements (nodes) of a stack...

Step 1: Check whether stack is Empty (top == NULL).

Step 2: If it is Empty, then display 'Stack is Empty!!!' and terminate the function.

Step 3: If it is Not Empty, then define a Node pointer 'temp' and initialize with top.

Step 4: Display 'temp → data --->' and move it to the next node. Repeat the same until temp reaches to the

first node in the stack (temp → next != NULL).

Step 4: Finally! Display 'temp → data ---> NULL'.

 Queue using Linked List:

The major problem with the queue implemented using array is, it will work for only fixed number of

data. That means, the amount of data must be specified in the beginning itself. Queue using array is

not suitable when we don't know the size of data which we are going to use. A queue data structure

can be implemented using linked list data structure. The queue which is implemented using linked list

can work for unlimited number of values. That means, queue using linked list can work for variable size

of data (No need to fix the size at beginning of the implementation). The Queue implemented using

linked list can organize as many data values as we want.

In linked list implementation of a queue, the last inserted node is always pointed by 'rear' and the first

node is always pointed by 'front'.

Example

 Operations:

enQueue(value) - Inserting an element into the Queue

We can use the following steps to insert a new node into the queue...

Step 1: Create a newNode with given value and set 'newNode → next' to NULL.

Step 2: Check whether queue is Empty (rear == NULL)

Step 3: If it is Empty then, set front = newNode and rear = newNode.

Step 4: If it is Not Empty then, set rear → next = newNode and rear = newNode.

deQueue () - Deleting an Element from Queue

We can use the following steps to delete a node from the queue...

Step 1: Check whether queue is Empty (front == NULL).

Step 2: If it is Empty, then display "Queue is Empty!!! Deletion is not possible!!!" and terminate from the

function

Step 3: If it is Not Empty then, define a Node pointer 'temp' and set it to 'front'.

Step 4: Then set 'front = front → next' and delete 'temp' (free(temp)).

Display () - Displaying the elements of Queue

We can use the following steps to display the elements (nodes) of a queue...

Step 1: Check whether queue is Empty (front == NULL).

Step 2: If it is Empty then, display 'Queue is Empty!!!' and terminate the function.

Step 3: If it is Not Empty then, define a Node pointer 'temp' and initialize with front.

Step 4: Display 'temp → data --->' and move it to the next node. Repeat the same until 'temp' reaches to

'rear' (temp → next != NULL).

Step 4: Finally! Display 'temp → data ---> NULL'.

Program Output:

Note: For task # 01 & 02: The given below Menu base program contain operation on stack using linked list

Program Code:

#include #include<process.h> #include<stdio.h> #include<windows.h> #include using namespace std; struct Node { int data; Node next; }top=NULL,p; Node newnode(int x) { p=new Node; p->data=x; p->next=NULL; return(p); } void push(Node *q) { if(top==NULL) top=q; else { q->next=top; top=q; } cout<<"\n\t\tElement inserted Sucessfully....!!!"<<endl; } void pop() { if(top==NULL){ cout<<"\n\n\t\tStack is empty!!"; } else{ cout<<"\n\n\tDeleted element is "<<top->data; p=top; top=top->next; delete(p);

cout<<"\n\n\tElement deleted Sucessfully....!!!"<<endl; } } void showstack() { Node *q; q=top; if(top==NULL){ cout<<"\n\n\t\tStack is empty!!"; } else{ cout<<"\n\n\t\tList of Stack element "; while(q!=NULL) { cout<<q->data<<" "; q=q->next; } } } int main() { int ch,x; Node *nptr; while(1) { cout<<"\n\t >>>>>>>> Menu of Stack using Linked list <<<<<<<<"; cout<<"\n"; cout<<"\n\t-------------------------------------------------------------"; cout<<"\n\tENTER 1: Push / Insert element"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 2: Pop / Delete element"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 3: Displaying linked list"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 4: To Exit"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\n\tPlease Enter Your Choice: "; cin>>ch; switch(ch){ case 1: system("cls"); cout<<"\n\t\tENTER AN ELEMENT TO BE INSERTED :"; cin>>x; nptr=newnode(x); push(nptr);Sleep(2000);system("cls"); break; case 2: system("cls"); pop();Sleep(2000);system("cls"); break; case 3: system("cls"); showstack();Sleep(3000);system("cls"); break; case 4: exit(0); default: cout<<"\nWrong choice!!"; }

Option#2 is used to delete element in stack using linked list as shown in figs

3. Write a program that Enqueue insertion using linked list?

Main Program:

using namespace std; struct node { int data; struct node next; }; struct node front = NULL; struct node* rear = NULL; struct node* temp;

Function to insert EnQueue using linked list:

void Insert() { int val; cout<<"\n\t\tENTER AN ELEMENT TO BE INSERTED :"; cin>>val; if (rear == NULL) { rear = (struct node *)malloc(sizeof(struct node)); rear->next = NULL; rear->data = val; front = rear; } else { temp=(struct node *)malloc(sizeof(struct node)); rear->next = temp; temp->data = val; temp->next = NULL; rear = temp; } cout<<"\n\t\tElement inserted Sucessfully....!!!"<<endl; }

Program Output:

4. Write a program that Dequeue deletion using linked list?

Function to DeQueue using linked list:

void Delete() { temp = front; if (front == NULL) { cout<<"\n\n\t\tQueue is Underflow"<<endl; return; } else if (temp->next != NULL) { temp = temp->next; cout<<"\n\n\tElement deleted from queue is : "<<front->data<<endl; free(front); front = temp; } else { cout<<"\n\n\tElement deleted from queue is : "<<front->data<<endl; free(front); front = NULL; rear = NULL; } cout<<"\n\n\tElement deleted Sucessfully....!!!"<<endl; }

Program Output:

Note: For task # 03 & 04: The given below Menu base program contain operation on Queue using linked list

Program Code:

#include #include<stdlib.h> #include<process.h> #include<stdio.h> #include<windows.h> #include using namespace std; struct node { int data; struct node next; }; struct node front = NULL; struct node* rear = NULL; struct node* temp; void Insert() { int val; cout<<"\n\t\tENTER A ELEMENT TO BE INSERTED :"; cin>>val; if (rear == NULL) { rear = (struct node *)malloc(sizeof(struct node)); rear->next = NULL; rear->data = val;

cin>>ch; switch (ch) { case 1: system("cls");Insert();Sleep(2000);system("cls"); break; case 2: system("cls");Delete();Sleep(2000);system("cls"); break; case 3: system("cls");Display();Sleep(3000);system("cls"); break; case 4: cout<<"Exit"<<endl; break; default: cout<<"Invalid choice"<<endl; } } while(ch!=4); return 0; }

Program Output:

Menu of Queue using Linked List

Option#1 is used to EnQueue using linked list as shown in figs

Option#3 is used to Display list of Queue using linked list as shown in fig Option#2 is used to DeQueue using linked list as shown in figs