Mark Allen Weiss, Addison Wesley

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Stacks, Queues, and Linked Lists
CS201: Data Structures and Discrete Mathematics I Linked Lists, Stacks and Queues.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
CS Data Structures ( 資料結構 ) Chapter 3: Stacks and Queues Spring 2012.
Rossella Lau Lecture 12, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 12: Stack and Expression Evaluation  Stack basic.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
 Balancing Symbols 3. Applications
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Implementing and Using Stacks
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Objectives of these slides:
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
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.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
CHP-3 STACKS.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Data Structures & Algorithm Stack Lecturer : Kritawan Siriboon, Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss,
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Stacks Stacks.
Revised based on textbook author’s notes.
CS 201 Data Structures and Algorithms
Objectives In this lesson, you will learn to: Define stacks
Stacks.
Stack and Queue APURBO DATTA.
CSC 172 DATA STRUCTURES.
Stacks Stack: restricted variant of list
Stacks Chapter 4.
Data Structures – Week #3
Algorithms and Data Structures
Stack and Queue.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks and Queues.
Stack and Queues Stack implementation using Array
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
Data Structures and Algorithms for Information Processing
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
CE 221 Data Structures and Algorithms
Stacks and Queues CSE 373 Data Structures.
Stack.
Data Structures – Week #3
CSE 373 Data Structures Lecture 6
Stacks and Queues.
LINEAR DATA STRUCTURES
Lecture 9: Stack and Queue
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

Mark Allen Weiss, Addison Wesley Stack & Queue Lecturers : Boontee Kruatrachue Room no. 913 Kritawan Siriboon Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss, Addison Wesley

Stack Lecturers : Boontee Kruatrachue Room no. 913 Kritawan Siriboon Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss, Addison Wesley

push pop top Stack LIFO List ( LastInFirstOut ) An ordered collection of items. One end is called a top of the stack. Items are inserted onto the top of the stack. => push Items are deleted from the top of the stack . => pop

Stack Implementations Array Implementation Linked Stack

Array Implementation An array. 2 1 How can we make a stack? What is it? An ordered collection of items. What data type can we use? What can keep many items? What can make items in ordered? An array. 2 1 Now, we have place for items of a stack. Next, can stack operations be done?

Stack Top With array, can we do stack operations, push, pop,...? Where can we push in the array ? At index 3. Why? We push on the top. We need a data structure for top. Where is the top element in each picture? At index 2,3. int top; What type of data can be top -> index ? 2 1 2 1 3

top= Stack Data Structure typedef char T; #define SIZE 6 //constant SIZE T item[SIZE]; int top; 5432 1 top= C B A item A B C 1 2 3 4 5 top 2

Stack Operations

What if the array is full ? push( ) What if the array is full ? Check Overflow pushing variable i ++ top; item[top] = i; item[++top] = i; push D D C B A 2 1 top = 3 top = 2 1 C B A item item

What if the array is empty ? pop( ) What if the array is empty ? Check Underflow int oldTop = top; top --; return item[oldTop]; return item[top--]; pop Need to erase D? D D C B A 2 1 D C B A 3 2 1 No. Only change top. top=3 oldTop= top=

Try pop until empty top = 2 1 C B A top = 2 1 C B A isEmpty( ) What happens when stack empty? Try pop until empty return (top==-1); top = 2 1 C B A top = 2 1 C B A top = -1

2 2 1 1 top = A init( ) Before pushing, a stack is empty. top = -1 2 1 test : push(A) top = A ++ top; item[top] = ‘A’;

top = 3 2 D 1 C B A isFull( ) What happens when stack full? D C B A return (top==SIZE-1); top = SIZE-1

Stack Implementations Array Implementation Linked Stack

Linked Stack top Check if it support every operations. pop ? push ?

Queue Lecturers : Boontee Kruatrachue Room no. 913 Kritawan Siriboon Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss, Addison Wesley

