您的位置:首页 > 编程语言 > C语言/C++

c语言实现链表的相关的操作

2017-03-07 22:21 429 查看
最近复习了一下C语言数据结构的内容,实现了C语言链表的相关的操作,链表采用的是带有头结点的链表,个人觉得这种方式操作链表的时候比较的方便,对于插入或删除首节点的时候比较的方便。

#include<stdio.h>
#include <malloc.h>
#include<stdlib.h>
#include <math.h>

typedef struct Node
{
int data; //数据域
struct Node *pNode; //指针域

}NODE,*PNODE;

//函数申明
struct Node* creat_list(); //创建一个链表
void traverse_list(struct Node* pHead) ; //将链表遍历输出
void nixutraverse_list(struct Node* pHead) ; //将链表遍历输出
void maxsort_list(struct Node*pHead); //将链表按照从大到小的顺序排列
void minsort_list(struct Node*pHead); //将链表按照从小到大的顺序排列
void clear_list(struct Node*); //将链表清空
void destroy_list(struct Node*pHead);
bool delete_list(struct Node*pHead,int,int*); //删除链表的元素
bool insert_list(struct Node*pHead,int,int); //在链表中插入一个元素//在pHead所指向的链表的第pos个节点前面插入一个新的节点,该节点的值是val,并且pos的值从1
bool is_empty(struct Node*pHead); //判断链表是否为空
int find(struct Node*pHead,int); //查找链表中的元素
struct Node* delete_list(struct Node*pHead,int pos,int num);
//1开始

int length_list(struct Node*pHead); //链表的长度
void clear_zero(struct Node*pHead);
int main()
{
int num;
struct Node* pHead =NULL;
pHead=creat_list(); //创建一个非循环的单链表,并将该链表的头节点的地址付给PHEAD
//maxsort_list(pHead);
//destroy_list(pHead);
traverse_list(pHead);
//clear_list(pHead);

if(is_empty(pHead))
printf("链表为空\n");
else
printf("链表不为空\n");
int a=length_list(pHead);
printf("链表的长度为%d\n",a);
int c,d;
scanf("%d%d",&c,&d);
printf("你将要在第%d的位置上插入数%d\n",c,d);
if(insert_list(pHead,c,d))
printf("添加成功\n");
else
printf("添加失败\n");
traverse_list(pHead);
printf("\n");
int pos,s,n;
scanf("%d",&pos);
printf("你将要删除第%d个元素",pos);
if(delete_list(pHead,pos,&num))
printf("删除成功,删除了%d\n",num);
else
printf("删除失败\n");
//minsort_list(pHead);
//maxsort_list(pHead);
nixutraverse_list(pHead);
traverse_list(pHead);
struct Node *p=pHead->pNode;
printf("%d\n",p->data);
printf("输入你要查找的元素:");
scanf("%d",&n);
s=find(pHead,n);
if(s==-1)
printf("查找失败,没有这个元素");
else
printf("你要查找的元素是滴%d个元素",s+1);
clear_zero(pHead);
traverse_list(pHead);
return 0;
}

//创建一个链表
struct Node* creat_list()
{
int len; //用来存放有效节点的个数
int val; //用来临时存放用户输入节点的值
printf("请输入您要生成的链表节点的个数len=");
scanf("%d",&len);

struct Node* pHead =(struct Node*)malloc(sizeof(struct Node));

struct Node*pTail=pHead;
pTail->pNode=NULL;

i
4000
f(NULL==pHead)
{
printf("分配内存失败,程序终止\n");
exit(-1);
}
for(int i=0;i<len;i++)
{
printf("请输入第%d个节点的值:",i+1);
scanf("%d",&val);
//为每一个节点分配内存
struct Node* pnew =(struct Node*)malloc(sizeof(struct Node));
if(NULL==pnew)
{
printf("分配内存失败,程序终止\n");
exit(-1);
}
pnew->data=val;
pTail->pNode=pnew;
pnew->pNode=NULL;
pTail=pnew;
}
return pHead;
}

//链表的正序输出
void traverse_list(struct Node* pHead)
{
struct Node *p=pHead->pNode;
while(NULL!=p)
{
printf("%d ",p->data);
p=p->pNode;
}
printf("\n");
}

