栈的c语言实现
如听仙乐耳暂明
回头准备写一遍顺序表,龙哥没讲,先去看那个数据结构网站,写的稀巴烂,直接把整个结构体往函数里面嗯塞,家里卖内存的是吧。
去找了浙大陈越姥姥的代码,写的确实是漂亮,每个参数的意义都很明确,也把所有细节都做到了完美。唯一的缺点是没有给测试函数,只能费劲写一遍。
顺序表
#include<stdio.h>
#include<stdlib.h>
#define maxsize 10
typedef int Position;
typedef struct LNode {
int data[maxsize];
Position last;
}List,*pList;
pList empty()
{
pList L;
L = (pList)malloc(sizeof(List));
L->last = -1;
return L;
}
Position find(pList L, int X)
{
Position i = 0;
while (i <= L-> last && L->data[i] != X)
{
if (i > L->last) return -1;
else return i;
}
}
int insert(pList L, int X, Position p)
{
Position i;
if (L->last == maxsize - 1)
{
printf("表满");
return 0;
}
if (p<0 || p>L->last + 1)
{
printf("插入操作非法");
return 0;
}
for (i = L->last; i >= p; i--)//后移
{
L->data[i + 1] = L->data[i];
}
L->data[p] = X;
L->last++;
return 1;
}
int delete(pList L,Position p)
{
Position i;
if (L->last == -1)
{
printf("表空");
return 0;
}
if (p<0 || p>L->last)
{
printf("插入操作非法");
return 0;
}
for (i = p; i <= L->last; i++)//前移
{
L->data[i] = L->data[i + 1];
}
L->last--;
return 1;
}
void print(int* arr,int num)
{
for (int i = 0; i <= num; i++)
{
printf("%d ", arr[i]);
if (i == num)printf("\n");
}
}
int main()
{
pList L = empty();
for (int i = 0; i < maxsize-1; i++)
{
L->data[i] = i;
L->last = i;
}
print(L->data, L->last);
insert(L, 4, 9);
print(L->data, L->last);
delete(L, 1);
print(L->data, L->last);
}
栈
栈的顺序存储
#include<stdio.h>
#include<stdlib.h>
#define maxsize 5
typedef int position;
typedef struct {
int* data;
position top;
}stack,*pstack;
pstack creatstack()
{
pstack s = (pstack)malloc(sizeof(stack));
s->data = (int*)malloc(maxsize*sizeof(int));
s->top = -1;
return s;
}
int isfull(pstack s)//满了返回1,没满返回0
{
return (s->top == maxsize - 1);
}
int isempty(pstack s)//空的返回1,非空返回0
{
return (s->top == 0);
}
int push(pstack s,int x)
{
if (isfull(s))
{
printf("满了");
return 0;
}
else
{
s->top++;
s->data[s->top] = x;
return 1;
}
}
int pop(pstack s)
{
if (isempty(s))
{
printf("空的");
return 0;
}
else
{
return s->data[s->top--];
}
}
void print(pstack s)
{
for (int i=0; i <= s->top; i++)
{
printf("%d ",s->data[i]);
if (i == s->top)
printf("\n");
}
}
int main()
{
pstack s = creatstack();
for (int i = 0; i < maxsize; i++)
{
push(s, i);
}
print(s);
int out = pop(s);
print(s);
printf("弹出了%d\n", out);
out = pop(s);
print(s);
printf("弹出了%d\n", out);
}
栈的链式存储
#include<stdio.h>
#include<stdlib.h>
//一个只能头插头删的链表
typedef struct Node{
int data;
struct Node* next;
}node,*pnode;
pnode createstack()
{
pnode s = (pnode)malloc(sizeof(node));
s->next = NULL;
return s;
}
int isempty(pnode s)
{
return (s->next == NULL);
}
int isfull(pnode s)//链表栈满不了
{
}
int push(pnode s,int x)
{
pnode a = (pnode)malloc(sizeof(node));
a->data = x;
a->next = s->next;
s->next = a;
return 1;
}
int pop(pnode s)
{
if (isempty(s))
{
printf("栈空");
return 0;
}
pnode temp = s->next;
int elem = temp->data;
s->next = s->next->next;
free(temp);
return elem;
}
void print(pnode s)
{
if (isempty(s))
{
printf("栈空");
return 0;
}
pnode temp = s;
temp = temp->next;
while(1)
{
if (temp->next != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
else
{
printf("%d", temp->data);
break;
}
}
}
int main()
{
pnode s = createstack();
for (int i = 0; i < 10; i++)
{
push(s, i);
}
print(s);
}
列表
列表的顺序存储
列表的链式存储
嘻嘻嘻嘻大伙都休息了吧我还在卷还在卷嘻嘻嘻嘻
累死了列表明天再写