220 likes | 499 Views
Data Structures. Stack Namiq Sultan. Data Structure. Definition: Data structures is a study of different methods of organizing the data and possible operations on these structures. Stack Queue Linked list Trees. Stack. push. pop.
E N D
Data Structures Stack Namiq Sultan
Data Structure • Definition: Data structures is a study of different methods of organizing the data and possible operations on these structures. • Stack • Queue • Linked list • Trees
Stack push pop • A stack is an ordered collection of items inot which new items may be added and from which items may be deleted at one end, called the top of the stack. • Stack operations push pop This stack is called last-in first-out stack LIFO.
Stack Representing stacks constint SIZE = 8; struct stack{ int item[SIZE}; int top; }; stack s; s.top = -1; // initialization s.item[7] s s.item[0] s.top
Stack Push and pop algorithms Push x if stack is full then overflow error else add x onto a stack end Pop if stack is empty then underflow error else remove item from a stack end
Stack Programming push operation void push(stack &st, int x) { if(st.top == SIZE-1) exit(1); else st.items[++st.top] = x; } Function call push(s, 4); push(s, 33); 7 6 5 ? 4 ? 3 ? 2 ? 1 ? 33 0 ? 4 -1 0 1
Stack Programming pop operation int pop(stack &st) { if(st.top == -1) exit(1); else return st.items[st.top--]; } Function call y = pop(s); n = pop(s); 12 8 -5 33 4 4 3 2
Program 3.1 // stack implementation using array #include <iostream.h> const SIZE = 10; // Length of the stack struct stack{ int items[SIZE]; int top; }; void push(stack &, int); //prototype for push int pop(stack &); //prototype for pop void main () { stack stk; int item, value; int s; stk.top = -1; // intializing top to -1
do{ cout<<"select : \n 0 : exit\n 1 : push \n 2 : pop\n >>>>>"; cin >> s; //taking input switch(s){ case 1://if PUSH cout<<"\n Enter item to push : "; cin>>item; push(stk, item); break; case 2: //if POP value = pop(stk); cout << value <<‘\n’; } }while (s != 0 ); }
void push(stack &st, int x) { if (st.top == SIZE-1){ // if overflow cout << “Stack is full”; } st.items[++st.top]=x; //insert the specified value into the stack array } int pop(stack &st) { if (st.top == -1) { // if empty cout<<“Stack is empty”; } returnst.items[st.top--]; //return the popped value }
Program 3.2 // stack implementation using array and object-oriented programming #include <iostream.h> const MAX = 100; class STACK{ int items[MAX]; int top; public: STACK() //constructor initializes top to -1 { top= -1; } void push(); void pop(); void traverse(); };
void STACK::push() // add an element to the stack { int item; // if STACK is full then overflow if (top == MAX-1) cout<<"\nThe Stack Is Full"; else { cout<<"\nEnter The Element = "; cin>>item; items[++top]=item; } }
void STACK::pop() //delete an element from the stack { int item; if (top == -1) cout<<"\nThe Stack Is Empty"; else { item=items[top--]; cout<<"\nThe Deleted Element Is = "<<item; } }
//This function to print all the existing elements in the stack void STACK::traverse() { inti; if (top == -1) cout<<"\nThe Stack is Empty"; else { cout<<"\n\nThe Elements In The Stack are..."; for(i=top; i>=0; i--) cout<<"\n "<<items[i]; } }
void main() { int choice; charch; STACK ps; do{ //A menu for the stack operations cout<<"\n1. PUSH"; cout<<"\n2. POP"; cout<<"\n3. TRAVERSE"; cout<<"\nEnter Your Choice = "; cin>>choice; switch(choice){ case 1: ps.push(); break;
case 2: ps.pop(); break; case 3: ps.traverse(); break; default: cout<<"\nYou Entered Wrong Choice" ; } cout<< "\n\nPress (Y/y) To Continue = "; cin>> ch; }while(ch == 'Y' || ch == 'y'); }
Stacks, cont. • More operations, general to most list data structures: • Create - Creates & initializes a new stack • Destroy - Destroys a stack, freeing up storage • Full - Function returning TRUE if stack is full • Empty - Function returning TRUE if stack has no elements • Clear - Removes all elements from stack • Size - Function returning number of elements in the stack
APPLICATIONS OF STACKS • Stack is internally used by compiler when we implement (or execute) any recursive function. • Stack is also used to evaluate a mathematical expression and to check the parentheses in an expression.
Evaluating Postfix Expression opndStk = the empty stack // scan the input string reading one element at a time into symb While(not end of input){ symb = next input character if(symb is an operand) push(opndStk, symb) else{ // symb is an operator opnd2 = pop(opndStk) opnd1 = pop(opndStk) value = result of applying symb to opnd1 and opnd2 push(opndStk, value) } // end else } // end while Return (pop(opndStk))
Example: Postfix Expression • Infix to postfix conversion: a+b*c-d a+(b*c)-d a+(bc*)-d (abc*+)-d ab*+d- • The only rules to remember during the conversion are that operations with highest precedence are converted first and that after a portion of the expression has been converted to postfix it is to be treated as a single operand. • Postfix expression requires no paranthesis
Example: Brace matching • Another use of stack is in compiler design, the need to match opened braces with closed braces. braceStk = the empty stack While(not end of input){ symb = next input character if symb is ‘(‘ push(braceStk, symb) if symb is ‘)’ if empty(braceStk) then error else x = pop(braceStk) } //end while If(!empty(braceStk)) error Else output(“Brace match”)