C3 stack queue

Download C3 stack queue

Post on 21-Jun-2015

990 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

<ul><li> 1. Chng 3.2: NGN XP HNG I (Stack - Queue) </li></ul> <p> 2. Chng 3: Ngn xp Hng i Ni dung Ngn xp (Stack) Hng i (Queue) 2 3. Chng 3: Ngn xp Hng i Ni dung 3 Ngn xp (Stack) Khi nim Stack Cc thao tc trn Stack Hin thc Stack ng dng ca Stack 4. Chng 3: Ngn xp Hng i Stack - Khi nim Stack l mt danh sch m cc i tng c thm vo v ly ra ch mt u ca danh sch V th, thao tc trn Stack c thc hin theo c ch LIFO (Last In First Out - Vo sau ra trc) 4 5. Chng 3: Ngn xp Hng i Stack Cc thao tc Stack h tr 2 thao tc chnh: Push: Thm 1 i tng vo Stack Pop: Ly 1 i tng ra khi Stack V d: 5 2 3 - - 4 Stack cng h tr mt s thao tc khc: isEmpty(): Kim tra xem Stack c rng khng Top(): Tr v gi tr ca phn t nm u Stack m khng hy n khi Stack. Nu Stack rng th li s xy ra 5 6. Chng 3: Ngn xp Hng i Hin thc Stack dng DSLK 6 Khai bo cc cu trc: struct Node { DataType data; Node *next; }; struct Stack { Node *top; }; 7. Chng 3: Ngn xp Hng i Hin thc Stack dng DSLK (tt.) Khi to Stack: 7 void Khoitao(Stack &amp;s) { s.top = NULL; } 8. Chng 3: Ngn xp Hng i Hin thc Stack dng DSLK (tt.) Kim tra xem Stack c rng khng: 8 int isEmpty ( Stack s ) { return s.top == NULL ? 1 : 0; } 9. Chng 3: Ngn xp Hng i Hin thc Stack dng DSLK (tt.) Thm mt phn t vo Stack: 9 void Push ( Stack &amp;s, DataType x ) { Node *p = new Node; if ( p==NULL ) { coutnext = NULL; if (s.top==NULL) // if (isEmpty(s)) s.top = p; else{ p-&gt;next = s.top; s.top = p; } } Thm phn t vo u danh sch 10. Chng 3: Ngn xp Hng i Hin thc Stack dng DSLK (tt.) Ly mt phn t ra khi Stack: 10 DataType Pop ( Stack &amp;s ) { if ( s.top==NULL ){ coutdata; delete p; return x; } Ly v xa phn t u danh sch 11. Chng 3: Ngn xp Hng i Hin thc Stack dng DSLK (tt.) Xem phn t nh Stack: 11 DataType Top ( Stack s ) { if ( s.top==NULL ){ coutnext = NULL; p-&gt;data = x; if (q.front==NULL) // TH Queue rng q.front = q.rear = p; else { q.rear-&gt;next = p; q.rear = p; } } 21. Chng 3: Ngn xp Hng i Hin thc Queue dng DSLK Ly phn t ra khi Queue: 21 DataType DeQueue(Queue &amp;q) { if (isEmpty(q)) { coutpNext; if ( q.front==NULL ) q.rear = NULL; delete p; return x; } 22. Chng 3: Ngn xp Hng i Hin thc Queue dng DSLK Xem thng tin ca phn t u Queue: DataType Front(Queue q) { if (isEmpty(q)) { cout</p>