Chain stack: c++ version
1 #include<iostream>
2 #include<stdlib.h>
3 using namespace std;
4 struct Node{
5 int value; // Define the data type in the stack as int
6 Node * next;
7 };
8 Node * build_stack (){ // Function to create an empty chain stack
9 Node *p=(Node*) malloc ( sizeof (Node));
10 p->next= NULL;
11 return p;
12 }
13 bool stack_empty(Node *a ){ //Function to determine whether the stack is empty
14 if ((a->next)==NULL) return true ; // If the next address is empty, then the stack is empty
15 return false ;
16 }
17 int stack_size(Node *a ){ // Function to get the size of the stack
18 Node *p=a-> next;
19 int length_size= 0 ;
20 while (p!= NULL){
21 length_size++ ;
22 p=p-> next;
23 }
24 return length_size;
25 }
26 bool push_stack(Node *a, int value){ // Push the value element into the stack
27 Node *p=(Node *) malloc ( sizeof (Node));
28 if (p==NULL) return false ;
29 p->value= value;
30 p->next=a-> next;
31 a->next= p;
32 return true ;
33 }
34 void pop_stack(Node *a){ // delete stack top element
35 Node *p,* q;
36 p=a->next;
37 q=a->next-> next;
38 free (p); // release the space for the top element of the stack
39 a->next=q; // the head pointer points to the top element of the stack
40 }
41 int get_top(Node * a){ // Get the top element
42 of the stack if (a->next==NULL) return - 1 ;
43 int value=a->next-> value;
44 return value;
45 }
46 int main(){
47 Node * a;
48 a=build_stack(); // call stack build function
49 if (a== NULL){
50 cout<< " Failed to build the stack! " << endl;
51 return 0 ;
52 }
53 cout<< " Successful to build the stack! " << endl;
54 cout<< "The first Enter an n in one line, and enter the n elements you want to push in the next line " << endl;
55 int n;
56 cin>>n; // Enter n, which means n elements
57 while (n-- ){
58 int value;
59 cin>>value;
60 push_stack(a,value); // Push the input element onto the stack
61 }
62 if (! stack_empty(a)){
63 cout<< " The size of the stack is: " <<stack_size(a)<< endl;
64 } else {
65 cout<< "The stack is empty " << endl;
66 }
67 if (! stack_empty(a))
68 pop_stack(a);
69 if (! stack_empty(a))
70 cout<< " Remove one element, the top element of the stack is:" <<get_top(a)<< endl;
71 int SIZE= stack_size(a);
72 cout<< "The current stack size is: " <<SIZE<< endl;
73 cout<< " The current stack element Pop all: " << endl;
74 while (! stack_empty(a)){
75 cout<<get_top(a)<< endl;
76 pop_stack(a);
77 }
78 return 0 ;
79 }
Chain queue: c++ version
1 #include<iostream>
2 #include<stdlib.h>
3 using namespace std;
4 struct Node{
5 int value;
6 Node *next;
7 };
8 Node * build_queue(){ //建立一个空队列
9 Node *p=(Node *)malloc(sizeof(Node));
10 if(p==NULL) return p;
11 p->next=NULL;
12 return p;
13 }
14 bool queue_empty(Node *queue_head){ //判断队列是否为空
15 if((queue_head->next)==NULL) return true;
16 else return false;
17 }
18 void push_queue(Node * *queue_end,int value){ //将值为value的元素入队
19 Node *q=*queue_end;
20 Node *p=(Node *)malloc(sizeof(Node));//为新元素开辟空间
21 p->value=value;
22 p->next=NULL;
23 q->next=p;
24 *queue_end=p; //尾指针后移
25 }
26 void pop_queue(Node * queue_head){ //出队函数
27 Node *p=queue_head;
28 if(queue_empty(queue_head)) return; //判断队列是否为空
29 Node *q=p->next->next;
30 p=p->next;
31 free(p); //释放出队元素的存储空间
32 queue_head->next=q; //头指针后移
33 }
34 int get_front(Node *queue_head){ //获取队首元素的值
35 if(queue_empty(queue_head)==true) return -1; //如果队列为空,返回-1
36 else return queue_head->next->value;
37 }
38 int main(){
39 Node *queue_head,*queue_end;
40 queue_head=build_queue();
41 queue_end=queue_head; //队列初始时,队尾指针和队首指针指向同一结点
42 if(queue_end==NULL){
43 cout<<"建立队列失败!"<<endl;
44 return 0;
45 }
46 else cout<<"建立队列成功!第一行输入一个n表示你要将n个元素入队,第二行输入n个元素"<<endl;
47 int n;
48 cin>>n; //输入一个n表示下一行输入n个元素
49 for(int i=0;i<n;i++){
50 int x;
51 cin>>x;
52 push_queue(&queue_end,x); //将值为x的元素入队
53 }
54 cout<<"让当前队首元素 X 出队: X = "<<get_front(queue_head)<<endl;
55 pop_queue(queue_head);
56 cout<<"让当前队首元素 X 出队: X = "<<get_front(queue_head)<<endl;
57 pop_queue(queue_head);
58 cout<<"按顺序打印队列里面剩余的元素: ";
59 while(queue_head->next!=NULL){
60 cout<<queue_head->next->value<<' ';
61 pop_queue(queue_head);
62 }
63 cout<<endl;
64 return 0;
65 }