1.实现双向链表按元素插入
head…h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <stdlib.h>
typedef int datatype;
typedef struct node
{
//数据域
union{
int len;
datatype data;
};
struct node *prev;
struct node *next;
}*DoubleLink,Node;
DoubleLink CreateHead();
DoubleLink CreateNode();
void InsertHead(DoubleLink L,datatype e);
void InsertRear(DoubleLink L,datatype e);
void DoubleLinkShow(DoubleLink L);
//暗元素删除
void DeleteByData(DoubleLink L,datatype x);
#endif
test.c
#include "head.h"
DoubleLink CreateHead()
{
DoubleLink L=(DoubleLink)malloc(sizeof(Node));
if(L==NULL)
return NULL;
//成功
L->len=0;
L->prev=NULL;
L->next=NULL;
return L;
}
//创建普通结点
DoubleLink CreateNode()
{
DoubleLink p=(DoubleLink)malloc(sizeof(Node));
if(p==NULL)
return NULL;
p->data=0;//给头结点的数据域赋值
p->prev=NULL;
p->next=NULL;
return p;
}
//头插
void InsertHead(DoubleLink L,datatype e)
{
//1.判断链表是否存在
if(L==NULL)
return;
//2.在L和L->next支架存入p
DoubleLink p=CreateNode();
if(p==NULL)
return;
//数据域
p->data=e;
//指针域
p->next=L->next;
p->prev=L;
if(L->next!=NULL)
{
L->next->prev=p;
}
L->next=p;
L->len++;
}
//尾插
void InsertRear(DoubleLink L,datatype e)
{
if(L==NULL)
return;
DoubleLink q=CreateNode();
if(q==NULL)
return;
q->data=e;
//指针域
DoubleLink p=L;
while(p->next)
{
p=p->next;
}
p->next=q;
q->prev=p;
L->len++;
}
//遍历
void DoubleLinkShow(DoubleLink L)
{
printf("\n正向打印:");
DoubleLink p=L;
while(p->next)
{
p=p->next;
printf("%d\t",p->data);
}
printf("\n");
}
//按元素删除
void DeleteByData(DoubleLink L,datatype x)
{
if(L->len==0)
printf("删除失败!!!");
DoubleLink p=L;
while(p->next)
{
p=p->next;
if(p->data==x)
break;
}
p->prev->next=p->next;
p->next=p->prev;
L->len--;
free(p);
p=NULL;
}
main.c
#include "head.h"
int main(int argc, const char *argv[])
{
DoubleLink L=CreateHead();
if(L==NULL)
{
printf("创建失败!!!");
return -1;
}
//插入
int n;
printf("插入几个值?");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
datatype e;
scanf("%d",&e);
InsertRear(L,e);
}
DoubleLinkShow(L);
//按元素删除
printf("请输入要删除的元素:");
datatype x;
scanf("%d",&x);
DeleteByData(L,x);
DoubleLinkShow(L);
return 0;
}
结果:
2.双向循环链表(包括循环双向链表按位置插入)
head.h
#ifndef __Head_H__
#define __Head_H__
#include <stdio.h>
#include <stdlib.h>
typedef float datatype;
typedef struct node
{
//数据域
union{
int len;
datatype data;
};
//指针域
struct node *prev;
struct node *next;
}*LoopDoubleLink,Node;
}
LoopDoubleLink CreateHead();
LoopDoubleLink CreateNode();
void InsterHead(LoopDoubleLink L,datatype e);
void LoopDoubleLinkShow(LoopDoubleLink L);
void InsertRear(LoopDoubleLink L,datatype e);
void DeleteHead(LoopDoubleLink L);
void DeleteRear(LoopDoubleLink L);
void InsertByPot(LoopDoubleLink L,datatype e,int n);
#endif
test.c
#include "head.h"
//创建头结点
//成功返回头结点,失败返回NULL
LoopDoubleLink CreateHead()
{
LoopDoubleLink L=(LoopDoubleLink)Malloc(sizeof(Node));
if(L==NULL)
return NULL;
//成功返回头结点
L->len=0;
L->prev=L;
L->next=L;
return L;
}
//创建普通结点
LoopDoubleLink CreateNode()
{
LoopDoubleLink p=(LoopDoubleLink)Malloc(sizeof(Node));
if(p==NULL)
return NULL;
//成功返回头结点
p->data=0;
p->prev=NULL;
p->next=NULL;
return p;
}
//双向循环链表头插
void InsterHead(LoopDoubleLink L,datatype e)
{
//1.判断L是否存在
if(L==NULL)
return;
//2.创建新结点
LoopDoubleLink p;
p->data=e;
p->next=L->next;
p->prev=L;
L->next->prev=p;
L->next=p;
}
//遍历循环双向链表
void LoopDoubleLinkShow(LoopDoubleLink L)
{
printf("\n正向打印");
LoopDouble p=L;
while(p->next!=L)
{
p=p->next;
printf("%.2f\t",p->data);
}
printf("\n逆向遍历");
while(p!=L)
{
p=p->prev;
printf("%.2f\t",p->data);
}
}
//双向循环链表尾插
void InsertRear(LoopDoubleLink L,datatype e)
{
//1.判断链表是否存在
if(L==NULL)
return;
//2.找到最后一个节点
LoopDoubleLink p=L->prev;
//3.创建新结点
LoopDoubleLink q=CreateNode();
if(q==NULL)
return;
q->data=e;
q->next=p->next;
q->prev=p;
p->next->prev=q;
p->next=q;
L->len++;
}
//双向循环链表头删:永远删除第一个结点
void DeleteHead(LoopDoubleLink L)
{
//1.判空
if(L->len==0)
{
printf("表头删除失败!!!");
return;
}
//2.删除头结点的后继结点
LoopDoubleLink p=L->next;
L->next=p->next;
p->next->prev=L;
L->len--;
free(p);
p=NULL;
}
//双向循环链表尾删:永远删除最后一个结点
void DeleteRear(LoopDoubleLink L)
{
//1.判空
if(L->len==0)
{
printf("尾删失败!!!");
return;
}
//2.删除最后与各结点
LoopDoubleLink p=L->prev;
L->prev=p->prev;
p->prev->next=L;
L->len--;
free(p);
p=NULL;
}
//双向循环链表按位置插入
void InsertByPot(LoopDoubleLink L,datatype e,int n)
{
//1.判空
if(L->len==0)
{
printf("插入失败!!!");
return;
}
//2.创建新的结点
datatype p=CreateNode();
datatype q=L;
p->data=e;
for(int i=0;i<n;i++)
{
q=q->next;
}
q->prev->next=p;
p->next=q;
L->len++;
}
main.c
#include "head.h"
int main(int argc, const char *argv[])
{
LoopDoubleLink L=CreateHead();
if(L==NULL)
return -1;
//头插:永远在第一个结点的后面插入
for(int i=0;i<5;i++)
{
datatype e;
scanf("%f",&e);
InsterHead(L,e);
}
#if 0
//尾插
for(int i=0;i<L->len;i++)
{
datatyoe e1;
scanf("%f",&e);
InsertRear(L);
}
#endif
//遍历循环双向链表
LoopDoubleLinkShow(L);
#if 0
//循环链表头删:
DeleteHead(L);
LoopDoubleLink(L);
//循环链表尾删
DeleteRear(L);
LoopDoubleLink(L);
//循环链表按位置插入
int n;
printf("请输入要插入的位置:");
scanf("%d",&n);
getchar();
datatype e2;
Printf("请输入要插入的值");
scanf("%.2f",&e2);
getchar();
InsertBypot(L,r2,n);
#endif
return 0;
}
3.实现约瑟夫环
n人围成一圈,从某人开始数,数到m的人出圈,然后从下一个人开始重复此过程,指导全部人出圈,于是得到出圈人员的新序列
4.删除链表中的所有结点
5.实现单向链表中的简单选择排序