顺序栈,链栈
//1.h
#ifndef __1_H__
#define __1_H__
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXSIZE 4
typedef int datatype;
typedef struct
{
datatype data[MAXSIZE];
int top;
}*ss,SS;
ss create();
int insert(ss s,datatype e);
void output(ss s);
int pop(ss s);
typedef struct node
{
union
{
int len;
datatype data;
};
struct node *next;
}*ls,ll;
ls create1(int flag);
int insert1(ls s,datatype e);
void output1(ls s);
int pop1(ls s);
#endif
//1.c
#include"1.h"
int main(int argc, const char *argv[])
{
//ss s = create();
char select;
datatype e;
#if 0
while(1)
{
printf("input insert data\n");
scanf("%d",&e);
printf("go on ? y/n\n");
scanf(" %c",&select);
insert(s,e);
if(select == 'N' ||select == 'n')
break;
}
output(s);
pop(s);
output(s);
#else
ls s = create1(1);
while(1)
{
printf("input insert1 data\n");
scanf("%d",&e);
printf("go on ? y/n\n");
scanf(" %c",&select);
insert1(s,e);
if(select == 'N' ||select == 'n')
break;
}
output1(s);
pop1(s);
output1(s);
#endif
return 0;
}
//fun1.c
#include"1.h"
//创建
ss create()
{
ss s = (ss)malloc(sizeof(SS));
if(!s)
{
printf("create failed\n");
return NULL;
}
s->top = -1;
return s;
}
int insert(ss s,datatype e)
{
//判断成功和满
if(!s || s->top == MAXSIZE-1)
{
printf("insert failed \n");
return -1;
}
s->data[++s->top] = e;
return 0;
}
void output(ss s)
{
if(!s || s->top == -1)
{
printf("output failed\n");
return;
}
for(int i = 0; i <= s->top; i++)
printf("%d\t",s->data[i]);
puts("");
}
int pop(ss s)
{
if(!s || s->top == -1)
{
printf("pop failed\n");
return -1;
}
printf("pop data = %d\n",s->data[s->top--]);
}
//
ls create1(int flag)
{
ls s = (ls)malloc(sizeof(ll));
if(!s)
{
printf("create1 failed\n");
return NULL;
}
if(flag == 1)
s->len = 0;
else if(flag == 0)
s->data = 0;
s->next = NULL;
return s;
}
int insert1(ls s,datatype e)
{
if(!s)
{
printf("insert1 failed\n");
return -1;
}
ls p = create1(0);
if(!p) return -1;
p->data = e;
p->next = s->next;
s->next = p;
s->len ++;
return 0;
}
void output1(ls s)
{
if(!s || !s->next)
{
printf("output1 failed\n");
return;
}
ls p = s;
while(p->next)
{
p = p->next;
printf("%d\t",p->data);
}
}
int pop1(ls s)
{
if(!s || !s->next)
{
printf("pop1 failed\n");
return -1;
}
ls p = s->next;
s->next = p->next;
printf("pop data = %d\n",p->data);
free(p);
p = NULL;
s->len --;
return 0;
}
2.顺序队列 循环队列
#ifndef __2_H__
#define __2_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int datatype;
#define MAXSIZE 6
typedef struct
{
datatype data[MAXSIZE];
int front;
int rear;
}*qq,Q;
qq create();
int enqueue(qq q,datatype e);
void output(qq q);
int dequeue(qq q);
typedef struct
{
datatype data[MAXSIZE];
int front;
int rear;
}*qq1,Q1;
qq1 create1();
int enqueue1(qq1 q,datatype e);
void output1(qq1 q);
int dequeue1(qq1 q);
int len1(qq1 q);
#endif
//2.c
#include"2.h"
int main()
{
qq1 q = create1();
char select[10];
datatype e;
/* while(1)
{
printf("input enqueue data\n");
scanf("%d",&e);
enqueue(q,e);
printf("go on? yes/no: ");
scanf("%s",select);
if(!strcmp(select,"no"))
break;
}
output(q);
while(1)
{
dequeue(q);
printf("go on? yes/no: ");
scanf("%s",select);
if(!strcmp(select,"no"))
break;
}
output(q);
enqueue(q,1000);
*/
while(1)
{
printf("input enqueue1 data\n");
scanf("%d",&e);
enqueue1(q,e);
printf("go on? yes/no: ");
scanf("%s",select);
if(!strcmp(select,"no"))
break;
}
output1(q);
while(1)
{
dequeue1(q);
printf("go on? yes/no: ");
scanf("%s",select);
if(!strcmp(select,"no"))
break;
}
output1(q);
enqueue1(q,1000);
output1(q);
int len = len1(q);
printf("len = %d\n",len);
return 0;
}
//fun2.c
#include"2.h"
qq create()
{
qq q = (qq)malloc(sizeof(Q));
if(!q)
{
printf("create failed\n");
return NULL;
}
q->front = q->rear = 0;
return q;
}
int enqueue(qq q,datatype e)
{
if(!q || q->rear == MAXSIZE)
{
printf("enqueue failed\n");
return -1;
}
q->data[q->rear++] = e;
return 0;
}
void output(qq q)
{
if(!q || q->rear == q->front)
{
printf("output failed\n");
return;
}
for(int i = q->front; i < q->rear; i++)
printf("%d\t",q->data[i]);
puts("");
}
int dequeue(qq q)
{
if(!q || q->rear == q->front)
{
printf("dequeue failed\n");
return -1;
}
printf("dequeue data = %d\n",q->data[q->front++]);
return 0;
}
///
qq1 create1()
{
qq1 q = (qq1)malloc(sizeof(Q1));
if(!q)
{
printf("create failed\n");
return NULL;
}
q->front = q->rear = 0;
return q;
}
int enqueue1(qq1 q,datatype e)
{
if(!q || q->front == (q->rear+1)%MAXSIZE)
{
printf("enqueue failed\n");
return -1;
}
q->data[q->rear] = e;
q->rear = (q->rear+1)%MAXSIZE;
return 0;
}
void output1(qq1 q)
{
if(!q || q->front == q->rear)
{
printf("output1 failed\n");
return;
}
for(int i = q->front; i != q->rear; i = (i+1)%MAXSIZE)
printf("%d\t",q->data[i]);
puts("");
}
int dequeue1(qq1 q)
{
if(!q || q->rear == q->front)
{
printf("dequeue1 failed\n");
return -1;
}
printf("dequeue1 data = %d\n",q->data[q->front]);
q->front = (q->front+1)%MAXSIZE;
return 0;
}
int len1(qq1 q)
{
return (MAXSIZE-q->front+q->rear);
}
循环队列
因循环队列存在队满的情况,所以引出链式队列,解决循环队列满的问题。
1> 链式存储:逻辑相邻,物理不一定相邻
2> 链式队列的插入和删除
2.1 尾插和头删:单链表的表尾等价于队列的队尾,链表的头等价于队列的队头
2.2 头插和尾删:单链表的头等价于队列的队尾,链表的尾等价于队列的队头
//1.h
#ifndef __3_H__
#define __3_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int datatype;
typedef struct links
{
union
{
int len;
datatype data;
};
struct links *next;
}*ls;
typedef struct linkq
{
ls front;
ls rear;
}*lq;
lq create_h();
int insert(lq q,datatype e);
void output(lq q);
int dequeue(lq q);
lq free_q(lq q);
#endif
//fun3.c
#include "3.h"
lq create_h()
{
lq q = (lq)malloc(sizeof(struct linkq));
if(!q)
{
printf("create_h q failed\n");
return NULL;
}
ls l = (ls)malloc(sizeof(struct links));
if(!l)
{
printf("create_h l failed\n");
return NULL;
}
l->len = 0;
l->next = NULL;
q->front = l;
q->rear = l;
return q;
}
int insert(lq q,datatype e)
{
if(!q)
{
printf("insert failed \n");
return -1;
}
ls s = (ls)malloc(sizeof(struct links));
if(!s)
{
printf("insert ls s failed\n");
return -1;
}
s->data = e;
s->next = NULL;
q->rear->next = s;
q->rear = s;
q->front->len ++;
return 0;
}
void output(lq q)
{
if(!q || q->front == q->rear)
{
printf("output failed\n");
return;
}
ls p = q->front;
while(p->next)
{
p = p->next;
printf("%d\t",p->data);
}
puts("");
}
int dequeue(lq q)
{
if(!q || q->front == q->rear)
{
printf("dequeue failed\n");
return -1;
}
ls s = q->front->next;
if(q->rear == s) q->rear = q->front;
int data = s->data;
printf("dequeue data = %d\n",data);
q->front->next = s->next;
q->front->len --;
free(s);
s = NULL;
return 0;
}
lq free_q(lq q)
{
if(!q || q->rear == q->front)
{
printf("free_q failed \n");
return NULL;
}
int len = q->front->len;
for(int i = 0; i < len; i ++)
{
dequeue(q);
}
free(q->front);
q->front = q->rear = NULL;
free(q);
q= NULL;
printf("free_q success\n");
return q;
}
//3.c
#include"3.h"
int main()
{
lq q = create_h();
datatype e;
char select;
while(1)
{
printf("input insert data: ");
scanf("%d",&e);
insert(q,e);
printf("go on? y/n : ");
scanf(" %c",&select);
if(select == 'n')
break;
}
output(q);
dequeue(q);
output(q);
q = free_q(q);
return 0;
}