Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stacks. COMP104 Slide 2 Stacks * A stack, S, is a data structure that supports: n push(x) make x the top element in stack S n Pop Remove the top item.

Similar presentations


Presentation on theme: "Stacks. COMP104 Slide 2 Stacks * A stack, S, is a data structure that supports: n push(x) make x the top element in stack S n Pop Remove the top item."— Presentation transcript:

1 Stacks

2 COMP104 Slide 2 Stacks * A stack, S, is a data structure that supports: n push(x) make x the top element in stack S n Pop Remove the top item from stack S (and return its value). * A stack has the LIFO –Last in First out –property. AA B A B C A B A B D A B A Push(S,A)Push(S,B)Push(S,C)Pop(S,C)Push(S,D)Pop(S,D)Pop(S,A)

3 COMP104 Slide 3 Stack Operations * stackIsEmpty() //determine whether a stack is empty * Push() //add new item to a stack and return a //boolean value to indicate if the //insertion is successful * Pop() //remove most recent pushed item from stack //and return a pointer to the item * searchStack() //get the top item from top of stack //without changing the stack content

4 COMP104 Slide 4 Stack Application * Recognition of “balanced braces”. * A sequence of braces }, { is balanced if n Each time a “}” is encountered it matches a previously encountered “{“ n When reaching the end of the string, every “{“ is matched. * Examples {{ } { }}{ } balanced { } { }}{ } not balanced

5 COMP104 Slide 5 A linked list implementation * A stack can be implemented as a special type of linked list where: n New items are always inserted to the head of the linked list n The remove operation always removes the first node of the linked list * Empty Stack * Push Stack 20 pTop newPtr 13 204575...

6 COMP104 Slide 6 A Linked list Implementation * PushStack //same as the insertion to the head of linked //lists. 20 pTop newPtr 13 20 4575... pNew->next = pTop; pTop = pNew;

7 COMP104 Slide 7 A Linked List Implementation * Pop Node //same as delete the first node from linked list pTop = pTop -> next; (to delete) pTop 20457585...

8 #include using namespace std; struct Node{ int data; Node* next; }; bool stackIsEmpty(Node* pTop){ return (pTop == NULL); } void pushStack(Node* &pTop, int item){ Node* pNew; pNew = new Node; if(pNew == NULL){ cout << "Failed at Memory Allocation" << endl; return; } pNew->data = item; pNew->next = pTop; pTop = pNew; }

9 void printStack(Node *pTop){ Node* pCur = pTop; while(pCur != NULL){ cout data << " "; pCur = pCur->next; } cout << endl; } Node* searchStack(Node *pTop, int item){ Node *pCur = pTop; while(pCur != NULL){ if(pCur->data == item) break; pCur = pCur->next; } return pCur; }

10 Node* popStack(Node* &pTop){ Node* pCur = NULL; if(!stackIsEmpty(pTop)){ pCur = pTop; pTop = pTop -> next; } return pCur; }

11 void main(){ Node *pTop = NULL; pushStack(pTop, 3); pushStack(pTop, 1); pushStack(pTop, -5); pushStack(pTop, 7); printStack(pTop); popStack(pTop); printStack(pTop); pushStack(pTop, 16); pushStack(pTop, -51); pushStack(pTop, 27); printStack(pTop); popStack(pTop); printStack(pTop); } l Result is 7 -5 1 3 1 3 27 -51 16 1 3 16 1 3


Download ppt "Stacks. COMP104 Slide 2 Stacks * A stack, S, is a data structure that supports: n push(x) make x the top element in stack S n Pop Remove the top item."

Similar presentations


Ads by Google