Docsity

CSE Stack operation PPT, Assignments of Computer Science

Partial preview of the text

Download CSE Stack operation PPT and more Assignments Computer Science in PDF only on Docsity!

vaRIOUS

OPERATIONS

ON STACK

CONTAINER

vaRIOUS

OPERATIONS

ON STACK

CONTAINER

PRESENTED BY:

• AMAN PANDEY (20BCE10615)
• SAGAR SUMAN (20BCE10588)
• DAKSHH SINGH (20BCE10711)
• ASHUTOSH MANDLOI (20BCE10546)

OPERATIONS ON

STACK

A stack is a linear data structure. It is controlled by two operations : push and pop. Both the operations take place from one end of the stack, usually called top. Top points to the current top positions of the stack. Push operation adds an element to the top of the stack. Pop operation removes an element from the top of the stack. These two operations implements the LIFO method.

IMPLEMENTATION

OF STACK

Stack can be implemented in two ways :

  • (^) As an Array
  • (^) As a Linked List

PUSH OPERATION

 (^) Initially when the stack is empty, TOP can have any integer value other than any valid index number of the array. Let TOP = -1;  (^) The first element in the empty stack goes to the 0th position of the array and the Top is initialized to value 0.  (^) After this every push operation increase the TOP by 1 and inserts new data at that particular position.  (^) As arrays are fixed in length, elements can not be inserted beyond the maximum size of the array. Pushing data beyond the maximum size of the stack results in “data overflow”.

PUSH OPERATION

POP OPERATION

PROGRAM TO ILLUSTRATE OPERATIONS ON STACK AS AN INTEGER ARRAY case 3: { display(); break; } c } case 4: { cout<<"Exit"<<endl; break; } default: { cout<<"Invalid Choice"<<endl; } }

  • (^) }while(ch!=4);
  • (^) return 0;
  • } #include using namespace std; int stack[100], n=100, top=-1; void push(int val) { if(top>=n-1) cout<<"Stack Overflow"<<endl; else { top++; stack[top]=val; } } void pop() { if(top<=-1) cout<<"Stack Underflow"<<endl; else { cout<<"The popped element is "<< stack[top] <<endl; top--; } } void display() { if(top>=0) { cout<<"Stack elements are:"; for(int i=top; i>=0; i--) cout<<stack[i]<<" "; cout<<endl; } else cout<<"Stack is empty"; } int main() { int ch, val; cout<<"1) Push in stack"<<endl; cout<<"2) Pop from stack"<<endl; cout<<"3) Display stack"<<endl; cout<<"4) Exit"<<endl; do { cout<<"Enter choice: "<<endl; cin>>ch; switch(ch) { case 1: { cout<<"Enter value to be pushed:"<<endl; cin>>val; push(val); break; } case 2: { pop(); break; } case 3: { display(); break; } c
  • }

Infix To postfix Conversion

  • (^) Infix expressions are readable and solvable by humans. We can easily distinguish the order of operators, and also can use the parenthesis to solve that part first during solving mathematical expressions. The computer cannot differentiate the operators and parenthesis easily, that’s why postfix conversion is needed.
  • (^) Example of infix to postfix: a+b(infix) its equivalent postfix is ab+.

Stacks usage in infix to postfix conversion

  • (^) To convert infix expression to postfix expression, we will use the stack data structure. By scanning the infix expression from left to right, when we will get any operand, simply add them to the postfix form, and for the operator and parenthesis, add them in the stack maintaining the precedence of them.

STACK AS A LINKED LISTSTACK AS A LINKED LIST Linked list implementation of stack uses  A linked list to store data  A pointer TOP pointing to the top most position of the stack. NOTE: Each node of a stack as a linked list has two parts : data part and link part and is created with the help of self referential structure. The data part stores the data and link part stores the address of the next node of the linked list.

PROGRAM TO ILLUSTRATE OPERATIONS ON STACK AS A LINKED LIST : Explanation