แถวคอย Queue FIFO List FirstInFirstOut front / head rear/tail deQueue (delete) enQueue (insert) An ordered collection of items. There are 2 ends, head (front) and tail (rear). Items are inserted to the tail (rear). => enQueue Items are deleted from the head (front). => deQueue

Queue Implementations Linked Queue Array Implementation

Linked Queue How do they link? Support every operations ? enQueue ? front rear front rear How do they link? Support every operations ? front rear enQueue ? (insert) deQueue ? (delete) Every operations ?

Queue Implementations Linked Queue Array Implementation

Array Implementation C D E A B C D E 1 f r 4 = r Straight array f = 2 f = 2 3 4 = r Circular array 0 1 2 3 4 A B C D E f r Not full. But cannot enqueue ! No place left at the rear side ! Can we enqueue at the other side ? Think as an array is a circle.

Array Implementation C D E void increase(int &r) void increase(int &i) #define SIZE 5 C D E 1 f = 2 3 4 = r r++ does not work ! void increase(int &r) if (i == (SIZE -1)) i = 0; else i++; void increase(int &i) if (r == (SIZE -1)) r = 0; else r++; Also f++ does not work !

isEmpty( ) A B E r f f r f f f f = 1 r = 1 r = 4 4 2 f = 2 3 3 0 1 2 3 4 0 1 2 3 4 A B E f r f f r f f 1 3 4 2 r = f = 1 2 3 4 r = f = isEmpty( ): can’t just check r>f ? or f>r ? It’s circular. Same as isFull( ).

isFull( ) A B C D E A F B C D E F B C D E A B E C D f r r f f r 1 3 4 0 1 2 3 4 0 1 2 3 4 A B C D E A F B C D E f r r f f r 1 3 4 2 r = f = F B C D E 1 2 3 4 r = f = A B E C D isFull( ), also can’t just check r>f ? or f>r ?

isEmpty(), isFull( ) ? A B C D E A B E C D f r r f 1 2 3 4 r = f = 1 2 0 1 2 3 4 0 1 2 3 4 A B C D E f r r f 1 2 3 4 r = f = 1 2 3 4 r = f = A B E C D isEmpty() & isFull( ), has the same condition ie. increase(r) makes it equal front. How to check isEmpty() or isFull()?

isEmpty( ) / isFull( ) Solution #define MAX 50 typedef int T; struct queue{ T data[MAX]; int front, rear; int count; }; enQ deQ init isFull isEmpty count++ count-- count = 0 count == SIZE count == 0

Queue Operations enQ isFull deQ isEmpty init

Algorithms

Evaluate Postfix Expression Stack Applications Parenthesis Matching Evaluate Postfix Expression Infix to Postfix Conversion (Reverse Polish Notation) Function Call (clearly see in recursion)

