链表(单链表)的多种功能实现
单链表
简介
单链表的多种功能实现
代码
点击查看代码
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct Node{
int data;
struct Node * next;
}Node;//链表创建
void creat(Node *head,int size);//创建
void forwardInsert(Node *head,int data);//头插
void backInsert(Node *head,int data);//尾插
void travel(Node *head);//遍历
int isEmpty(Node *head);//判空
void AllDelete(Node *head);//删除
void SomeDataDelete(Node *head,int data);//删除一个数
void SomeDataInsert(Node *head,int data,int num);//在某位置插入一个数
int Search(Node *head,int data);//查找
void sort(Node *head,int (*cmp)(int,int));//冒泡排序
int Ascend(int x,int y);//升序
int Descend(int x,int y);//降序
int main()
{
Node *head=(Node*)malloc(sizeof(Node));//头节点
head->next=NULL;
int num,temp,i;
printf("Linknum:");//创建
scanf("%d",&num);
creat(head,num);
travel(head);
printf("Insert forwarddata:");//头插
scanf("%d",&temp);
forwardInsert(head,temp);
travel(head);
printf("Insert backdata:");//尾插
scanf("%d",&temp);
backInsert(head,temp);
travel(head);
printf("Somedata delete:");//删除
scanf("%d",&temp);
SomeDataDelete(head,temp);
travel(head);
printf("SomeDataInsert:input data and which:"); //某一位置插入某个数(BUG不可尾插)
scanf("%d %d",&temp,&i);
SomeDataInsert(head,temp,i);
travel(head);
sort(head,Descend);
travel(head);
printf("Alldelete:\n"); //删除
AllDelete(head);
travel(head);
isEmpty(head);
free(head);//free掉头节点
return 0;
}
int Descend(int x,int y)
{
if(x<y)
{
return 0;
}else
{
return 1;
}
}
int Ascend(int x,int y)
{
if(x<y)
{
return 1;
}else
{
return 0;
}
}
void sort(Node *head,int(*cmp)(int,int))
{
if(head->next==NULL)
{
printf("Empty Node!\n");
return;
}
if(head->next->next==NULL)
{
printf("Only one element!\n");
}
Node *p1;
Node *p2;
int temp;//交换
for (p1=head->next; p1->next!=NULL;p1=p1->next)
{
for (p2=p1->next; p2!=NULL; p2=p2->next)
{
if ((*cmp)(p1->data,p2->data))
{
temp=p1->data;
p1->data=p2->data;
p2->data=temp;
}
}
}
}
void creat(Node *head,int size)
{
int i;
Node *p=head;
for(i=1;i<=size;i++)
{
Node *newnode =(Node*)malloc(sizeof(Node));
newnode->next=NULL;
printf("--%d--:请输入数据:",i);
scanf("%d",&newnode->data);
p->next= newnode;
p=newnode;
}
return;
}
void forwardInsert(Node *head,int data)
{
Node *newhead=(Node*)malloc(sizeof(Node));
newhead->data=data;
newhead->next= head->next;
head->next= newhead;
return;
}
void travel(Node *head)
{
Node *p=head;
int i=0;
while(p->next!=NULL)
{
i++;
p=p->next;
printf("List%d: data: %d\n",i,p->data);
}
if(i==0)
{
printf("Empty!\n");
}
return;
}
void backInsert(Node *head,int data)
{
Node *p=head;
Node *back=(Node*)malloc(sizeof(Node));
back->next=NULL;
back->data=data;
while(p->next!=NULL)
{
p=p->next;
}
p->next=back;
return;
}
int isEmpty(Node *head)
{
if(head->next==NULL)
{
printf("is Empty!\n");
return 1;
}else
{
printf("No Empty!\n");
return 0;
}
}
void AllDelete(Node *head)
{
Node *p=head->next;
Node *temp=head;
while(p!=NULL)
{
temp=p;
p=p->next;
free(temp);
}
head->next=NULL;
return;
}
void SomeDataDelete(Node *head,int data)
{
Node *p=head;
Node *last=head;
int flag=1;
while(p->next!=NULL)
{
p=p->next;
if(p->data==data)
{
flag=0;
if(p->next!=NULL)
{
last->next=p->next;
last=p->next;
}else
{
last->next=NULL;
}
}else
{
last=last->next;
}
}
if(flag)
{
printf("No this data!\n");
}
return;
}
void SomeDataInsert(Node *head,int data,int num)
{
Node *p=head;
Node *last=head;
int i=0;
while(p->next!=NULL)
{
p=p->next;
i++;
if(i==num)
{
Node *NewNode=(Node*)malloc(sizeof(Node));
NewNode->data=data;
last->next= NewNode;
NewNode->next=p;
}
last=last->next;
}
}
本文来自博客园,作者:江水为竭,转载请注明原文链接:https://www.cnblogs.com/Az1r/p/16667497.html