1. Trang chủ
  2. » Công Nghệ Thông Tin

Bài giảng Cấu trúc dữ liệu và giải thuật: Stack and Queue - TS. Ngô Hữu Dũng

61 6 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 61
Dung lượng 554,32 KB

Nội dung

Bài giảng Cấu trúc dữ liệu và giải thuật: Stack and Queue trình bày các nội dung sau: Khái niệm Stack, applications, thao tác trên Stack, tổ chức dữ liệu, thao tác Push vào Stack, thao tác Pop khỏi stack, Stack – Sử dụng mảng,... Đây là tài liệu học tập và giảng dạy dành cho sinh viên ngành tham khảo!

INDUSTRIAL UNIVERSITY OF HO CHI MINH CITY Data structures and algorithms Stack and Queue Dr Ngô Hữu Dũng Introduction Stack (LIFO – last in, first out: a collection of items in which only the most recently added item may be removed   Queue (FIFO – first in, first out): a collection of items in which first items entered are the first ones to be removed Cấu trúc liệu giải thuật - Stack&Queue Stack vs Queue Stack – Ngăn xếp    Last In First Out (LIFO) Thao tác  Push  Pop Push Pop 34 Top 56 45 Queue – Hàng đợi    First In First Out (FIFO) Thao tác deQueue  enQueue  deQueue 37 34 56 45 Front Cấu trúc liệu giải thuật - Stack&Queue 37 Rear enQueue Push Pop 34 Top 56 45 37 Stack – Last in, first out Stack Ngăn xếp Cấu trúc liệu giải thuật - Stack&Queue Khái niệm Stack Lưu trữ tập phần tử theo trật tự định Nguyên tắc: Last in, first out    Vào sau cùng, trước tiên Top: Phần tử Chèn phần tử vào top     Push Pop Thao tác push Chèn vào đầu danh sách Xuất phần tử từ top    Thao tác pop Xoá phần tử đầu danh sách Cấu trúc liệu giải thuật - Stack&Queue 34 56 45 37 Top Applications Balancing of symbols Infix to Postfix /Prefix conversion Redo-undo features at many places like editors, Photoshop Forward and backward feature in web browsers Used in many algorithms like Tower of Hanoi, tree traversals, stock span problem, histogram problem Other applications can be Backtracking, Knight tour problem, rat in a maze, N queen problem and Sudoku solver       Cấu trúc liệu giải thuật - Stack&Queue Thao tác Stack Push: Thêm phần tử vào stack   Nếu stack chưa đầy thêm phần tử top Pop: Xoá phần tử từ stack   Nếu stack khơng rỗng xố phần tử top Top: Lấy phần tử top initStack: khởi tạo Stack isEmpty: Kiểm tra stack rỗng?     Trả true stack rỗng isFull: Kiểm tra stack đầy?   Trả true stack đầy Tổ chức liệu  Array struct Stack { int top; int capacity; int* array; }; struct Stack* tStack;  Linked list struct Node { int data; struct Node* next; }; struct Stack { Node *top; }; PUSH Thao tác Push vào Stack Top Thao tác Pop khỏi stack Top POP 10 Queue số nguyên – Sử dụng mảng bool InitQueue(QUEUE &q, int MaxItem) { q.QArray = new int[MaxItem]; if (q.QArray == NULL) return false; q.QMax = MaxItem; q.QNumItems = 0; q.QFront = q.QRear = -1; return true; } 47 Queue số nguyên – Sử dụng mảng bool IsEmpty(QUEUE q) { if (q.QNumItems == 0) return true; return false; } 48 Queue số nguyên – Sử dụng mảng bool IsFull(QUEUE q) { if (q.QMax == q.QNumItems) return true; return false; } 49 Queue số nguyên – Sử dụng mảng bool EnQueue(QUEUE &q, int newitem) { if (IsFull(q)) return false; q.QRear++; if (q.QRear==q.QMax) q.QRear = 0; q.QArray[q.QRear] = newitem; if (q.QNumItems==0) q.QFront = 0; q.QNumItems++; return true; } 50 Queue số nguyên – Sử dụng mảng bool DeQueue(QUEUE &q, int &itemout) { if (IsEmpty(q)) return false; itemout = q.QArray[q.QFront]; q.QFront++; q.QNumItems ; if (q.QFront==q.QMax) q.QFront = 0; if (q.QNumItems==0) q.QFront = q.QRear = -1; return true; } 51 Queue số nguyên – Sử dụng mảng bool QueueFront(const QUEUE &q, int &itemout) { if (IsEmpty(q)) return false; itemout = q.QArray[q.QFront]; return true; } 52 Queue số nguyên – Sử dụng mảng bool QueueRear(const QUEUE &q, int &itemout) { if (IsEmpty(q)) return false; itemout = q.QArray[q.QRear]; return true; } 53 Queue – Ví dụ ứng dụng     54 Quản lý việc thực tác vụ (task) môi trường xử lý song song Hàng đợi in ấn tài liệu Vùng nhớ đệm (buffer) dùng cho bàn phím Quản lý thang máy Queue – Sử dụng DSLK typedef struct tagNODE { int data; tagNODE* pNext; } NODE, *PNODE; typedef struct tagQUEUE { int NumItems; PNODE pFront, pRear; } QUEUE; 55 Queue – Sử dụng DSLK Các thao tác bool InitQueue(QUEUE &q); bool IsEmpty(const QUEUE &q); bool IsFull(const QUEUE &q); bool EnQueue(QUEUE &q, int newitem); bool DeQueue(QUEUE &q, int& itemout);  56 Queue – Sử dụng DSLK bool InitQueue(QUEUE &q) { q.NumItems = 0; q.pFront = q.pRear = NULL; return true; } 57 Queue – Sử dụng DSLK bool IsEmpty(const QUEUE& q) { return (q.NumItems==0); } 58 Queue – Sử dụng DSLK bool IsFull(const QUEUE &q) { PNODE tmp = new NODE; if (tmp==NULL) return true; delete tmp; return false; } 59 Queue – Sử dụng DSLK bool EnQueue(QUEUE &q, int newitem) { if (IsFull(q)) return false; PNODE p = new NODE; p->data = newitem; p->pNext = NULL; if (q.pFront==NULL && q.pRear==NULL) q.pFront = q.pRear = p; else { q.pRear->pNext = p; q.pRear = p; } q.NumItems++; return true; } 60 Queue – Sử dụng DSLK bool DeQueue(QUEUE &q, int &itemout) { if (IsEmpty(q)) return false; PNODE p = q.pFront; q.pFront = p->pNext; itemout = p->data; q.NumItems ; delete p; if (q.NumItems==0) InitQueue(q); return true; } 61 ... tác deQueue  enQueue  deQueue 37 34 56 45 Front Cấu trúc liệu giải thuật - Stack& Queue 37 Rear enQueue Push Pop 34 Top 56 45 37 Stack – Last in, first out Stack Ngăn xếp Cấu trúc liệu giải thuật... phải trục vào stack Stack rỗng Stop 31 (3,4) (0,4) (0,1) t 5 i j deQueue 34 56 45 Front 37 Rear Queue – First in, first out Queue Hàng đợi 32 Cấu trúc liệu giải thuật - Stack& Queue enQueue Queue. .. problem and Sudoku solver       Cấu trúc liệu giải thuật - Stack& Queue Thao tác Stack Push: Thêm phần tử vào stack   Nếu stack chưa đầy thêm phần tử top Pop: Xoá phần tử từ stack   Nếu stack

Ngày đăng: 10/05/2021, 23:17

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN