Fun.h //头文件声明等
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct student{
int num;
struct student *pnext;
}Node,*pNode;
void list_sort_insret(pNode*,pNode*,int);
void list_out(pNode);
void list_del(pNode*,pNode*,int);
void list_con(pNode*,pNode*,pNode*);
void list_rec(pNode*);
int list_zhao(pNode);
void list_zhong(pNode);
void list_shanchong(pNode*);
List.c //被调函数文件放置区,实际操作分成两个放的,不然太多了
#include"func.h"
void list_sort_insret(pNode *pphead,pNode *pptail,int i) //对输入的元素进行快速排序并存入单链表
{
pNode pnew=(pNode)malloc(sizeof(Node));
pNode pcur=*pphead;
pNode ppre=*pphead;
memset(pnew,0,sizeof(Node));
pnew->num=i;
if(NULL==*pphead)
{
*pphead=pnew;
*pptail=pnew;
}else if(pcur->num>i){
pnew->pnext=*pphead;
*pphead=pnew;
}else {
while(pcur->num<i)
{
ppre=pcur;
pcur=pcur->pnext;
if(NULL==ppre->pnext)
break;
}
if(NULL!=ppre->pnext)
{
ppre->pnext=pnew;
pnew->pnext=pcur;
}
else{
ppre->pnext=pnew;
*pptail=pnew;
}
}
}
void list_out(pNode phead)
{
while(phead!=NULL)
{
printf("%d ",phead->num);
phead=phead->pnext;
}
printf("\n");
}
void list_del(pNode *pphead,pNode *pptail,int i) //删除指定的某一元素
{
pNode pcur=*pphead;
pNode ppre=*pphead;
if(pcur->num==i)
{
*pphead=(*pphead)->pnext;
free(pcur);
}else{
while(pcur->num!=i)
{
ppre=pcur;
pcur=pcur->pnext;
if(ppre->pnext==NULL)
{
printf("没有你要删除的元素\n");
}
}
ppre->pnext=pcur->pnext;
free(pcur);
if(ppre->pnext==NULL)
{
*pptail=ppre;
}
}
}
void list_con(pNode *pphead,pNode *pptail,pNode *pphead1) //把两个有序的链表合并
{
pNode pcur=*pphead;
pNode ppre=*pphead;
pNode pcur1=*pphead1;
pNode ppre1=*pphead1;
do{
list_sort_insret(pphead,pptail,pcur1->num);
ppre1=pcur1;
pcur1=pcur1->pnext;
free(ppre1);
}while(pcur1!=NULL);
}
void list_rec(pNode *pphead) //倒置单链表
{
pNode a=(*pphead)->pnext;
pNode q=*pphead;
pNode head=*pphead;
while(q->pnext!=NULL)
{
q->pnext=q->pnext->pnext;
a->pnext=head;
head=a;
a=q->pnext;
}
*pphead=head;
}
int list_zhao(pNode phead) //查找倒数第四个数,前面的int可用void替换
{
pNode q=phead;
pNode p=phead->pnext->pnext->pnext;
if(p!=NULL)
{
while(p->pnext!=NULL)
{
p=p->pnext;
q=q->pnext;
}
printf("倒数第四个数为%d\n",q->num);
}
}
void list_zhong(pNode phead) //查找中间的数,如果是偶数个则为中间两个数的后者
{
pNode q=phead;
pNode p=phead;
while(p->pnext!=NULL)
{
q=q->pnext;
p=p->pnext->pnext;
}
printf("中间的数为: %d\n",q->num);
}
void list_shanchong(pNode *pphead) //删除单链表中所有重复的数
{
pNode pcur=*pphead;
pNode ppre=*pphead;
while(pcur->pnext!=NULL)
{
ppre=pcur;
pcur=pcur->pnext;
if(ppre->num==pcur->num)
{
pcur=pcur->pnext;
free(ppre->pnext);
ppre->pnext=pcur;
}
}
}
Main.c //main函数文件,一系列主线操作的控制
#include"func.h"
int main()
{
int i;
pNode phead=NULL;
pNode ptail=NULL;
pNode phead1=NULL;
pNode ptail1=NULL;
printf("请输入第一个链表:\n");
while(scanf("%d",&i)!=EOF)
{
list_sort_insret(&phead,&ptail,i);
}
printf("请输入第二个链表:\n");
while(scanf("%d",&i)!=EOF)
{
list_sort_insret(&phead1,&ptail1,i);
}
list_con(&phead,&ptail,&phead1);
printf("合并后的链表为:\n");
list_out(phead);
printf("请输入要删除的数\n");
scanf("%d",&i);
list_del(&phead,&ptail,i);
printf("删除后的链表为:\n");
list_out(phead);
printf("逆置后为:\n");
list_rec(&phead);
list_out(phead);
list_zhao(phead);
list_zhong(phead);
list_shanchong(&phead);
printf("删除重复元素后:\n");
list_out(phead);
system("pause");
return 0;
最终的结果演示
新增两个基本操作
void list_top_insert(pNode* phead,pNode* ptail,int i) /头插法,自己写的可能不够精简
{
pNode pnew=(pNode)malloc(sizeof(Node));
memset(pnew,0,sizeof(Node));
pnew->i=i;
if(*phead==NULL)
{
*phead=pnew;
*ptail=pnew;
}else{
pnew->pnext=*phead;
*phead=pnew;
void list_last_insert(pNode* phead,pNode* ptail,int i) //尾插法,一样。。。
{
pNode pnew=(pNode)malloc(sizeof(Node));
memset(pnew,0,sizeof(Node));
pnew->i=i;
if(*phead==NULL)
{
*phead=pnew;
*ptail=pnew;
}else{
(*ptail)->pnext=pnew;
*ptail=pnew;
}
}