0% found this document useful (0 votes)
24 views

Assignment 01

Uploaded by

anas1054108
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Assignment 01

Uploaded by

anas1054108
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

UNIVERSITY OF

MANAGEMENT AND
TECHNOLOGY

SIALKOT CAMPUS

Student Name MUHAMMAD ANAS


Student ID 23018020083
Program BS-CS Batch 18 3 Sem
Course Title Data Structures
Resource Person
Activity
Due Date

MANUAL 01
QUESTION 01
/*
Implement a stack using an array in C++ with
the following functions:
• push (): Adds an item to the top of the
stack.
• pop (): Removes and returns the top item
from the stack.
• traverse (): Displays all items in the
stack.
• sort (): Sorts the stack in ascending or
descending order.
• search (): Searches for an item in the stack
and returns its index.
• minVal (): Displays Minimum value item in
stack.
• maxVal (): Displays Maximum value item in
stack.
*/

#include<iostream>
using namespace std;

int top;
class stack
{
private:
int arr[7];
public:
stack()
{
top=-1;
}
void traverse()
{
if(top==-1)
{
cout<<"stack is empty (stack
underflow)\n";
}
else
{
cout<<"displaying elements\n";

for(int i=0;i<=top;i++)
{
cout<<arr[i]<<" ";
}
}
}
void push()
{
if(top==6)
{
cout<<"stack is full (stack
overflow)\n";

}
else
{
cout<<"enter element to insert \n";
int element;
cin>>element;
top=top+1;
arr[top]=element;
cout<<"element added successfully \
n";
}

}
void pop()
{
if(top==-1)
{
cout<<"stack is empty (stack
underflow)\n";
}
else
{
top=top-1;
cout<<"element deleted
successfully\n";
}
}
void sort()
{
if(top==-1)
{
cout<<"stack is empty (stack
underflow)\n";
}
else
{
for (int i=0; i<top; i++)
{
for (int j=i+1; j<=top; j++)
{
if (arr[i] > arr[j])
{
swap(arr[i],arr[j]);
}
}
}
cout<<"stack sorted successfully\
n";
}
}
void search()
{
bool isfound=false;
cout<<"enter element to search \n";
int element;
cin>>element;
for(int i=0;i<=top;i++)
{
if(arr[i]==element)
{
cout<<" element found
successfully at position "<<i+1<<endl;
isfound=true;
}

}
if(isfound==false)
cout<<"element not found\n";

}
void minVal()
{
int min=arr[0];

for(int i=0;i<=top;i++)
{
if(min>arr[i])
min=arr[i];
}
cout<<"minimum value is "<<min;
}
void maxVal()
{
int max=arr[0];

for(int i=0;i<=top;i++)
{
if(max<arr[i])
max=arr[i];
}
cout<<"maximum value is "<<max;
}
};

int main()
{
stack a1;
while(true)
{
cout<<endl;
system("pause");
system("cls");
cout<<"\t----------------\n";
cout<<"\t| MAIN MENU |\n";
cout<<"\t----------------\n";
cout<<"\t|(1)Traverse |\n";
cout<<"\t|(2)Push |\n";
cout<<"\t|(3)Pop |\n";
cout<<"\t|(4)Search |\n";
cout<<"\t|(5)Sort |\n";
cout<<"\t|(6)minimum |\n";
cout<<"\t|(7)maximun |\n";
cout<<"\t|(8)exit |\n";
cout<<"\t----------------\n";
int choice;
cin>>choice;
switch (choice)
{
case 1:
{

a1.traverse();
break;
}
case 2:
{
a1.push();
break;
}
case 3:
{
a1.pop();
break;
}
case 4:
{
a1.search();
break;
}
case 5:
{
a1.sort();
break;
}
case 6:
{
a1.minVal();
break;
}
case 7:
{
a1.maxVal();
break;
}
case 8:
{
cout << "Exiting program.\n";
return 0;
break;
}
default:
cout << "Invalid choice!
Please try again.\n";
}
}
}
//status completed and tested

