Gửi bởi
nunkstop
stack và queue đều dùng array hết mà ta[IMG]images/smilies/lick.gif[/IMG][IMG]images/smilies/lick.gif[/IMG][IMG]images/smilies/lick.gif[/IMG]
Stack
Mã:
#include <iostream>#include <string.h>using namespace std; //--------------------------- typedef struct Node{ float value; Node *prev;}; typedef struct{ Node *top;}Stack; //-----------------------------void CreatStack(Stack &p);void Push(Stack &p,float x);void Pop(Stack &p,float &x);void DestroyStack(Stack &p);int isEmpty(Stack p);void menu();//----------------------------- void main(){ Stack S; float tmp; menu(); for(;;) { char ch[1]; do { cout<<"
Choice: "; cin>>ch; } while(atoi(ch)<1 || atoi(ch)>7); switch (atoi(ch)) { case 1: CreatStack(S); break; case 2: cout<<"
Float number: "; cin>>tmp; Push(S,tmp); break; case 3: if(!isEmpty(S)) { Pop(S,tmp); cout<<"
Popped number:"<<tmp; } break; case 4: cout<<"
All in stack:"; while(!isEmpty(S)) { Pop(S,tmp); cout<<tmp<<" "; } break; case 5: if(isEmpty(S)) cout<<"
Stack is empty"; else cout<<"
Stack isn't empty"; break; case 6: DestroyStack(S); cout<<"
Stack was destroyed."; break; case 7: exit(1); }; } system("pause");}void menu(){ system("cls"); cout<<"
Program to test a stack"; cout<<"
Please choose a choice:"; cout<<"
1.Create a stack"; cout<<"
2.Push a float number"; cout<<"
3.Pop from stack"; cout<<"
4.Pop all from stack"; cout<<"
5.Checking stack is empty"; cout<<"
6.Destroy stack"; cout<<"
7.Exit";}//Stack//----------------------------------------------------------------------void CreatStack(Stack &p){ p.top = NULL;} void Push(Stack &p,float x){ Node *temp; temp = new Node; if(temp == NULL) { cout<<"
Stack is full"; return; } else { temp->value = x; temp->prev = p.top; p.top = temp; }} void Pop(Stack &p,float &x){ if (isEmpty(p)) { cout<<"
Stack is empty"; return; } else { x = (p.top)->value; Node *temp = (p.top)->prev; delete p.top; p.top = temp; }} int isEmpty(Stack p){ if (p.top == NULL) return 1; else return 0;} void DestroyStack(Stack &p) { Node *tmp; while(!isEmpty(p)) { tmp = (p.top)->prev; delete p.top; p.top = tmp; } }//----------------------------------------------------------------------//
Queue
Mã:
#include <iostream>#include <string.h>using namespace std; //--------------------------- typedef struct Node{ float value; Node *next; Node *prev;}; typedef struct{ Node *front; Node *rear;}Queue; //-----------------------------void CreatQueue(Queue &p);void Enqueue(Queue &p,float x);void Dequeue(Queue &p,float &x);void DestroyQueue(Queue &p);int isEmpty(Queue p);void menu();//----------------------------- void main(){ Queue S; float tmp; menu(); for(;;) { char ch[2]; do { cout<<"
Choice: "; cin>>ch; } while(atoi(ch)<1 || atoi(ch)>7); switch (atoi(ch)) { case 1: CreatQueue(S); break; case 2: cout<<"
Float number: "; cin>>tmp; Enqueue(S,tmp); break; case 3: if (!isEmpty(S)) { Dequeue(S,tmp); cout<<"
Dequeued number:"<<tmp; } break; case 4: cout<<"
All in Queue:"; while(!isEmpty(S)) { Dequeue(S,tmp); cout<<tmp<<" "; } break; case 5: if(isEmpty(S)) cout<<"
Queue is empty"; else cout<<"
Queue isn't empty"; break; case 6: DestroyQueue(S); cout<<"
Queue was destroyed."; break; case 7: exit(1); }; } system("pause");}void menu(){ system("cls"); cout<<"
Program to test a Queue"; cout<<"
A Queue has created. Please choose a choice:"; cout<<"
1.Create a Queue"; cout<<"
2.Enqueue a float number"; cout<<"
3.Dequeue from Queue"; cout<<"
4.Dequeue all from Queue"; cout<<"
5.Checking Queue is empty"; cout<<"
6.Destroy Queue"; cout<<"
7.Exit";}//Queue//----------------------------------------------------------------------void CreatQueue(Queue &p){ p.front = NULL; p.rear = NULL;}//Hàng đợi nạp phần tử vào cuối danh sách móc nốivoid Enqueue(Queue &p,float x){ Node *temp; temp = new Node; if(temp == NULL) { cout<<"
Queue is full"; return; } else if (!isEmpty(p)) { temp->value = x; //Cài đặt nút temp temp->next = NULL; //với tư cách là temp->prev = p.rear;/út cuối p.rear->next = temp; p.rear = temp; } else { temp->value = x; temp->next = NULL; temp->prev = NULL; p.front = p.rear = temp; }}//Hàng đợi lấy phần tử từ đầu danh sách móc nốivoid Dequeue(Queue &p,float &x){ if (isEmpty(p)) { cout<<"
Queue is empty"; return; } else if (p.front != p.rear)//Nếu hàng đợi có hơn 1 phần tử { x = (p.front)->value; Node *temp = p.front; p.front = temp->next; delete temp; } else//Hàng đợi có 1 phần tử { x = (p.front)->value; delete p.front; p.front = p.rear = NULL; }} int isEmpty(Queue p){ if (p.front == NULL && p.rear == NULL) return 1; else return 0;} void DestroyQueue(Queue &p) { Node *tmp; if (!isEmpty(p)) { while(p.front != NULL) { tmp = (p.front)->next; delete p.front; p.front = tmp; } p.front = p.rear = NULL; } }//----------------------------------------------------------------------//
Thế cách cài đặt 2 cái trên là gì vậy. [IMG]images/smilies/biggrin.png[/IMG]
Pallet nhựa Long An đã trở thành một trong những lựa chọn phổ biến cho nhu cầu vận chuyển và lưu trữ hàng hóa trong nhiều ngành công nghiệp. Với đặc tính nhẹ nhàng, chắc chắn và dễ vận chuyển, các...
Thanh lý pallet nhựa Long An giá rẻ