//链表的逆序输出
void nixutraverse_list(struct Node* pHead)
{
struct Node * current;
struct Node *p;
current=pHead->pNode;
while(current->pNode!=NULL)
{
p=current->pNode;
current->pNode=p->pNode;
p->pNode=pHead->pNode;
pHead->pNode=p;
}
}

//判断链表是否为空
bool is_empty(struct Node*pHead)
{
if(pHead->pNode==NULL)
return true;
else
return false;
}

//计算链表的长度
int length_list(struct Node*pHead)
{
int cnt=0;
struct Node *p=pHead->pNode;
while(p!=NULL)
{
cnt++;
p=p->pNode;
}
return cnt;
}

//在pHead所指向的链表的第pos个节点前面插入一个新的节点,该节点的值是val,并且pos的值从1
//1开始
bool insert_list(struct Node*pHead,int pos,int val)
{

int c=0;
struct Node *p=pHead;
while(c<pos-1)
{
c++;
p=p->pNode;
}
if(c>pos-1 || NULL==p)
return false;
struct Node *r;
struct Node *q=(struct Node*)malloc(sizeof(struct Node));
if(NULL==q)
{
printf("分配内存失败,程序终止\n");
exit(-1);
}
q->data=val;
r=p->pNode;
p->pNode=q;
q->pNode=r;
return true;
}

bool delete_list(struct Node*pHead,int pos,int* val)//删除链表的元素
{
int c=0;
struct Node* p=pHead;
while(c<pos-1 && NULL!=p)
{
c++;
p=p->pNode;
}
if(c>pos-1 || NULL==p)
return false;
*val=p->pNode->data;
struct Node *r;
r=p->pNode;
p->pNode=p->pNode->pNode;
free(r);
return true;
}

//将链表按照从小到大的顺序排列
void maxsort_list(struct Node*pHead)
{
int i,j,t;
struct Node * p;
struct Node *q;
int len=length_list(pHead);
for(i=0,p=pHead->pNode;i<len-1;i++,p=p->pNode)
{
for(j=i+1,q=p->pNode;j<len;j++,q=q->pNode)
{
if(p->data>q->data)
{
t=p->data;
p->data=q->data;
q->data=t;
}
}
}
}

//将链表从大到小输出
void minsort_list(struct Node*pHead)
{
struct Node*p;
struct Node*q;
int i,j,t;
int len=length_list(pHead);
for(i=0,p=pHead->pNode;i<len-1;i++,p=p->pNode)
{
for(j=i+1;q=p->pNode;j++,q=q->pNode)
{
if(p->data<q->data)
t=p->data;
p->data=q->data;
q->data=t;
}
}
}

int find(struct Node*pHead,int val)
{
//maxsort_list(pHead);
int mid,low,high,i=0;
struct Node*p=pHead->pNode;
low=0;
high=length_list(pHead)-1;
while(low<=high)
{
mid=(low+high)/2;
while(i<mid)
{
p=p->pNode;
i++;
}
if(p->data>val)
low=mid+1;
else if(p->data<val)
high=mid-1;
else
return mid;
p=pHead->pNode;
i=0;
}
return -1;
}
void clear_list(struct Node*pHead)
{

if(is_empty(pHead))
return;
else
{
struct Node*p=pHead->pNode;
struct Node*q;
while(p!=NULL)
{
q=p->pNode;
free(p);
p=q;
}
}
pHead->pNode=NULL;
}
//线性表L已存在。操作结果:销毁线性表L
void destroy_list(struct Node*pHead)
{
struct Node *p=pHead->pNode;
struct Node *q;
while(q!=NULL)
{
q=p->pNode;
free(p);
p=q;
}

}

//删除链表中所有为k的节点
struct Node* delete_list(struct Node*pHead,int pos,int num)
{
struct Node* p=pHead,*q;
q=p->pNode;
while(q!=NULL){
if(q->data==pos)
{
p->pNode=q->pNode;
q=q->pNode;
}
else{
p=q;
q=q->pNode;
}
}
if(pos==pHead->data)
pHead=pHead->pNode;
return pHead;
}

void clear_zero(struct Node*pHead)
{
struct Node*p=pHead,*q;
q=p->pNode;
while(q!=NULL)
{
if(fabs(q->data)<=0.0000001)
{
p->pNode=q->pNode;
free(q);
q=p->pNode;
}
else
{
p=p->pNode;
q=q->pNode;
}
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: