0% found this document useful (0 votes)
11 views3 pages

CS301P Lab 2 Solution

Uploaded by

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

CS301P Lab 2 Solution

Uploaded by

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

// CS301P Lab 2 Problem KST Learning

#include<iostream>
#include<conio.h>
using namespace std;

class Stack_Array
{
private:
int array[5];
int top = -1;
public:
void push(int d)
{
if(top >= 5)
{
cout << "\n\n*** Stack is Full ***";
}
else
{
array[++top] = d;
}
}
void pop()
{
if(top == -1)
{
cout << "\n\n*** Stack is Empty ***";
}
else
{
cout << "\n\nStack With Array Elements : ";
for(int i=top; i >= 0; i--)
{
cout << array[i] << " ";
}
}
}
};

class Node
{
private:
int data;
Node *nextAdd;
public:
void setData(int d)
{
data = d;
}
void setNext(Node *ptr)
{
nextAdd = ptr;
}
int getData()
{
return data;
}
Node* getNext()
{
return nextAdd;
}
};

class Stack_LinkedList
{
private:
Node *Head;
public:
Stack_LinkedList()
{
Head = NULL;
}
void push(int d)
{
Node *newNode = new Node;
newNode -> setData(d);
newNode -> setNext(NULL);

if(Head == NULL)
{
Head = newNode;
}
else
{
newNode -> setNext(Head);
Head = newNode;
}
}
void pop()
{
if(Head == NULL)
{
cout << "\n\n*** Stack is Empty ***";
}
else
{
Node *ptr = Head;
cout << "\n\nStack With Linked List Elements : ";
while(ptr != NULL)
{
cout << ptr -> getData() << " ";
ptr = ptr -> getNext();
}
}
}
};

main()
{
Stack_Array sa;
Stack_LinkedList sl;
int d;
int choice;
while(1)
{
cout << "\n\n1. Push Stack Elements Using Array";
cout << "\n2. Push Stack Elements Using Linked List";
cout << "\n3. Pop Stack Elements Using Array";
cout << "\n4. Pop Stack Elements Using Linked List";
cout << "\n5. Exit Program";
cout << "\n\nEnter Your Choice : ";
cin >> choice;
switch(choice)
{
case 1:
for(int i=1; i<=5; i++)
{
cout << "\n\nEnter Element : ";
cin >> d;
sa.push(d);
}
break;
case 2:
cout << "\n\n How many Nodes You Want To Push : ";
cin >> choice;
for(int i=1; i<=choice; i++)
{
cout << "\n\nEnter Element : ";
cin >> d;
sl.push(d);
}
break;
case 3:
sa.pop();
break;
case 4:
sl.pop();
break;
case 5:
exit(0);
default:
cout << "\n\n*** Invalid Choice ***";
}
}
}

You might also like