TRAVERSE PUSH

POP SEARCH
SORT MINIMUM

MAXIMUM TRAVERSING STACK


QUESTION 02
//q2
/* write a function to identify and display
any duplicate data in the stack along with
their indices */

#include<iostream>
using namespace std;

int top;

class stack
{
public:
int arr[10];

stack()
{
top=-1;
}
void push()
{
if(top==10)
{
cout<<"stack is full (stack
overflow)\n";
for(int i=0;i<=top;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
else
{
cout<<"enter element to insert \n";
int element;
cin>>element;
top=top+1;
arr[top]=element;
cout<<"element added successfully \
n";
}

}
void copy()
{
int duplicates = 0;
bool idex[top+1];

for (int i = 0; i <top; i++)


{

idex[i] = false;
}
for (int i = 0; i <= top; i++)
{
for (int j = 0; j < i; j++)
{
if (arr[i] == arr[j])
{
if ((!idex[j]))
{
cout << arr[j] << "
duplicated at index " << j << '\n';
idex[j] = true;
}
if ((!idex[i]))
{
cout << arr[i] << "
duplicated at index "<< i << '\n';
idex[i] = true;
}
duplicates++;
}
}
}

if (!duplicates)
cout << "No duplicates found!\n";
}
};
int main()
{
stack exp1;
while(true)
{
system("pause");
system("cls");
cout<<"\t----------------\n";
cout<<"\t| MAIN MENU |\n";
cout<<"\t----------------\n";
cout<<"\t|(1)push |\n";
cout<<"\t|any key to exit |\n";
int choice;
cin>>choice;
switch (choice)
{
case 1:
{
exp1.push();
break;
}
default:
goto a;
}
}
a:
cout<<" elements are : "<<endl;
for(int i=0;i<=top;i++)
{
cout<<exp1.arr[i]<<" ";
}
cout<<endl;
exp1.copy();
}

A TOTAL OF 9 ELEMENTS WERE ADDED AND THEN TERMINATED:


QUESTION 03
/*
Write a C++ program that:
• Takes input for elements of a stack named inputStack.
• Sorts these elements in ascending order.
• Stores the sorted elements in another stack named sortedStack
such that the
smallest elements are on the top of sortedStack.
• Display both stacks.
*/
#include<iostream>
using namespace std;
int top;

class stack
{
private:
int inputStack[7];
int sortedStack[7];
public:
stack()
{
top=-1;
}
void traverse()
{
if(top==-1)
{
cout<<"stack is empty (stack
underflow)\n";
}
else
{
cout<<"displaying elements of
input stack \n";
for(int i=0;i<=top;i++)
{
cout<<inputStack[i]<<" ";
}
cout<<endl;

}
void push()
{
if(top==6)
{
cout<<"stack is full (stack
overflow)\n";

}
else
{

cout<<"enter element to insert \n";


int element;
cin>>element;
top=top+1;
inputStack[top]=element;
cout<<"element added successfully \
n";
}

}
void sort()
{
if(top==-1)
{
cout<<"stack is empty (stack
underflow)\n";
}
else
{
for (int i=0; i<top; i++)
{
for (int j=i+1; j<=top; j++)
{
if (inputStack[i] >
inputStack[j])
{

swap(inputStack[i],inputStack[j]);
}
}
}
cout<<"stack sorted successfully\
n";
cout<<"displaying elements of
sorted stack \n";

for(int i=0;i<=top;i++)
{
cout<<inputStack[i]<<" ";
}
cout<<endl;
}
}
void store()
{
for(int i=0; i<=top ;i++)
{
sortedStack[i]=inputStack[top-
i];
}
cout<<"stack stored successfully in
another stack \n";
cout<<"displaying elements of new
stack \n";

for(int i=0;i<=top;i++)
{
cout<<sortedStack[i]<<" ";
}
cout<<endl;
}

};

