Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Lab06 Stack using Linked List

This document outlines a lab exercise focused on constructing a Stack Abstract Data Type (ADT) using a linked list implementation. It details the basic operations of a stack, including push, pop, and display, along with a provided C++ code example for these operations. Additionally, it includes tasks for further development, such as displaying the top item of the stack and implementing a stack using a doubly linked list.

Uploaded by

syedshahrukh1425
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab06 Stack using Linked List

This document outlines a lab exercise focused on constructing a Stack Abstract Data Type (ADT) using a linked list implementation. It details the basic operations of a stack, including push, pop, and display, along with a provided C++ code example for these operations. Additionally, it includes tasks for further development, such as displaying the top item of the stack and implementing a stack using a doubly linked list.

Uploaded by

syedshahrukh1425
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab No.

06
Objectives:

To construct Stack ADT using a non-contiguous data structure

Introduction
We go for a linked list implementation of a stack rather than an array implementation because
of the run time memory allocation feature of linked lists. In this implementation we make a
head pointer and make it point to the first created node in the stack. Whenever a new node is
pushed into the stack we make the head pointer point to the new node and the new node to
point to the already existing node that was pointed by the head pointer.

Basic Operation Associated with Stacks:


1) Insert (Push) an item into the stack.
2) Delete (Pop) an item from the stack.
3) Display the items.

Creation of head pointer initially:

Pushing an element:

Pushing a second element:

Figure 1: Representation of Stack

Lab Tasks:
1. Compile and examine the program provided below. Print the output in the space
provided.
Code:
#include<iostream>
using namespace std;

// Creating a NODE Structure


struct node
{
int data;
struct node *next;
};

// Creating a class STACK


class stack
{
struct node *top;
public:
stack() // constructure
{
top=NULL;
}
void push(); // to insert an element
void pop(); // to delete an element
void show(); // to show the stack
};
// PUSH Operation
void stack::push()
{
int value;
struct node *ptr;
cout<<"\nPUSH Operation:";
cout<<"Enter a number to insert: ";
cin>>value;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
cout<<"\nNew item is inserted to the stack!!!";

// POP Operation
void stack::pop()
{
struct node *temp;
if(top==NULL)
{
cout<<"\nThe stack is empty!!!";
}
temp=top;
top=top->next;
cout<<"\nPOP Operation........\nPoped value is "<<temp->data;
delete temp;
}

// Show stack
void stack::show()
{
struct node *ptr1=top;
cout<<"\nThe stack is\n";
while(ptr1!=NULL)
{
cout<<ptr1->data<<" ->";
ptr1=ptr1->next;
}
cout<<"NULL\n";
}

// Main function
int main()
{
stack s;
int choice;
while(1)
{
cout<<"\n-----------------------------------------------------------";
cout<<"\n\t\tSTACK USING LINKED LIST\n\n";
cout<<"1:PUSH\n2:POP\n3:DISPLAY STACK\n4:EXIT";
cout<<"\nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.show();
break;
case 4:
return 0;
break;
default:
cout<<"\nPlease enter correct choice(1-4)!!";
break;
}
}
return 0;
}
Output:
2. Develop a program that displays the top item of the stack.
3. Develop a program that implements Stack using Doubly Linked List

Code:

Output:

You might also like