链栈和链队列的建立及基本操作

链栈:c++版

 1 #include<iostream>
 2 #include<stdlib.h>
 3 using namespace std;
 4 struct Node{
 5     int value; //定义栈中的数据类型为 int 型
 6     Node *next;
 7 };
 8 Node * build_stack(){ //建立空的链栈的函数
 9     Node *p=(Node*)malloc(sizeof(Node));
10     p->next=NULL;
11     return p;
12 }
13 bool stack_empty(Node *a){ //判断栈是否为空的函数
14     if((a->next)==NULL) return true; //如果下一个地址为空,那么说明栈为空
15     return false;
16 }
17 int stack_size(Node *a){   //获取栈的大小的函数
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){  //将值为value的元素压入栈
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){   //删除栈顶元素
35     Node *p,*q;
36     p=a->next;
37     q=a->next->next;
38     free(p); //释放栈顶元素空间
39     a->next=q; //头指针指向栈顶元素
40 }
41 int get_top(Node *a){ //获取栈顶元素
42     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();  //调用建栈函数
49     if(a==NULL){
50         cout<<"建栈失败!"<<endl;
51         return 0;
52     }
53     cout<<"建栈成功! "<<endl;
54     cout<<"第一行输入一个n,下一行输入你要压入的n个元素 "<<endl;
55     int n;
56     cin>>n;  //输入n,表示n个元素
57     while(n--){
58         int value;
59         cin>>value;
60         push_stack(a,value);  //将输入的元素压入栈
61     }
62     if(!stack_empty(a)){
63         cout<<"栈的大小为: "<<stack_size(a)<<endl;
64     }else{
65         cout<<"栈为空"<<endl;
66     }
67     if(!stack_empty(a))
68         pop_stack(a);
69     if(!stack_empty(a))
70         cout<<"删除一个元素后,栈顶元素为:"<<get_top(a)<<endl;
71     int SIZE=stack_size(a);
72     cout<<"当前栈的大小为: "<<SIZE<<endl;
73     cout<<"将当前栈中的元素全部弹出:"<<endl;
74     while(!stack_empty(a)){
75         cout<<get_top(a)<<endl;
76         pop_stack(a);
77     }
78     return 0;
79 }

链队列:c++版

 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 }

猜你喜欢

转载自www.cnblogs.com/ISGuXing/p/9016121.html