int main()
{
stack s1;

while(true)
{
system("pause");
system("cls");
cout<<"\t-------------------\n";
cout<<"\t|(1)Push |\n";
cout<<"\t|any key to proeed |\n";

int ch;
cin>>ch;

switch (ch)
{
case 1:
{
s1.push();
break;
}
default:
goto a;
}

}
a:
s1.traverse();
s1.sort();
s1.store();

}
QUESTION 04
/*Write a C++ program that simulates three
real-world scenarios where stacks are useful.
Implement each scenario using stack
operations. */
#include <iostream>
#include <string>
using namespace std;
const int MAX = 100;
class Stack
{
public:
int top;
char arr[MAX];

Stack()
{
top = -1;
}

bool isFull()
{
return top == MAX - 1;
}

bool isEmpty()
{
return top == -1;
}

void push(char element)


{
if (isFull())
{
cout << "Stack Overflow" << endl;
return;
}
arr[++top] = element;
}

char pop()
{
if (isEmpty())
{
cout << "Stack Underflow" << endl;
return '\0';
}
return arr[top--];
}

char peek()
{
if (isEmpty())
{
cout << "Stack is Empty" << endl;
return '\0';
}
return arr[top];
}
};
void reverseWord(const string word)
{
Stack s;
for (int i=0;i<word.length(); i++)
{
char ch=word[i];
s.push(ch);
}

cout << "Reversed Word: ";


while (!s.isEmpty())
{
cout << s.pop();
}
cout << endl;
}

bool isBalanced(const string exp)


{
Stack s;
for (int i=0; i < exp.length(); i++)
{
char ch = exp[i];
if (ch == '(')
{
s.push(ch);
}
else if (ch == ')')
{
if (s.isEmpty())
{
return false;
}
s.pop();
}
}
return s.isEmpty();
}
void bagles()
{

Stack C;
char color;
while(true)
{
system("cls");
cout<<"enter your color combination \n";
cout<<"(R for red)\n";
cout<<"(G for green)\n";
cout<<"(B for blue)\n";
cout<<"(b for black)\n";
cout<<"(S for silver)\n";
cout<<"any other key to end wearing\n";
cin>>color;

switch(color)
{
case 'R':
{
C.push('R');
break;
}
case 'G':
{
C.push('G');
break;
}
case 'B':
{
C.push('B');
break;
}
case 'b':
{
C.push('b');
break;
}
case 'S':
{
C.push('S');
break;
}
default:
{
goto a;
break;
}

}
}
a:
cout<<"Displaying your color combination\
n";
for(int i=0;i<=C.top;i++)
{
if(C.arr[i]=='R')
{
cout<<"\033[31mO\033[0m"<<" ";
}
else if(C.arr[i]=='G')
{
cout<<"\033[32mO\033[0m"<<" ";
}
else if(C.arr[i]=='B')
{
cout<<"\033[34mO\033[0m"<<" ";
}
else if(C.arr[i]=='b')
{
cout<<"\033[30mO\033[0m"<<" ";
}
else
{
cout<<"\033[37mO\033[0m"<<" ";
}
}
cout<<endl;
system("pause");
system("cls");
}
int main()
{
string word;
while(true)
{
cout<<"\
t|-----------------------------------|\n";
cout<<"\t|(1) word reverse
|\n";
cout<<"\t|(2) paranthesis balance check
|\n";
cout<<"\t|(3) bangles color combination
|\n";

int ch;
cin>>ch;

switch (ch)
{

case 1:
{
cout<< "Enter a Word: ";
cin >> word;
reverseWord(word);
cout << endl;
break;
}
case 2:
{
cout<< "enter an Expression:
";
string expression;
cin >> expression;

if (isBalanced(expression)) {
cout << "Expression is
balanced" << endl;
}
else {
cout << "Expression is not
balanced" << endl;
}
break;
}
case 3:
{
bagles();
break;
}
default:
{
return 0;
}
}
}
}

You might also like