Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.

Similar presentations


Presentation on theme: "Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted."— Presentation transcript:

1 Data Structures

2 The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted called the TOP of the stack A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted at one end called the TOP of the stack

3 What is a Stack A common data structure in computing. A common data structure in computing. Data items are "popped" and "pushed" (retrieved and stored) from the top of the stack. Data items are "popped" and "pushed" (retrieved and stored) from the top of the stack. Stacks normally have a maximum size. It is an error to push items onto a full stack, or pop items off an empty stack. Stacks normally have a maximum size. It is an error to push items onto a full stack, or pop items off an empty stack. LIFO: Last In First Out LIFO: Last In First Out

4 Stack features ORDERING: maintains order when elements added (new elements are added to the end by default) ORDERING: maintains order when elements added (new elements are added to the end by default) DUPLICATES: yes (allowed) DUPLICATES: yes (allowed) OPERATIONS: OPERATIONS:  add element to end of list ('push')  remove element from end of list ('pop')  isEmpty()  isFull()  Top()

5 Stacks in computer science the stack is one of the most important data structures in all of computer science the stack is one of the most important data structures in all of computer science  compilers use stacks to evaluate expressions  stacks are great for reversing things, matching up related pairs of things, and backtracking algorithms  stack programming problems:  reverse letters in a string, reverse words in a line, or reverse a list of numbers  examine a file to see if its braces { } and other operators match  convert infix expressions to postfix or prefix

6 Another implementation of Stack Stacks can be declared by using structures containing two objects Stacks can be declared by using structures containing two objects #define STACKSIZE 100 struct stack { int top; int items[STACKSIZE]; }; struct stack s;

7 empty() If (s.top==-1)  Stack is empty Else stack is not empty int empty(struct stack *ps) { if (ps->top == -1) return(TRUE);elsereturn(FALSE);} if (empty(&s)) if (empty(&s)) s tack is empty s tack is emptyelse stack is not empty stack is not empty

8 Push() void push(struct stack *ps, int x) { ps->items[++(ps->top)] = x; { ps->items[++(ps->top)] = x;} void push(struct stack *ps, int x) { if (ps->top==STACKSIZE-1) { if (ps->top==STACKSIZE-1){ cout<< “ stack overflow ” ; exit();} ps->items[++(ps->top)] = x; ps->items[++(ps->top)] = x;return;}

9 POP( ) int pop(struct stack *ps) { if (empty(ps)) { cout<<stack underflow; exit(1); } return (ps->items[ps->top--]); } To use the pop function, the programmer can declare int x and write x=pop(&s);


Download ppt "Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted."

Similar presentations


Ads by Google