#include <stdio.h>
#include <cstdlib>
typedef struct SNode{
int data;
struct SNode *next;
}SNode,*StackList;
bool InitStack(StackList &s){
s = (SNode*)malloc(sizeof (SNode));
if(s==NULL) return false;
s->next=NULL;
return true;
};
bool InsertNextNode(SNode *p,int e){
SNode *q = (SNode*)malloc(sizeof (SNode));
if(q==NULL) return false;
q->next=p->next;
p->next=q;
q->data=e;
return true;
}
bool DeleteNextNode(SNode *p){
if(p==NULL) return false;
SNode *q=p->next;
p->next=q->next;
free(q);
return true;
}
int getItem(StackList L,int index){
SNode *p=L->next;
int j=0;
if (L == NULL) return -1;
while (p->next!=NULL&&j<index-1){
p=p->next;
j++;
}
return p->data;
}
int main() {
StackList L;
InitStack(L);
InsertNextNode(L,3);
InsertNextNode(L,2);
InsertNextNode(L,1);
printf_s("%d",getItem(L,1));
DeleteNextNode(L);
printf_s("%d",getItem(L,1));
return 0;
}
3.1栈的链式存储
于 2021-07-10 22:55:34 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)