一、双向链表
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
int date;
struct node *next;
struct node *pri;
}node,*node_p;
node_p creat_double_link();
node_p creat_new_node(int date);
int empty_link(node_p H);
void insert_head(node_p H,int date);
void insert_tail(node_p H,int date);
void delete_head(node_p H);
void delete_tail(node_p H);
void insert_pos(node_p H,int pos,int date);
void delete_pos(node_p H,int pos);
void show_link(node_p H);
#endif
main.c
#include"head.h"
int main()
{
node_p H=creat_double_link();
insert_head(H,11);
insert_tail(H,22);
insert_tail(H,33);
insert_tail(H,44);
delete_head(H);
delete_tail(H);
insert_pos(H,2,55);
delete_pos(H,3);
show_link(H);
}
test.c
#include"head.h"
node_p creat_double_link()
{
node_p H=(node_p)malloc(sizeof(node));
if(H==NULL)
{
printf("fail to apply!\n");
return NULL;
}
H->next=NULL;
H->pri=NULL;
H->date=0;
return H;
}
node_p creat_new_node(int date)
{
node_p h=(node_p)malloc(sizeof(node));
if(h==NULL)
{
printf("new node fail to apply!\n");
return NULL;
}
h->date=date;
return h;
}
int empty_link(node_p H)
{
if(H==NULL)
{
printf("fail to apply!\n");
return -1;
}
return H==NULL?1:0;
}
void insert_head(node_p H,int date)
{
if(H==NULL)
{
printf("fail to apply!\n");
return;
}
node_p new=creat_new_node(date);
new->next=H->next;//插入的新节点,后继要保存它插入前头结点后的地址
if(H->next!=NULL)//只有头结点时,后面没有元素,无需连接前驱
{
H->next->pri=new;// 当已有元素时,需连接前驱,插入前该节点后移,插入后该节点的前驱要保存插入节点的地址
}
new->pri=H;//插入节点保存头节点地址
H->next=new;//更新头节点后继,使其保存下一个元素的地址(被插入节点)
H->date++;
}
void insert_tail(node_p H,int date)
{
if(H==NULL)
{
printf("fail to apply!\n");
return;
}
node_p new =creat_new_node(date);
node_p p=H;
while(p->next!=NULL)//找到最后一个节点
{
p=p->next;
}
new->next=NULL;
p->next=new;
new->pri=p;
H->date++;
}
void delete_head(node_p H)
{
if(empty_link(H))
{
printf("input empty,don't delete!\n");
return;
}
node_p del=H->next;//保存要删除的位置
H->next=H->next->next;
H->next->next->pri=H;
free(del);
H->date--;
}
void delete_tail(node_p H)
{
if(empty_link(H))
{
printf("input empty,don't delete!\n");
return;
}
node_p p=H;
while(p->next->next!=NULL)
{
p=p->next;
}
node_p del=p->next;
p->next=NULL;
free(del);
H->date--;
}
void insert_pos(node_p H,int pos,int date)
{
if(empty_link(H))
{
printf("input empty,don't inserting!\n");
return;
}
if(pos<=0||pos>H->date+1)
{
printf("input pos error!\n");
return;
}
node_p p=H;
node_p new=creat_new_node(date);
for(int i=0;i<pos-1;i++)
{
p=p->next;
}
new->next=p->next;
if(p->next!=NULL)//在最后插入,相当于尾插,此时插入元素后面没有元素,此条无需连接
{
p->next->pri=new;
}
p->next=new;
new->pri=p;
H->date++;
}
void delete_pos(node_p H,int pos)
{
if(empty_link(H))
{
printf("input empty,don't delete!\n");
return;
}
if(pos<=0||pos>H->date)
{
printf("input pos error!\n");
return;
}
node_p p=H;
for(int i=0;i<pos-1;i++)
{
p=p->next;
}
node_p del=p->next;
if(p->next->next!=NULL)//最后一个元素相当于尾删,无需连接后面的元素
{
p->next->next->pri=p;
}
p->next=p->next->next;
free(del);
H->date--;
}
void show_link(node_p H)
{
if(empty_link(H))
{
printf("input empty,don't ouput!\n");
return;
}
node_p p=H;
while(p->next!=NULL)
{
printf("%d->",p->next->date);
p=p->next;
}
printf("NULL\n");
}
运行结果
二、顺序栈
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include<stdio.h>
#include<stdlib.h>
#define MAX 8
typedef struct stack
{
int top;
int date[MAX];
}seq_stack,*seq_p;
seq_p creat_stack();//创建
int empty_stack(seq_p S);//判空
int full_stack(seq_p S);//判满
void push_stack(seq_p S,int date);//输入
void show_stack(seq_p S);//顺序输出
void free_stack(seq_p *S);//释放
void pop_stack(seq_p S);// 出栈
#endif
ubuntu
main.c
#include"head.h"
int main()
{
seq_p S=creat_stack();
push_stack(S,1);
push_stack(S,2);
push_stack(S,3);
show_stack(S);
pop_stack(S);
}
test.c
#include"head.h"
seq_p creat_stack()
{
seq_p S=(seq_p)malloc(sizeof(seq_stack));
if(S==NULL)
{
printf("fail to apply!\n");
return NULL;
}
S->top=-1;
return S;
}
int empty_stack(seq_p S)
{
if(S==NULL)
{
printf("fail to apply!\n");
return -2;
}
return S->top==-1?1:0;
}
int full_stack(seq_p S)
{
if(S==NULL)
{
printf("fail to apply!\n");
return -2;
}
return S->top==MAX-1?1:0;
}
void push_stack(seq_p S,int date)
{
if(full_stack(S))
{
printf("input full,no inputing!\n");
return;
}
S->top++;
S->date[S->top]=date;
}
void show_stack(seq_p S)//顺序要求与入栈一致
{
if(empty_stack(S))
{
printf("input empty,don't show!\n");
return;
}
printf("show_stack:");
for(int i=S->top;i>=0;i--)
{
printf("%3d",S->date[i]);
}
putchar(10);
}
void free_stack(seq_p *S)//传参的时候指针是不同的存储空间,所以用二级指针
{
if(S==NULL||*S==NULL)
{
printf("stack empty,don't free!\n");
return;
}
free(*S);
*S=NULL;
}
void pop_stack(seq_p S)
{
if(empty_stack(S))
{
printf("stack empty,don't pop!\n");
return;
}
printf("pop_stack:%d\n",S->date[S->top]);
S->top--;
}
运行结果:
思维导图: