You are on page 1of 4

LOKMANYA TILAK COLLEGE OF ENGINEERING

Navi Mumbai
Department of Computer Engineering
Academic Year (2021-2022
(Odd -SEM) (CBCGS- R19- ‘C’ Scheme)

Course Name: Skill Based lab Course: Data Structure Lab

Course Code: CSL303

Experiment No:05
Title of Experiment: To Implement Stack Operation Using
. Linked List

Name of Student: SHARDUL VANAGE

Student Roll No: IOTE67

Year/Semester: DSE/III

Date Of Performance: 21/01/22

Date of Submission: 22/01/22


EXPERIMENT NO. 03

To Implement Stack Operation Using Linked List


CODE-

#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
struct Node* link;
};

struct Node* top;

void push(int data)


{

struct Node* temp;


temp = new Node();

if (!temp)
{
cout << "\nHeap Overflow";
exit(1);
}
temp->data = data;

temp->link = top;
top = temp;
}

int isEmpty()
{
return top == NULL;
}

int peek()
{

// Check for empty stack


if (!isEmpty())
return top->data;
else
exit(1);
}

void pop()
{
struct Node* temp;

// Check for stack underflow


if (top == NULL)
{
cout << "\nStack Underflow" << endl;
exit(1);
}
else
{
temp = top;

top = top->link;

temp->link = NULL;

free(temp);
}
}

void display()
{
struct Node* temp;

if (top == NULL)
{
cout << "\nStack Underflow";
exit(1);
}
else
{
temp = top;
while (temp != NULL)
{
cout << temp->data << "-> ";

temp = temp->link;
}
}
int main()
{

// Push the elements of stack


push(11);
push(22);
push(33);
push(44);

// Display stack elements


display();

// Print top element of stack


cout << "\nTop element is "
<< peek() << endl;

// Delete top elements of stack


pop();
pop();

// Display stack elements


display();

// Print top element of stack


cout << "\nTop element is "
<< peek() << endl;

return 0;
}

Output:

You might also like