目录
链栈
1.入栈
#include"stdio.h"
#include"stdlib.h"
typedef struct node
{
int data;
struct node *next;
}linklist;
linklist *push(linklist *top)
{
linklist *s;
s=(linklist *)malloc(sizeof(linklist));
scanf("%d",&s->data);
s->next=top;
top=s;
return top;
}
2.出栈
linklist *pop(linklist *top)
{
linklist *p;
while(top)
{
p=top;
int x=top->data;
printf("%d",x);
top=top->next;
free(p);
}
}
栈的完整代码
#include"stdio.h"
#include"stdlib.h"
typedef struct node
{
int data;
struct node *next;
}linklist;
linklist *push(linklist *top)
{
linklist *s;
s=(linklist *)malloc(sizeof(linklist));
scanf("%d",&s->data);
s->next=top;
top=s;
return top;
}
linklist *pop(linklist *top)
{
linklist *p;
while(top)
{
p=top;
int x=top->data;
printf("%d",x);
top=top->next;
free(p);
}
}
main()
{
linklist *top=NULL;
top=push(top);
top=pop(top);
}
队列(顺序结构)
1.循环队列(顺序结构)入队
void push(linklist *s)
{
printf("请输入%d个数据",maxsize-1);
if(((s->rear+1)%maxsize)==s->front)
exit(0);
else
for(int i=0;i<maxsize-1;i++)
{
scanf("%d",&s->data[s->rear]);
s->rear=(s->rear+1)%maxsize;
}
}
2.循环对列(顺序结构)出队
void pop(linklist *s)
{
if(s->rear==s->front)
{printf("失败"); exit(0);}
else
for(int i=0;i<maxsize-1;i++)
{
printf("%d ",s->data[s->front]);
s->front=(s->front+1)%maxsize;
}
}
循环对列(出队 入队)完整代码
#include"stdio.h"
#include"stdlib.h"
#define maxsize 5
typedef struct node
{
int front,rear;
int data[maxsize];
}linklist;
void push(linklist *s)
{
printf("请输入%d个数据",maxsize-1);
if(((s->rear+1)%maxsize)==s->front)
exit(0);
else
for(int i=0;i<maxsize-1;i++)
{
scanf("%d",&s->data[s->rear]);
s->rear=(s->rear+1)%maxsize;
}
}
void pop(linklist *s)
{
if(s->rear==s->front)
{printf("失败"); exit(0);}
else
for(int i=0;i<maxsize-1;i++)
{
printf("%d ",s->data[s->front]);
s->front=(s->front+1)%maxsize;
}
}
main()
{
linklist s;
s.rear=0;s.front=0;
push(&s);
pop(&s);
}
链队列
1.入队(链队列)
linklist *push(linklist *rear)
{
linklist *p;
p=(linklist *)malloc(sizeof(linklist));
scanf("%d",&p->data);
p->next=rear->next;
rear->next=p;
rear=p;
return rear;
}
2.出队(链队列)
linklist *pop(linklist *front,linklist *rear)
{
while(front!=rear)
{
linklist *p;
p=front->next;
front->next=p->next;
printf("%d",p->data);
free(p);
}
}
链队完整代码
#include"stdio.h"
#include"stdlib.h"
typedef struct node
{
int data;
struct node *next;
}linklist;
linklist *push(linklist *rear)
{
linklist *p;
p=(linklist *)malloc(sizeof(linklist));
scanf("%d",&p->data);
p->next=rear->next;
rear->next=p;
rear=p;
return rear;
}
linklist *pop(linklist *front,linklist *rear)
{
while(front!=rear)
{
linklist *p;
p=front->next;
front->next=p->next;
printf("%d",p->data);
free(p);
}
}
main()
{
linklist *rear,*front;
front=(linklist *)malloc(sizeof(linklist));
rear=front;
rear->next=NULL;
rear=push(rear);
rear=pop(front,rear);
}