1:队列
#include <stdio.h>
#include <stdlib.h>
#include "./02队列.h"
/*
* function: 创建一个空的队列
* @param [ in]
* @param [out]
* @return
*/
Sequeue* xinduilie()
{
Sequeue* sq = (Sequeue*)malloc(sizeof(Sequeue));
if(NULL == sq)
{
printf("创建失败!!!\n");
}
sq->front = sq->rear = 0;
return sq;
}
/*
* function: 判满
* @param [ in]
* @param [out]
* @return
*/
int panman(Sequeue* sq)
{
return (sq->rear+1)%(N+1) == sq->front ? 1:0;
}
/*
* function: 入队
* @param [ in]
* @param [out]
* @return
*/
void rudui(Sequeue* sq,datatype num)
{
if(panman(sq))
{
printf("插入失败,队列已满\n");
return;
}
sq->data[sq->rear]= num;
sq->rear = (sq->rear+1)%(N+1);
printf("入队成功%d\n",num);
return;
}
/*
* function: 出队
* @param [ in]
* @param [out]
* @return
*/
void chudui(Sequeue* sq)
{
if(sq->front == sq->rear)
{
printf("队列为空!!!\n");
}
Sequeue* num = sq->data[sq->front];
printf("出队的值为%d\n",num);
sq->front = (sq->front+1)%N;
return;
}
/*
* function: 遍历
* @param [ in]
* @param [out]
* @return
*/
void bianli(Sequeue* sq )
{
for(int i=sq->front;i!=sq->rear;i=(i+1)%(N+1))
{
printf("%d ",sq->data[i]);
}
putchar(10);
return;
}
/*
* function: 判空
* @param [ in]
* @param [out]
* @return
*/
int pankon(Sequeue* sq)
{
return sq->rear==sq->front ? 1:0;
}
/*
* function: 出队
* @param [ in]
* @param [out]
* @return
datatype chudui(Sequeue* sq)
{
if(pankon(sq) == 0)
{
printf("出队失败,队列为空\n");
}
datatype num = sq->data[sq->front];
sq->front - (sq->front+1)%(N+1);
return num;
}
*/
/*
* function: 计算队列个数
* @param [ in]
* @param [out]
* @return
*/
datatype jisuan(Sequeue* sq)
{
return (sq->rear-sq->front+N+1)%(N+1);
}
头文件:
1 #ifndef __02队列_H__
2 #define __02队列_H__
3 #define N 5
4 typedef int datatype ;
5 typedef struct
6 {
7 int front;
8 int rear;
9 datatype data[N+1];
10 }Sequeue;
11 Sequeue* xinduilie();//创建一个新的队列
12 void rudui(Sequeue* sq,datatype num) ;//入队
13 void chudui(Sequeue* sq);//出队
14 int panman(Sequeue* sq);//判满
15 void bianli(Sequeue* sq );//遍历
16 int pankon(Sequeue* sq);//判空
17 datatype jisuan(Sequeue* sq);//计算个数
#endif
主函数:
#include <stdio.h>
#include "./02队列.h"
int main(int argc, const char *argv[])
{
Sequeue* sq = xinduilie();
rudui(sq,20);
rudui(sq,20);
rudui(sq,60);
rudui(sq,27);
rudui(sq,29);
chudui(sq);
bianli(sq);
printf("队列有效个数:%d\n",jisuan(sq));
return 0;
}
结果图:
2:链式队列:
功能代码
#include <stdio.h>
#include <stdlib.h>
#include "./03链式队列.h"
/*
* function: 创建一个空的链式队列
* @param [ in]
* @param [out]
* @return
*/
QueueOp* create_linkqueue()
{
QueueOp* fp = (QueueOp*)malloc(sizeof(QueueOp));
fp->front = (Lnkqueue*)malloc(sizeof(Lnkqueue));
if(fp->front == NULL)
{
printf("创建失败!\n");
return NULL;
}
//初始化,下标地址及有效长度
fp->front->next = NULL;
fp->front->msg.len = 0;
fp->rear = fp->front;
printf("创建成功!\n");
return fp;
}
/*
* function: 尾插
* @param [ in]
* @param [out]
* @return
*/
void del_list(QueueOp* head,datatype num)
{
Lnkqueue* temp = (Lnkqueue*)malloc(sizeof(Lnkqueue));
if(temp == NULL)
{
printf("创建失败,节点为空\n");
return;
}
//给链表插入值
temp->msg.data = num;
temp->next = NULL;
temp->next = head->rear->next;
head->rear->next = temp;
//更新rear指向
head->rear = head->rear->next;
head->front->msg.len++;
printf("插入成功%d\n",num);
return;
}
/*
* function: 判空
* @param [ in]
* @param [out]
* @return
*/
int isEmpty_list(QueueOp* head)
{
return head->front == head->rear ? 1:0;
}
/*
* function: 出栈
* @param [ in]
* @param [out]
* @return
*/
datatype output_list (QueueOp* head)
{
if(isEmpty_list(head))
{
printf("队列为空,出队失败\n");
return (datatype) -1;
}
Lnkqueue* temp = head->front->next;
head->front->next = temp->next;
datatype num = temp->msg.data;
if(head->front->next == NULL)
{
head->rear = head->front;
}
head->front->msg.len--;
return num;
free(temp);
}
/*
* function: 遍历
* @param [ in]
* @param [out]
* @return
*/
void bianli_list(QueueOp* head)
{
while(head->front->next != head->rear->next)
{
head->front = head->front->next;
printf("%d ",head->front->msg.data);
}
putchar(10);
return;
}
主函数
#include <stdlib.h>
#include "./03链式队列.h"
int main(int argc, const char *argv[])
{
QueueOp* head = create_linkqueue();
del_list(head,10);
del_list(head,20);
del_list(head,30);
del_list(head,40);
printf("%d\n",output_list(head));
bianli_list(head);
return 0;
}
头文件函数
#ifndef __03链式队列_H__
#define __03链式队列_H__
typedef int datatype;
typedef struct lnkq
{
union
{
int len;
datatype data;
}msg;
struct lnkq *next;
}Lnkqueue;
//用于存储头结点尾节点地址
typedef struct
{
Lnkqueue* front;
Lnkqueue* rear;
}QueueOp;
QueueOp* create_linkqueue();//创建链式列表成功
void del_list(QueueOp* head,datatype num);//尾插
datatype output_list (QueueOp* head);//出栈
void bianli_list(QueueOp* head);//遍历
#endif
结果图:
3:双向链表
功能代码:
include <stdlib.h>
include <stdio.h>
include "./02双向链表.h"
*
* function: 创建一个双向链表
* @param [ in]
* @param [out]
* @return
*/
oublelist* lianbiao()
Doublelist* twolianbiao = (Doublelist* )malloc(sizeof(Doublelist));
if(NULL == twolianbiao)
{
printf("创建失败,链表为空\n");
}
//初始化双向链表
twolianbiao->next = NULL;
twolianbiao->prev = NULL;
twolianbiao->msg.len = 0;
return twolianbiao;
*
* function: 头插
* @param [ in]
* @param [out]
* @return
*/
oid headcha(Doublelist* head,datatype num)
Doublelist* temp = (Doublelist*)malloc(sizeof(Doublelist));
if(NULL == temp)
{
printf("插入失败,创建新结点失败!!!\n");
}
temp->msg.data = num; //给temp赋值
temp->next = head->next; //修改next指向
head->next = temp;
temp->prev = head;//修改prev指向
if(temp->next != NULL)
{
temp->next->prev = temp;
}
printf("头插入成功=%d\n",num);
head->msg.len++;
return;
*
* function: 尾插
* @param [ in]
* @param [out]
* @return
*/
oid ledcha (Doublelist* head,datatype num)
Doublelist* temp =(Doublelist*)malloc(sizeof(Doublelist));
temp->msg.data = num;//创建新节点并存入值
temp->next = NULL;
Doublelist* p = head;
while(p->next != NULL)
{
p=p->next;
}
temp->next=p->next;
p->next = temp;//更改next的指向
temp->prev = p;//更改prev的指向
printf("尾插入成功=%d\n",num);
head->msg.len++;
return;
*
* function: 头删
* @param [ in]
* @param [out]
* @return
*/
oid headshan(Doublelist* head)
if(head->next == NULL)
{
printf("删除失败,链表为空!!!\n");
}
Doublelist* p=head;
Doublelist* temp = p->next;
datatype num = temp->msg.data;
p->next=temp->next;//修改next的指向
if(temp->next != NULL)
{
temp->next->prev = p;
}
printf("删除的值=%d\n",temp->msg.data);
free(temp);
temp->next = NULL;
head->msg.len--;
*
* function: 尾删
* @param [ in]
* @param [out]
* @return
*/
oid ledshan(Doublelist* head)
if(head->next == NULL)
{
printf("删除失败,链表为空!!!\n");
}
Doublelist* p = head;
while(p->next != NULL)
{
p = p->next;
}
datatype num = p->msg.data;
p->prev->next = p->next;
printf("删除的值=%d\n",num);
free(p);
head->msg.len--;
return;
*
* function: 判空
* @param [ in]
* @param [out]
* @return
*/
nt pankon(Doublelist* head)
return head->next == NULL ? 1:0 ;}
*
* function: 中间插入
* @param [ in]
* @param [out]
* @return
*/
oid middlecha(Doublelist* head,datatype num,int n)
if(n<=0)
{
printf("插入失败,插入的位置非法!!!\n");
return;
}
Doublelist* p = head;
for(int i=0;i<n-1;i++)
{
p=p->next;
printf("%d\n",head->msg.len);
if(NULL == p)
{
printf("插入位置非法!!!\n");
return;
}
}
Doublelist* temp=(Doublelist*)malloc(sizeof(Doublelist));
if(NULL == temp)
{
printf("创建失败!,插入失败!!!\n");
return;
}
temp->msg.data = num;
temp->next = NULL;
temp->prev = NULL;
temp->next = p->next;
p->next = temp;
temp->prev = p;
if(temp->next != NULL)
{
temp->next->prev = temp;
}
printf("在第%d个位置插入的值是%d\n",n,num);
head->msg.len++;
return;
}
/*
* function: 中间删除
* @param [ in]
* @param [out]
* @return
*/
void middleshan(Doublelist* head,int n)
{
if(head->next ==NULL || head == NULL)
{
printf("删除失败,链表为空!!!%d\n");
return;
}
if(n <= 0);
{
printf("删除位置非法!!!%d\n",n);
return;
}
Doublelist* p = head;
for(int i=0;i<n;i++)
{
p=p->next;
if(NULL == p)
{
printf("%d删除位置非法!!!\n",n);
return;
}
}
p->prev->next = p->next;
if(p->next != NULL)
{
p->next->prev = p->next;
}
datatype num = p->msg.data;
free(p);
head->msg.len --;
return;
}
头文件:
#ifndef __02双向链表_H__
#define __02双向链表_H__
typedef int datatype;
typedef struct doublelist
{
union
{
int len;
datatype data;
}msg;
struct doublelist* next;
struct doublelist* prev;
}Doublelist;
Doublelist* lianbiao();//创建一个空的双向链表
void headcha(Doublelist* head,datatype num);//头插
void ledcha (Doublelist* head,datatype num);//尾插
void headshan(Doublelist* head);//头删
void ledshan(Doublelist* head);//尾删
void middlecha(Doublelist* head,datatype num,int n);//中间插入
void middleshan(Doublelist* head,int n);//中间删除
#endif
主函数main:
#include <stdio.h>
#include "./02双向链表.h"
int main(int argc, const char *argv[])
{
Doublelist* head = lianbiao();
headcha(head,10);
headcha(head,10);
ledcha(head,90);
headshan(head);
ledshan(head);
ledcha(head,80);
ledcha(head,60);
middlecha(head ,50,3);
middleshan(head,2);
return 0;
}