Tải bản đầy đủ (.pdf) (10 trang)

Bài giảng Cấu trúc dữ liệu và giải thuật – Bài 10: Ứng dụng của ngăn xếp

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (449.49 KB, 10 trang )

<span class='text_page_counter'>(1)</span><div class='page_container' data-page=1>

Giảng viên: TS. Ngo Huu Phuc
Tel: 0438 326 077
Mob: 098 5696 580
Email:


<b>C</b>

<b>ấu trúc dữ liệu và giải thuật</b>



</div>
<span class='text_page_counter'>(2)</span><div class='page_container' data-page=2>

<b>10.1. Đảo mảng (3)</b>
<b>10.2. Đảo chuỗi (4)</b>


<b>10.3. Chuyển đổi hệ số (9)</b>
<b>10.4. Bracket Matching (5)</b>
<b>10.5. Balancing Act (4)</b>


<b>Tham khảo:</b>


1. Data structures and Algorithms Stacks.htm


2. Kyle Loudon Mastering Algorithms, Chapter 6 Stacks and Queues


3. Elliz Horowitz – Fundamentals of Data Structures, Chapter 3 Stacks and


Queues


4. Deshpande Kakle – C and Data Structures, Chapter 19. Stacks and


Queues


</div>
<span class='text_page_counter'>(3)</span><div class='page_container' data-page=3>

 Cho một mảng gồm một dãy các giá trị.


 Để đảo thứ tự các phần tử trong mảng, sử dụng



nguyên lý Last-In-First-Out của Stack.


 Ví dụ về hoạt động của đảo mảng:


<i>17</i>
<i>23</i>
<i>25</i>
<i>18</i>


<i>18</i>
<i>25 </i>
<i>23</i>
<i>17</i>


<i>134</i>
<i>47</i>
<i>55</i>
<i>216</i>


</div>
<span class='text_page_counter'>(4)</span><div class='page_container' data-page=4>

1. Khởi tạo một Stack rỗng, có kiểu số.


2. Với n phần tử của mảng, lần lượt đưa vào Stack


thông qua hàm Push:


Push <b>a[i]</b> in to <b>Stack</b>.


3. Lần lượt lấy ra từ Stack n phần tử và đưa vào trở



lại mảng ban đầu:


Pop a item from <b>nStack</b> in to <b>a[i]</b>.


</div>
<span class='text_page_counter'>(5)</span><div class='page_container' data-page=5>

<b>Ví d</b>

<b>ụ về đảo mảng:</b>



void revArray(int a[],int n,int stack[])


{


makeEmpty(stack);


for(int i=0;i<n;i++)
push(stack,a[i]);


</div>
<span class='text_page_counter'>(6)</span><div class='page_container' data-page=6>

 Đảo chuỗi thực hiện việc đảo các từ trong chuỗi, sử


dụng ý tưởng Last-In-First-Out của Stack.


 Ví dụ về đảo chuỗi:


<i>Tơi</i>
<i>Làm</i>


<i>Bài</i>
<i>Tập</i>


<i>Tập</i>
<i>Bài </i>
<i>Làm</i>


<i>Tơi</i>


<i>Chăm</i>
<i>Học</i>


<i>Thì</i>
<i>Giỏi</i>


</div>
<span class='text_page_counter'>(7)</span><div class='page_container' data-page=7>

<b>Ý tưởng xây dựng chương trình:</b>
1. Tạo một <b>wStack</b> rỗng.


2. Với mỗi từ <b>mWord</b> đọc được từ <b>string</b>, đưa từ đó


vào Stack:


Push <b>mWord</b> into <b>wStack</b>.


3. Đọc từ <b>wStack</b> cho đến hết, thực hiện:


Pop a word from <b>wStack</b> into <b>mWord</b>.


Concate <b>mWord</b> to the end of <b>outp string</b>.


4. Dừng giải thuật.


</div>
<span class='text_page_counter'>(8)</span><div class='page_container' data-page=8>

#include <conio.h>


#include "string.h"


#define MAXSIZE 100



#define MAX 100


void push(char* stack[], char* value, int* top);


char* pop(char* stack[], int* top);


void main() {


char* stack[MAXSIZE];


int top=-1;


charmString[MAX];


char* mWord;


printf("Nhap vao mot chuoi: ");
gets(mString);


mWord = strtok(mString," ");
push(stack,mWord,&top);


mWord = strtok(NULL," ");


if(mWord) push(stack,mWord,&top);
}
char *p=pop(stack,&top);
while(top>=0)
{


p=strcat(p," ");
p=strcat(p,pop(stack,&top));
}


printf("Chuoi ket qua \n");
puts(p);


</div>
<span class='text_page_counter'>(9)</span><div class='page_container' data-page=9>

void push(char* stack[], char* value, int* top)
{


if(*top < MAXSIZE)


{
*top=*top+1;
stack[*top]=(char*)malloc(strlen(value));
strcpy(stack[*top],value);
}
else
{


printf("Khong the them vao STACK\n");
getch();


}
}


char* pop(char* stack[], int* top) {


char* value;



if(*top>=0) {


value=(char*)malloc(strlen(stack[*top]));
strcpy(value,stack[*top]);


*top=*top-1;
}


else


{ printf("STACK rong\n");


value = NULL; }


returnvalue;
}


void makeEmpty(int* top) {


</div>
<span class='text_page_counter'>(10)</span><div class='page_container' data-page=10>

nhị phân (Binary), hệ 16 (Hexadecimal).


 Con người thường sử dụng hệ thập phân.


 Đối với máy tính, thường sử dụng hệ nhị phân.


 Hệ 16 là hệ trung gian giữa hệ thập phân và hệ nhị
phân.


</div>

<!--links-->

×