Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.

Similar presentations


Presentation on theme: "Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations."— Presentation transcript:

1 Data Structures Stack Namiq Sultan 1

2 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 2

3 Stack 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. pushpop 3

4 Representing stacks const int SIZE = 8; struct stack{ int item[SIZE}; int top; }; stack s; s.top = -1; // initialization Stack s s.item[0] s.item[7] s.top 4

5 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 5

6 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); Stack ? ? ? ? ? ? 0 1 3 4 5 6 7 2 0 4 33 1 6

7 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); Stack 4 4 33 -5 8 12 32 7

8 // stack implementation using array #include using namespace std; 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 Program 3.1 8

9 do{ cout >>>>"; 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 ); } 9

10 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”; } return st.items[st.top--]; //return the popped value } 10

11 // stack implementation using array and object-oriented programming #include using namespace std; 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(); }; Program 3.2 11

12 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; } 12

13 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; } 13

14 //This function to print all the existing elements in the stack void STACK::traverse() { int i; 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]; } 14

15 void main() { int choice; char ch; 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; 15

16 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'); } 16

17 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 17

18 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. 18

19 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)) 19

20 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 20

21 Example: Evaluate 452*+8-32^+ opndStkvalueopnd2opnd1symb 4 4,5 4,5,2 4,10 14 14,8 6 6,3 6,3,2 6,9 15 10 14 6 9 15 2 10 8 2 9 5 4 14 3 6 452*+8-32^+452*+8-32^+ 21

22 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”) 22


Download ppt "Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations."

Similar presentations


Ads by Google