[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>链式栈
在上节我们提到了顺序栈,而链式栈就是把顺序栈用链表的方式来存储,如果对链表和顺序栈的学习较为深刻的话,学习链式栈会非常容易。
首先第一步和单链表操作基本相同,创建一个结构体,一块是数据域,另一块是指针域,用来指向下一个元素;
typedef struct Stack{ int data; struct Stack *next; }Stack;
对于链式栈的结构,可以看下图:
然后我们来看链式栈的一些基本操作:
初始化栈:
Stack *InitStack(Stack *s)//初始化栈 { s=NULL; return s; }
判断栈是否为空栈:
bool emptystack(Stack* s) {//判断是否为空栈 if (s)return true; else return false; }
链式栈的入栈操作:
Stack* push(Stack* s, int newnum) {//链式栈的入栈操作 Stack* newstack=(Stack*)malloc(
sizeof(Stack)); newstack->data = newnum; newstack->next = s; s = newstack;
return s; }
逐个打印栈内元素
void print(Stack*s){ //逐个打印栈内元素 Stack *a=s; while(a){ printf("%d ",a->data); a
=a->next; } }
链式栈的出栈操作
Stack* pop(Stack* s,int *newnum) {//链式栈的出栈操作 if (s==NULL) { printf("栈已空");
return NULL; } else { *newnum = s->data; Stack* p = s; s = s->next; free(p);
return s; } }
然后我们在主函数测试一下这些函数:
int main() { Stack *s; s=InitStack(s); int i; for(i=1;i<=10;i++){ s=push(s,i);
} print(s); }
运行结果: