2024/2/26

#include"link_stack.h"
  2 //申请栈顶指针
  3 top_p create_top()
  4 {
  5     top_p top = (top_p)malloc(sizeof(top_t));
  6     if(top==NULL)
  7     {
  8         printf("空间申请失败\n");
  9         return NULL;
 10     }
 11     top->len = 0;
 12     top->ptop = NULL;  //刚申请栈指针时没有指向元素
 13     return top;
 14 }
 15 //申请结点的函数
 16 link_p create_node(int data)
 17 {
 18     link_p new = (link_p)malloc(sizeof(link_stack));
 19     if(new==NULL)
 20     {
 21         printf("申请空间失败\n");
 22         return NULL;
 23     }
 24     new->data = data;
 25     return new;
 26 }
 27 //入栈/压栈
 28 void push_stack(top_p T,int data)
 29 {
 30     if(T==NULL)
 31     {
 32         printf("入参为空\n");
 33         return;
 34     }
 35     link_p new = create_node(data);
 36     //入栈
 37     new->next = T->ptop;
 38     T->ptop = new;
 39     T->len++;
 40 }
 41 //判空
 42 int empty(top_p T)
 43 {
 44     if(T==NULL)                                                                                                                                                                                                                                                                                                                                                                 
 45     {
 46         printf("入参为空\n");
 47         return;
 48     }
 49     return T->ptop==NULL?1:0;
 50 }
 51 //出栈/弹栈
 52 void pop_stack(top_p T)
 53 {
 54     if(T==NULL)
 55     {
 56         printf("入参为空\n");
 57         return;
 58     }
 59     if(empty(T))
 60     {
 61         printf("链表为空\n");
 62         return;
 63     }
 64     pritnf("%d",T->ptop->data);
 65     T->len--;
 66 }
 67 //遍历
 68 void show_stack(top_p T)
 69 {
 70     if(T==NULL)
 71     {
 72         printf("入参为空\n");
 73         return;
 74     }
 75     if(empty(T))
 76     {
 77         printf("链表为空\n");
 78         return;
 79     }
 80     link_p p=T->ptop;
 81     for(int i=0;i<T->len;i++)
 82     {
 83         printf("%d",p->data);
 84         p=p->next;
 85     }
 86 }
 87 //销毁
 88 void free_stack(top_p T)
 89 {
 90     if(T==NULL)
 91     {
 92          printf("入参为空\n");
 93          return;
 94     }
 95 
 96 }
 #include"double.list.h"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  2 #include<stdio.h>
  3 //创建双向循环链表
  4 double_p create_head()
  5 {
  6     double_p H = (double_p)malloc(sizeof(double_list));
  7     if(H==NULL)
  8     {
  9         printf("申请空间失败\n");
 10         return NULL;
 11     }
 12     H->len=0;
 13     H->next=NULL;
 14     H->pri=NULL;
 15     return H;
 16 }
 17 //创建结点
 18 double_p create_node(datatype data)
 19 {
 20     double_p new = (double_p)malloc(sizeof(double_list));
 21     if(new==NULL)
 22     {
 23         printf("申请空间失败\n");
 24         return NULL;
 25     }
 26     new->data = data;
 27     return new;
 28 }
 29 //判空
 30 int double_empty(double_p H)
 31 {
 32     if(H==NULL)
 33     {
 34         printf("入参为空,请检查\n");
 35         return -1;
 36     }
 37     //如果头结点指向空说明链表为空
 38     return H->next==NULL?1:0;
 39 }
 40 //头插
 41 void insert_head(double_p H,datatype data)
 42 {
 43     if(H==NULL)
 44     {
 45         printf("入参为空\n");
 46         return;
 47     }
 48     double_p new = create_node(data);
 49     if(H->next!=NULL)
 50     {
 51         new->pri=H;  //新结点的前驱指向头
 52         H->next->pri=new; //头结点后继结点的前驱指向新结点
 53         //如果不判断H->next==NULL,H->next->pri会涉及到空指针的间接访问
 54         new->next = H->next; //新结点的后继指向H的后继
 55         H->next = new;   //最后写
 56         H->len++;
 57     }
 58     else
 59     {
 60         new->next = H->next;
 61         H->next = new;
 62         new->pri = H;
 63         H->len++;
 64     }
 65 }
 66 //尾插
 67 void insert_tail(double_p H,datatype data)
 68 {
 69     if(H==NULL)
 70     {
 71         printf("入参为空\n");
 72         return;
 73     }
 74     double_p new = create_node(data);
 75     //找到最后一个结点
 76     double_p p = H;
 77     while(p->next!=NULL)
 78     {
 79         p = p->next;
 80     }
 81     new->pri = p;  //新结点的前驱指向原来的尾结点
 82     new->next = p->next;
 83     p->next = new; //尾结点的后继指向新结点
 84     H->len++;
 85 }
 86 //按位置插入
 87 void insert_pos(double_p H,datatype data,int pos)
 88 {
 89     if(H==NULL)
 90     {
 91         printf("入参为空\n");
 92         return;
 93     }
 94     //位置不合理
 95     if(pos<=0||pos>H->len+1)
 96     {
 97         printf("位置不合理\n");
 98         return;
 99     }
100     //找到pos-1位置的结点
101     double_p p = H;
102     for(int i=0;i<pos-1;i++)
103     {
104         p=p->next;
105     }
106     double_p new = create_node(data);
107     new->pri = p; //新结点的前驱指向pos-1
108     new->next = p->next; //新结点的后继指向原来pos-1的后继
109     if(p->next!=NULL)
110     {
111         p->next->pri = new;  //如果pos位置本身有结点
112         //让pos结点的前驱指向新结点
113     }
114     p->next = new;  //pos-1的后继指向新结点
115 }
116 //头删
117 void del_head(double_p H)
118 {
119     if(H==NULL)
120     {
121         printf("入参为空\n");
122         return;
123     }
124     if(double_empty(H))
125     {
126         printf("链表为空\n");
127         return;
128     }
129     double_p del = H->next;
130     //H->next->next->pri = H;
131     H->next = H->next->next;
132     del->next->pri = H;
133     free(del);
134     H->len--;
135 }
136 //尾删
137 void del_tail(double_p H)
138 {
139     if(H==NULL){
140         printf("入参为空\n");
141         return;
142     }
143     if(double_empty(H))
144     {
145         printf("链表为空\n");
146         return;
147     }
148     double_p p = H->next;
149     while(p->next!=NULL)
150     {
151         p=p->next;
152     }
153     p->pri->next=NULL;  //让尾结点前一个结点的next指向NULL;
154     free(p);
155     H->len--;
156 }
157 //按位置删除
158 void del_pos(double_p H,int pos)
159 {
160     if(H==NULL)
161     {
162         printf("入参为空\n");
163         return;
164     }
165     if(pos<=0||pos>H->len)
166     {
167         printf("位置不合理\n");
168         return;
169     }
170     double_p p = H;
171     for(int i=0;i<pos;i++)
172     {
173         p = p->next;
174     }
175     p->next->pri = p->pri;
176     p->pri->next = p->next;
177     free(p);
178     H->len--;
179 }
180 //释放双向链表
181 void free_double(double_p H)
182 {
183     if(H==NULL)
184     {
185         printf("入参为空\n");
186         return;
187     }
188     if(double_empty(H))
189     {
190         printf("链表为空\n");
191         return;
192     }
193     //循环头删
194     double_p p = H->next;
195     double_p del;
196     while(p!=NULL)
197     {
198         del=p;   //让del指针保存每次要释放的结点
199         p=p->next;
200         free(del);
201         H->len--;
202         del=NULL;
203     }
204 }
205 //输出
206 void out_put(double_p H)
207 {
208     if(H==NULL)
209     {
210         printf("入参为空,请检查\n");
211         return;
212     }
213     double_p p=H->next;
214     while(p!=NULL)
215     {
216         printf("%d->",p->data);
217         p=p->next;
218     }
219     printf("NULL\n");
220 }
1 #include"seq_stack.h"
  2 seq_p create_stack()
  3 {
  4     seq_p S = (seq_p)malloc(sizeof(seq_stack));
  5     if(S==NULL)
  6     {
  7         printf("申请空间失败\n");
  8         return NULL;
  9     }
 10     S->top = -1;  //记录栈中没有元素
 11     return S;
 12 }
 13 //判空
 14 int empty_stack(seq_p S)
 15 {
 16     if(S==NULL)
 17     {
 18         printf("入参为空\n");
 19         return -1;
 20     }
 21     return S->top==-1?1:0;
 22 }
 23 //判满
 24 int full_stack(seq_p S)
 25 {
 26     if(S==NULL)
 27     {
 28         printf("入参为空\n");
 29         return -1;
 30     }
 31     return S->top==MAX-1?1:0;
 32 }
 33 //入栈(只能在栈顶操作)
 34 void push_stack(seq_p S,datatype data)
 35 {
 36     if(S==NULL)
 37     {
 38         printf("入参为空\n");
 39         return;
 40     }
 41     if(full_stack(S))                                                                                                                                                                                                                                                                                                                                                                                                                                                        
 42     {
 43         printf("栈已满\n");
 44         return;
 45     }
 46     //可以入栈
 47     S->top++;   //先加
 48     S->data[S->top]=data; //再压
 49 }
 50 //出栈/弹栈
 51 void pop_stack(seq_p S)
 52 {
 53     if(S==NULL)
 54     {
 55         printf("入参为空\n");
 56         return;
 57     }
 58     if(empty_stack(S))
 59     {
 60         printf("栈空无需出栈\n");
 61         return;
 62     }
 63     printf("出栈元素为>>%d\n",S->data[S->top]);
 64     S->top--;
 65 }
 66 //清空
 67 void clean_stack(seq_p S)
 68 {
 69     if(S==NULL)
 70     {
 71         printf("入参为空\n");
 72         return;
 73     }
 74     S->top=-1;
 75 }
 76 //销毁栈
 77 //传二级指针,是为了给主函数内的S置空
 78 void free_stack(seq_p *S)
 79 {
 80     if(S==NULL||*S==NULL)
 81     {
 82         printf("入参为空\n");
 83         return;
 84     }
 85 
 86     //释放堆区空间
 87     free(*S);
 88     *S=NULL;
 89 }
 90 //输出
 91 void show_stack(seq_p S)
 92 {
 93     if(S==NULL)
 94     {
 95         printf("入参为空\n");
 96         return;
 97     }
 98     if(empty_stack(S))
 99     {
100         printf("栈为空\n");
101         return;
102     }
103 
104     //要保留栈只能从栈顶操作的性质
105     for(int i=S->top;i>=0;i--)
106     {
107         printf("%d\n",S->data[i]);
108     }
109 }
~                          

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值