1,实现双向链表按元素删除
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;
LoopLink CreateHead();
LoopLink CreateNode();
void LoopDoubleLinkShow(LoopDoubleLink L);
int DeleteByPos(LoopDoubleLink L,int pos);
int InsertByPos(LoopDoubleLink L,int pos,datatype e);
int DeleteByData(LoopDoubleLink L,datatype e);
#endif
main.c
#include "head.h"
int main(int argc, const char *argv[])
{
LoopDoubleLink L=CreateHead();
if(L==NULL)
return -1;
//遍历
LoopDoubleLinkShow(L);
//双向链表按位置插入
int pos;
printf("输入插入的位置:");
scanf("%d",&pos);
printf("输入插入的数据元素:");
getchar();
scanf("%c",&e);
flag=InsertByPos(L,pos,e);
if(flag==0)
LoopDoubleLinkShow(L);
//双向链表按元素删除
printf("\n输入删除的元素:");
scanf(" %c",&e);
flag=DeleteByData(L,e);
if(flag==0)
LoopDoubleLinkShow(L);
else
printf("\n按元素删除失败\n");
free(L);
L=NULL;
return 0;
}
test.c
#include "head.h"
LoopLink CreateHead()
{
LoopLink L=(LoopLink)malloc(sizeof(Node));
if(L==NULL)
return NULL;
L->len=0;
L->next=L;
return L;
}
LoopLink CreateNode()
{
LoopLink p=(LoopLink)malloc(sizeof(Node));
if(p==NULL)
return NULL;
p->data=0;
p->next=NULL;
return p;
}
void LoopDoubleLinkShow(LoopDoubleLink L)
{
printf("\n正向打印:");
LoopDoubleLink p=L;
while(p->next!=L)
{
p=p->next;
printf("%.2f\t",p->data);
}
printf("\n逆向遍历:");
while(p!=L)
{
printf("%.2f\t",p->data);
p=p->prev;
}
printf("\n");
}
int DeleteByPos(LoopDoubleLink L,int pos)
{
if(L->len==0||pos<1||pos>L->len)
{
printf("删除失败\n");
return -1;
}
LoopDoubleLink p=L;
for(int i=0;i<pos-1;i++)
{
p=p->next;
}
LoopDoubleLink q=p->next;
p->next=q->next;
q->next->prev=p;
free(q);
q=NULL;
L->len--;
return 0;
}
//双向链表按位置插入
int InsertByPos(LoopDoubleLink L,int pos,datatype e)
{
if(L->len==0||pos<1||pos>L->len+1)
{
printf("插入失败\n");
return -1;
}
LoopDoubleLink p=L;
for(int i=0;i<pos-1;i++)
{
p=p->next;
}
LoopDoubleLink q=CreateNode();
if(q==NULL)
{
printf("新节点创建失败\n");
return -1;
}
q->data=e;
q->next=p->next;
q->prev=p;
p->next->prev=q;
p->next=q;
L->len++;
return 0;
}
//双向链表按元素删除
int DeleteByData(LoopDoubleLink L,datatype e)
{
if(L->len==0)
return -1;
LoopDoubleLink p=L;
for(int i=1;i<=L->len;i++)
{
p=p->next;
if(p->data==e)
{
DeleteByPos(L,i);
return 0;
}
}
return -1;
}3,实现约瑟夫环
4,删除链表中的所有结点
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 *next;
}*LoopLink,Node;
LoopLink CreateHead();
LoopLink CreateNode();
void LoopLinkShow(LoopLink L);
void InsertRear(LoopLink L,datatype e);
void josepho(LoopLink L,int m,int n);
void DeleteByAll(LoopLink L);
#endif
main.c
#include "head.h"
int main(int argc, const char *argv[])
{
LoopLink L=CreateHead();
if(L==NULL)
{
printf("创建失败\n");
return -1;
}
//约瑟夫环
LoopLink L=CreateHead();
if(L==NULL)
{
printf("创建失败\n");
return -1;
}
//尾插:永远在最后一个节点的后面插入
int n,m;//n表示节点的总个数,m表示几个一出圈
printf("输入一共有多个人:");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
datatype e;
scanf("%d",&e);
InsertRear(L,e);
}
LoopLinkShow(L);
//约瑟夫环
printf("输入几个一出圈:");
scanf("%d",&m);
josepho(L,m,n);
//释放单向循环链表空间
free(L);
L=NULL;
return 0;
}
test.c
#include "head.h"
LoopLink CreateHead()
{
LoopLink L=(LoopLink)malloc(sizeof(Node));
if(L==NULL)
return NULL;
L->len=0;
L->next=L;
return L;
}
LoopLink CreateNode()
{
LoopLink p=(LoopLink)malloc(sizeof(Node));
if(p==NULL)
return NULL;
p->data=0;
p->next=NULL;
return p;
}
void LoopLinkShow(LoopLink L)
{
printf("\n");
LoopLink p=L;
while(p->next!=L)
{
p=p->next;
printf("%d\t",p->data);
}
printf("\n");
}
//尾插
void InsertRear(LoopLink L,datatype e)
{
if(L==NULL)
return;
LoopLink q=L;
while(q->next!=L)
{
q=q->next;
}
LoopLink p=CreateNode();
if(p==NULL)
return;
p->data=e;
p->next=q->next;
q->next=p;
L->len++;
}
//约瑟夫环
void josepho(LoopLink L,int m,int n)
{
int i,j;
LoopLink p=L;
for(i=0;i<n;i++)
{
for(j=0;j<m-1;j++)
{
p=p->next;
if(p==L)
j--;
}
LoopLink q=p->next;
if(q==L)
q=L->next;
printf("%d\t",q->data);
p->next=q->next;
free(q);
q=NULL;
}
}
//删除链表中的所有结点
void DeleteByAll(LoopLink L)
{
if(L->len==0)
{
printf("删除失败\n");
return -1;
}
LoopLink p=L;
for(i=0;i<L->len;i++)
{
p=p->next;
LoopLink q=p->next;
p->next=q->next;
free(q);
q=NULL;
}
}5,实现单向链表的简单选择排序
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <stdlib.h>
typedef char datatype;
typedef struct node
{
union{
int len;
datatype data;
};
struct node *next;
}*Linklist,Node;
Linklist LinklistCreateHead();
void LinklistShow(Linklist L);
void LinklistSelectSort(Linklist L);
#endif
main.c
#include "head.h"
int main(int argc, const char *argv[])
{
int flag;
Linklist L=LinklistCreateHead();
if(L==NULL)
return -1;
LinklistSelectSort(L);
LinklistShow(L);
free(L);
L=NULL;
return 0;
}
test.c
#include "head.h"
Linklist LinklistCreateHead()
{
Linklist L=(Linklist)malloc(sizeof(Node));
if(L==NULL)
return NULL;
L->len=0;
L->next=NULL;
return L;
}
void LinklistShow(Linklist L)
{
while(L->next)//while(p->next!=NULL)
{
L=L->next;
printf("%c\t",L->data);
}
}
void LinklistSelectSort(Linklist L)
{
datatype t;
Linklist min=L;
Linklist j;
while(L->next)
{
if(min->data>L->next->data)
{
min->L->next;
}
if(min->data!=j->data)
{
t=min->data;min->data=j->data;j->data=t;
}
L=L->next;
}
}
本文展示了如何使用C语言实现双向链表的按元素删除和按位置插入功能,以及循环双向链表的按位置插入。此外,还涵盖了约瑟夫环问题的解决方案和单向链表的选择排序算法。代码包括头文件定义、主函数和测试函数,涵盖了数据结构的基本操作和算法应用。
286

被折叠的 条评论
为什么被折叠?



