1,双向链表的增删改查
头文件.h
#ifndef _LK_LKLIST__
#define _LK_LKLIST__
typedef int datatype;
typedef struct s
{
union{
int len;
datatype data;
}msg;
struct s* next;
struct s* prev;
}doublehead;
doublehead* ceatn_doublelist();
void insert_doublelist(doublehead *head,datatype a);
void shuchu_lklist(doublehead *head);
void add_headdoublelist(doublehead *head,datatype a);
void rm_headdoublelist(doublehead *head);
void rm_taildoublelist(doublehead *head);
void weizhi_rm_doublelist(doublehead *head,int index,datatype a);
void rm_weizhidoublelist(doublehead *head,int index);
#endif
实现双向链表的增删改查函数
#include <stdio.h>
#include <stdlib.h>
#include "./double_list.h"
/*
* function: 创建单循环链表
* @param [ in]
* @param [out]
* @return
*/
doublehead* ceatn_doublelist()
{
doublehead *head=(doublehead*)malloc(sizeof(doublehead));
if(head==NULL)
{
printf("创建头结点失败\n");
return NULL;
}
head->next=NULL;
head->prev=NULL;
head->msg.len=0;
return head;
}
/*
* function: 头插法
* @param [ in]
* @param [out]
* @return
*/
void insert_doublelist(doublehead *head,datatype a)
{
//创建新的结点
doublehead *temp=(doublehead*)malloc(sizeof(doublehead));
if(temp==NULL)
{
printf("创建头结点失败\n");
return;
}
temp->prev=NULL;
temp->next=NULL;
temp->msg.data=a;
//修改next
temp->next=head->next;
head->next=temp;
if(head->next->next==NULL)
{
temp->prev=head;
}
else
{
temp->prev=head->next->next->prev;
temp->next->prev=temp;
}
head->msg.data++;
return;
}
/*
* function: 头删
* @param [ in]
* @param [out]
* @return
*/
void rm_headdoublelist(doublehead *head)
{
//判空
if(head->next==NULL)
{
printf("双向链表为空\n");
return;
}
//备份
doublehead *temp=head->next;
head->next=temp->next;
//修改prev
if(temp->next!=NULL)
{
head->next->prev=head;
}
free(temp);
temp=NULL;
head->msg.len--;
return;
}
/*
* function: 尾插
* @param [ in]
* @param [out]
* @return
*/
void add_headdoublelist(doublehead *head,datatype a)
{
//创建新的结点
doublehead *temp=(doublehead*)malloc(sizeof(doublehead));
if(temp==NULL)
{
printf("创建头结点失败\n");
return;
}
temp->prev=NULL;
temp->next=NULL;
temp->msg.data=a;
//遍历找到最后一个
doublehead *p=head;
while(p->next!=NULL)
{
p=p->next;
}
//插入
temp->next=p->next;
p->next=temp;
temp->prev=p;
head->msg.len++;
return;
}
/*
* function: 尾删
* @param [ in]
* @param [out]
* @return
*/
void rm_taildoublelist(doublehead *head)
{
//判空
if(head->next==NULL)
{
printf("双向链表为空\n");
return;
}
//遍历找到最后一个
doublehead *p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->prev->next=NULL;
free(p);
head->msg.len--;
return;
}
/*
* function: 按位置插入
* @param [ in]
* @param [out]
* @return
*/
void weizhi_rm_doublelist(doublehead *head,int index,datatype a)
{
if(index<0||index>head->msg.len-1)
{
printf("插入位置非法\n");
return;
}
//创建新的结点
doublehead *temp=(doublehead*)malloc(sizeof(doublehead));
if(temp==NULL)
{
printf("创建头结点失败\n");
return;
}
temp->prev=NULL;
temp->next=NULL;
temp->msg.data=a;
doublehead *p=head;
for(int i=0;i<index-1;i++)
{
p=p->next;
}
temp->next=p->next;
p->next=temp;
temp->prev=p;
if(temp->next!=NULL)
{
temp->next->prev=temp;
}
head->msg.len++;
return;
}
/*
* function: 按位置删除
* @param [ in]
* @param [out]
* @return
*/
void rm_weizhidoublelist(doublehead *head,int index)
{
//判空
if(head->next==NULL)
{
printf("双向链表为空\n");
return;
}
if(index<0||index>head->msg.len-1)
{
printf("删除位置非法\n");
return;
}
doublehead *p=head;
for(int i=0;i<index;i++)
{
p=p->next;
}
p->prev->next=p->next;
if(p->next!=NULL)
{
p->next->prev=p->prev;
}
free(p);
head->msg.len--;
return;
}
/*
* function: 遍历
* @param [ in]
* @param [out]
* @return
*/
void shuchu_lklist(doublehead *head)
{
while(head->next!=NULL)
{
head=head->next;
printf("%-3d",head->msg.data);
}putchar(10);
return;
}
mekefile函数
运行结果
2,链式队列的入队,出队,遍历
头文件.h
#ifndef _LK_LKLIST__
#define _LK_LKLIST__
typedef int datatype;
typedef struct s
{
union{
int len;
datatype data;
}msg;
struct s* next;
}sequeue;
typedef struct
{
sequeue* front;
sequeue* rear;
}frea;
frea *ceatn_list();
void intput_linkqueue(frea *pp,datatype a);
datatype rm_headstack(frea *pp);
void show_stack(frea *pp);
#endif
实现链式队列的入栈,出栈函数
#include <stdio.h>
#include <stdlib.h>
#include "./link_stack.h"
/*
* function: 创建单循环链表
* @param [ in]
* @param [out]
* @return
*/
frea *ceatn_list()
{
frea *pp=(frea*)malloc(sizeof(frea));
pp->front=(sequeue*)malloc(sizeof(sequeue));
if(pp->front==NULL)
{
printf("队列创建失败\n");
return NULL;
}
pp->front->msg.len=0;
pp->front->next=NULL;
pp->rear=pp->front;
return pp;
}
/*
* function: 尾插入 入栈
* @param [ in]
* @param [out]
* @return
*/
void intput_linkqueue(frea *pp,datatype a)
{
sequeue *temp=(sequeue*)malloc(sizeof(sequeue));
if(temp==NULL)
{
printf("新的队列创建失败\n");
return;
}
temp->msg.data=a;
temp->next=NULL;
//找到尾结点
temp->next=pp->rear->next;
pp->rear->next=temp;
//更新rear
pp->rear=pp->rear->next;
pp->front->msg.len++;
return;
}
/*
* function: 判空
* @param [ in]
* @param [out]
* @return
*/
int ifNUll_stack(frea *pp)
{
return pp->front==pp->rear?1:0;
}
/*
* function: 头删 出栈
* @param [ in]
* @param [out]
* @return
*/
datatype rm_headstack(frea *pp)
{
if(ifNUll_stack(pp))
{
printf("队列为空\n");
return (datatype)-1;
}
//备份
sequeue *temp=pp->front->next;
pp->front->next=temp->next;
datatype num=temp->msg.data;
free(temp);
if(pp->front->next==NULL)
{
pp->rear=pp->front;
}
pp->front->msg.len--;
return num;
}
/*
* function: 遍历
* @param [ in]
* @param [out]
* @return
*/
void show_stack(frea* pp)
{
sequeue *p=pp->front;
while(p->next!=NULL)
{
p=p->next;
printf("%-3d",p->msg.data);
}putchar(10);
return;
}
makefile函数
运行结果
3,顺序队列的入栈,出栈,遍历
头文件.h
#ifndef __QUEUE__
#define __QUEUE__
#define N 10
typedef int datatpye;
typedef struct
{
int rear; //入队
int front; //出队
datatpye data[N]; //数据有效个数为N-1
}queue;
queue* create_queue();
void add_sequeue(queue *str,datatpye num);
void input_sequeue(queue *str);
void show_sequeue(queue *str);
int num_sequeue(queue *str);
#endif
实现顺序队列的入栈,出栈,遍历的函数
#include <stdio.h>
#include "./seq_queue.h"
#include <stdlib.h>
/*
* function: 创建队列---循环队列
* @param [ in]
* @param [out]
* @return
*/
queue* create_queue()
{
queue *str=(queue*)malloc(sizeof(queue));
if(NULL==str)
{
printf("创建队列失败\n");
return NULL;
}
str->rear=0;
str->front=0;
}
/*
* function: 判满
* @param [ in]
* @param [out]
* @return 1--满 0---未满
*/
int isfull_queue(queue *str)
{
return (str->rear+1)%N==str->front?1:0;
}
/*
* function: 判空
* @param [ in]
* @param [out]
* @return
*/
int isNULL_queue(queue *str)
{
return str->rear==str->front?1:0;
}
/*
* function: 入队
* @param [ in]
* @param [out]
* @return
*/
void add_sequeue(queue *str,datatpye num)
{
if(isfull_queue(str)==1)
{
printf("队列满了,无法插入\n");
return;
}
str->data[str->rear]=num;
str->rear=(str->rear+1)%N;
/*str->rear++;
if(str->rear==N-1)
str->rear=0;
*/
return;
}
/*
* function: 出队
* @param [ in]
* @param [out]
* @return
*/
void input_sequeue(queue *str)
{
if(isNULL_queue(str)==1)
{
printf("队列为空\n");
return;
}
while(str->front!=str->rear)
{
datatpye num=str->data[str->front];
str->front=(str->front+1)%N;
printf("出队:%-3d\n",num);
}
return;
}
/*
* function: 遍历 计算有效个数
* @param [ in]
* @param [out]
* @return
*/
void show_sequeue(queue *str)
{
int p=str->front;
int num=0;
while(p!=str->rear)
{
printf("%-3d",str->data[p]);
p=(p+1)%N;
num++;
}putchar(10);
printf("有效个数:%d\n",num);
return;
}
/*
* function: 计算有效个数
* @param [ in]
* @param [out]
* @return
*/
int num_sequeue(queue *str)
{
return (str->rear-str->front+N)%N;
}
运行结果
4,顺序栈的入栈,出栈,遍历
头文件.h
#ifndef __STACK__
#define __STACK__
typedef int datatpy;
#define N 10
typedef struct
{
int len;
datatpy data[N];
}stack;
stack* ceatn_stack();
void add_stack(stack *head,datatpy mun);
datatpy chu_stack(stack *head);
void show_stack(stack *head);
#endif
实现顺序栈的入栈,出栈,遍历的函数
#include <stdio.h>
#include <stdlib.h>
#include "./seq_stack.h"
/*
* function: 创建顺序栈
* @param [ in]
* @param [out]
* @return
*/
stack* ceatn_stack()
{
stack *head=(stack*)malloc(sizeof(stack));
if(head==NULL)
{
printf("栈头创建失败\n");
return NULL;
}
head->len=0;
return head;
}
/*
* function: 判满
* @param [ in]
* @param [out]
* @return 如果为满,返回1,未满返回0
*/
int isfull_stack(stack *head)
{
return head->len==N?1:0;
}
/*
* function: 入栈
* @param [ in]
* @param [out]
* @return
*/
void add_stack(stack *head,datatpy mun)
{
//判断满栈
if(isfull_stack(head)==1)
{
printf("满栈,无法插入\n");
return;
}
head->data[head->len]=mun;
head->len++;
return;
}
/*
* function: 出栈
* @param [ in]
* @param [out]
* @return
*/
datatpy chu_stack(stack *head)
{
if(head->len==0)
{
printf("空栈\n");
return (datatpy)-1;
}
datatpy p;
while(head->len!=0)
{
head->len--;
p=head->data[head->len];
printf("%-3d",p);
}
return p;
}
/*
* function: 遍历
* @param [ in]
* @param [out]
* @return
*/
void show_stack(stack *head)
{
for(int i=0;i<head->len;i++)
{
printf("%-3d",head->data[i]);
}putchar(10);
return;
}
运行结果