Are the parenthesises correct? (a+b-c)*[d+e]/{f*(g+h)} Parenthesis Matching Are the parenthesises correct? (a+b-c)*[d+e]/{f*(g+h)} [(a+b-c}*[d+e]/{f*(g+h)} (a+b-c *[d+e]/{f*(g+h) }

[ (a+b)*{ (d+e)-3}] Parenthesis Matching Match ? Count : [ { ( ] } ) ? No. We can do it since we were young. How ?

[ (a+b)*{ (d+e)-3}] Parenthesis Matching Simulate what you do. random How to program this algorithm ? random What data structures are needed ? [ (a+b)*{ (d+e)-3}] / / / / / / / / Difficult ! ↑ ↑ Random position (until found match?). Scan out both ways. if Left side = open paren & Right side = close paren. If match : Check out. goto 2 else end process : Not match. else goto 1

[ (a+b)*{ (d+e)-3}] [ ( ) { ( ) }] [ ( ) { ( ) }] Parenthesis Matching Clear out what are irrelevant. [ ( ) { ( ) }] [ ( ) { ( ) }] Parenthesis matching’s nature is a stack. How ? The close parenthesis matches the previous open parenthesis.

[ (a+b)*{ (d+e)-3}] Parenthesis Matching ↑ ( { ( [ Scan left to right. Open paren : push to a stack. Close paren : check if match ? with top in stack. match pop out. else done : not match !

Design Tools + + Code if (i == (SIZE -1)) Pseudocode i = 0; else i++; if (i is at the last item) i = 0; else i++; Warnier – Orr Diagram i is last item i = 0 i is last item i++ + + Or if ......... then A not A

Warnier-Orr Diagram Design Tool Init empty stack s [ (a+b)*{ (d+e)-3}] read ch error=false ch = non_paren Scan (not EOF && not error) ch = open paren + ch = close paren s.push(ch) s.empty() + error =true(no match-open-paren) open = s.pop() Paren matching match(open,ch) notmatch(open,ch) error = true (missmatch) error + no match-open-paren / missmatch s.empty() + MATCH ( MISSMATCH open paren exceed [

Warnier-Orr Diagram Design Tool Init empty stack s read ch error=false Scan (until EOF || error) ch = open paren + ch = close paren s.push(ch) s.empty() + error =true(no match-open-paren) + open = s.pop() Paren matching notmatch(open,ch) error = true (missmatch) error + no match-open-paren / missmatch + s.empty() + MATCH + MISSMATCH open paren exceed

Evaluate Postfix Expression Stack Applications Parenthesis Matching Evaluate Postfix Expression Infix to Postfix Conversion (Reverse Polish Notation) Function Call (clearly see in recursion)

Evaluate Postfix Notation Infix Form Prefix Form Postfix Form (Polish notation) a + b + a b a b + a + b * c + a * b c a b c * +

Evaluate Postfix Notation Evaluate Postfix Notation’s nature is a stack. 6 5 2 3 + 8 * - 3 + * = ? 3 input: 6523 input: + Push : 6, 5, 2, 3 o2 <- pop#1 o1 <- pop#2 push S 5 6 S 2 5 + 6 5 What ‘s next ?

6 5 2 3 + 8 * - 3 + * = ? input: 6523 Push : 6 5 2 3 3 2 5 6 s input: + pop#1 → 3 pop#2 → 2 5 push 2+3 6 s input: 8 8 push 8 5 6 input: * pop#1 → 8 40 pop#2 → 5 5 push 5*8 6 input: - pop#1 → 40 pop#2 → 5 -35 push 5-40 6 input: 3 push 3 3 -35 6 input: + pop#1 → 3 pop#2 → -35 -32 push -35+3 6 input: * pop#1 → -32 pop#2 → 6 push 6 * -32 - 192

Evaluate Postfix Expression Stack Applications Parenthesis Matching Evaluate Postfix Expression Infix to Postfix Conversion (Reverse Polish Notation) Function Call (clearly see in recursion)

Infix to Postfix Conversion a * b + c ab*c+ a*b+c ===> Notice : output: operands’ order is the same as input’s. output stack

Infix to Postfix Conversion a*b+c ---- > input: stack: output: a*b+c a a*b+c * a a*b+c * ab a*b+c + ab* a*b+c + ab*c a*b+c ab*c+

Infix to Postfix Conversion a+b*c ---- > input: stack: output: a+b*c a a+b*c + a a+b*c + ab * a+b*c + abc a+b*c abc*+

Infix to Postfix Conversion abc*+d- a+b*c-d => input: stack: output: a+b*c-d a a+b*c-d + a a+b*c-d + ab * a+b*c-d + abc a+b*c-d - abc*+ a+b*c-d - abc*+d a+b*c-d abc*+d-

Infix to Postfix Conversion abc*+de/f+g*- a+b*c-(d/e+f)*g => a+b*c-(d/e+f)*g + ab * a+b*c-(d/e+f)*g + abc a+b*c-(d/e+f)*g - abc*+ ( a+b*c-(d/e+f)*g - abc*+d / a+b*c-(d/e+f)*g - abc*+de

Infix to Postfix Conversion (cont.) a+b*c-(d/e+f)*g => a+b*c-(d/e+f)*g - abc*+de + a+b*c-(d/e+f)*g - abc*+de/f a+b*c-(d/e+f)*g - abc*+de/f+ * a+b*c-(d/e+f)*g - abc*+de/f+g a+b*c-(d/e+f)*g - abc*+de/f+g*-

Evaluate Postfix Expression Stack Applications Parenthesis Matching Evaluate Postfix Expression Infix to Postfix Conversion (Reverse Polish Notation) Function Call (clearly see more in recursion)

Stack of Function Call x 3 p i 8 3 5 3 k 8 *p 9 10 3 int i = 5; int i = k; pass by reference is tecnically the same as pass address but for writing convenience (no dereference) int& ref = k; ref is another name of k void f() { int k = 8; sth(k); sth( 5); Add1(&k); Ad1(k); } void sth(int i){ int x = 3; i = x; } int *p = &k; //pass data void Add1(int *p) { (*p)++; } //pass address //pass by reference int& ref = k; void Ad1(int& ref){ ref++; } x 3 3 Add1( ) p Stack top i 8 3 5 3 Stack frame Ad1( ) sth( ) Stack top k f( ) 8 *p Stack frame ref 9 10

Struct Stack #define SIZE 6 typedef int T; typedef struct stack { T item[SIZE]; int top; } stack; #define SIZE 6 typedef int T; void push (stack *sp, T d){ if (!isFull(*sp)) sp->item[++sp->top] = d; } void push (int item[], int *pTop, T d ){ (*pTop)++; item[*pTop] = d; } int main() { stack s, s1; init(&s); push(&s, 5); int main(){ T item[SIZE], item2[SIZE]; int top, top2; init(&top); push( item, &top,5);

Lab Questions

Common Misunderstanding int *p = &i; int *p; p = &i; *p = &i; int *q = p; int *q; q = p; q p i 8 q points to what p points to 59 59 s and u are pointers 59 int *s, t, *u; int *s; int t; int *u; t is int

Lab : Scramble List

h I t1 love h2 you Opps! very much t2 C++ Bottom Up Lift it. Try to print h2. love Where ? h2 you Take the bottom up. Opps! very much t2 C++

Riffle Shuffle h1 h2 h1 h2 4 1 4 1 5 2 5 2 t2 t2 6 3 6 3 7 7 8 8 t1 t1 Riffle Shuffle each node of each packet from the top. put the rest at the back of the result packet. 8 8 t1 t1 9 9 Lift up to 2 packets.

Riffle Shuffle Riffle Shuffle each node of each packet from the top. 4 1 4 1 5 5 2 2 t2 t2 6 6 3 3 7 7 Riffle Shuffle each node of each packet from the top. put the rest at the back of the result packet. 8 8 t1 t1 9 9 Lift up to 2 packets.

FILE *fp = fopen("D:/zfile.txt","w"); fprintf(fp, "369"); 369 fclose(fp); fp = fopen("D:/zfile.txt", "a"); 369 EOF fprintf(fp, "Append"); 369Append EOF fclose(fp); fp = fopen("D:/zfile.txt", "r"); 369Append EOF int i; fscanf(fp,"%d",&i); i 369 369Append EOF char c = fgetc(fp); c A 369Append EOF

http://www.tutorialspoint.com/cprogramming/c_file_io.htm FILE * fopen( const char * filename, const char * mode ); int fputc( int c, FILE *fp ); int fputs( const char *s, FILE *fp ); int fprintf(FILE *fp,const char *format, ...); int fgetc( FILE * fp ); char *fgets( char *buf, int n, FILE *fp ); int fscanf(FILE *fp, const char *format, ...);

C string H e l o W o r l d char s[12]={'H','e','l','l','o','\0'}; char s1[]="World"; printf("%s%s\n",s,s1); //output:HelloWorld S H e l o \0 1 2 3 4 5 6 7 8 9 10 11 S1 W o r l d \0 1 2 3 4 5