0% found this document useful (0 votes)
6 views11 pages

Stack Operations in C++: Arrays & Lists

Uploaded by

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

Stack Operations in C++: Arrays & Lists

Uploaded by

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

STACKS

1. Write a program to implement basic operations of stack using


arrays.

#include <iostream.h>
#include <conio.h>
#de ne MAX 12
class Stack
{
int stack[MAX];
int top;
public:
Stack() { top = -1; }
void push(int item)
{
if (top >= MAX - 1)
{
cout << "Stack Over ow!\n";
return;
}
stack[++top] = item;
cout << "Pushed " << item << " onto stack.\n";
}
void pop()
{
if (top < 0)
{
cout << "Stack Under ow!\n";
return;
}
cout << "Popped " << stack[top--] << " from stack.\n";
}
void display()
{
if (top < 0)
{
fi
fl
fl
cout << "Stack is empty!\n";
return;
}
cout << "Stack elements: ";
for (int i = 0; i <= top; i++)
cout << stack[i] << " ";
cout << "\n";
}
};
void main()
{
clrscr();
Stack s;
int choice, item;
do
{
cout << "1. Push\n2. Pop\n3. Display\n4. Exit\nChoice: ";
cin >> choice;
ush(stdin); // <--- Added this line to clear input bu er
switch(choice)
{
case 1:
cout << "Enter value: ";
cin >> item;
ush(stdin); // <--- Added this line here as well
[Link](item);
break;
case 2: [Link](); break;
case 3: [Link](); break;
case 4: cout << "Exiting...\n"; break;
default: cout << "Invalid choice!\n";
}
}
while (choice != 4);
getch();
}
ffl
ffl
ff
Output

1. Push
2. Pop
3. Display
4. Exit
Choice: 1
Enter value: 10
Pushed 10 onto stack.
1. Push
2. Pop
3. Display
4. Exit
Choice: 1
Enter value: 20
Pushed 20 onto stack.
1. Push
2. Pop
3. Display
4. Exit
Choice: 3
Stack elements: 10 20
1. Push
2. Pop
3. Display
4. Exit
Choice: 2
Popped 20 from stack.
1. Push
2. Pop
3. Display
4. Exit
Choice: 3
Stack elements: 10
1. Push
2. Pop
3. Display
4. Exit
Choice: 4
Exiting...
2. Write a program to implement basic operations of stack using linked
lists.

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

struct Node
{
int data;
Node* next;
};
class Stack
{
Node* top;
public:
Stack()
{
top = NULL;
}

void push(int item)


{
Node* temp = (Node*)malloc(sizeof(Node));
if (temp == NULL)
{
cout << "Heap Over ow!\n";
return;
}
temp->data = item;
temp->next = top;
top = temp;
cout << "Pushed " << item << " onto stack.\n";
}
void pop()
{
if (top == NULL)
fl
{
cout << "Stack Under ow!\n";
return;
}
Node* temp = top;
cout << "Popped " << temp->data << " from stack.\n";
top = top->next;
free(temp);
}

void display()
{
if (top == NULL)
{
cout << "Stack is empty!\n";
return;
}
cout << "Stack elements: ";
Node* temp = top;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << "\n";
}
};

void main()
{
clrscr();
Stack s;
int choice, item;
do
{
cout << "1. Push\n2. Pop\n3. Display\n4. Exit\nChoice: ";
fl
cin >> choice;
ush(stdin); // Clear input bu er after reading choice

switch (choice)
{
case 1:
cout << "Enter value to push: ";
cin >> item;
ush(stdin); // Clear input bu er after reading item
[Link](item);
break;
case 2:
[Link]();
break;
case 3:
[Link]();
break;
case 4:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice!\n";
}
}
while (choice != 4);
getch();
}
ffl
ffl
ff
ff
Output

1. Push
2. Pop
3. Display
4. Exit
Choice: 1
Enter value to push: 10
Pushed 10 onto stack.
1. Push
2. Pop
3. Display
4. Exit
Choice: 1
Enter value to push: 20
Pushed 20 onto stack.
1. Push
2. Pop
3. Display
4. Exit
Choice: 3
Stack elements: 20 10
1. Push
2. Pop
3. Display
4. Exit
Choice: 2
Popped 20 from stack.
1. Push
2. Pop
3. Display
4. Exit
Choice: 3
Stack elements: 10
1. Push
2. Pop
3. Display
4. Exit
Choice: 4
Exiting...
3. Write a program to convert in x expression into equivalent post x
expression.

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

#de ne MAX 100

class Stack
{
char stack[MAX];
int top;
public:
Stack()
{
top = -1;
}
void push(char c)
{
if(top < MAX-1)
stack[++top] = c;
}
char pop()
{
if(top >= 0)
return stack[top--];
return '\0';
}
char peek()
{
if(top >= 0)
return stack[top];
return '\0';
}
fi
fi
fi
int isEmpty()
{
return (top == -1);
}
int precedence(char op)
{
if(op == '+' || op == '-') return 1;
if(op == '*' || op == '/') return 2;
return 0;
}
};
void in xToPost x(char in x[], char post x[])
{
Stack s;
int k = 0;
for(int i=0; in x[i] != '\0'; i++)
{
char ch = in x[i];
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <=
'9'))
{
post x[k++] = ch;
}
else if(ch == '(')
{
[Link](ch);
}
else if(ch == ')')
{
while(![Link]() && [Link]() != '(')
{
post x[k++] = [Link]();
}
if(![Link]()) [Link](); // remove '('
}
else
fi
fi
fi
fi
fi
fi
fi
fi
{
while(![Link]() && [Link]([Link]()) >= [Link](ch))
{
post x[k++] = [Link]();
}
[Link](ch);
}
}

while(![Link]())
{
post x[k++] = [Link]();
}
post x[k] = '\0';
}

void main()
{
clrscr();
char in x[MAX], post x[MAX];
cout << "Enter in x expression: ";
fgets(in x, MAX, stdin);
in xToPost x(in x, post x);
cout << "Post x expression: " << post x << "\n";
getch();
}

Output

Enter in x expression: a+b*c


Post x expression: abc*+
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
4. Write a program to calculate factorial using recursion to
demonstrate the use of stacks.

#include <iostream.h>
#include <conio.h>

int factorial(int n)
{
int stack[100], top = -1;
int result = 1;
for (int i = n; i > 0; i--)
{
stack[++top] = i;
}
while (top != -1)
{
result *= stack[top--];
}
return result;
}

void main()
{
int number;
clrscr();
cout << "Enter a non-negative integer: ";
cin >> number;
if (number < 0)
{
cout << "Factorial is not de ned for negative numbers." << endl;
}
else
{
cout << "Factorial of " << number << " is " << factorial(number) << endl;
}
getch();
}

Output

Enter a non-negative integer: 5


Factorial of 5 is 120
fi

You might also like