最近学了数据结构的单链表基本操作:创建,删除,插入,反转,排序等.
如有不当或错误之处,欢迎指正,不胜感激!
#include<stdio.h>
#include <malloc.h>
#include<stdlib.h>
int comp(const void *a,const void *b)//用于快排的比较函数
{
return (*(int*)a-*(int *)b);
}
struct node
{
int data;
struct node* next;
}Lnode;
struct node *p,*q;//声明临时节点
void head_insert(node &head,int x)//从头部插入新节点
{
p=(struct node*)malloc(sizeof(struct node));
if(p==NULL)
{
printf("内存申请失败,退出");
exit(0);
}
p->data=x;
p->next=head.next;
head.next=p;
}
void tail_insert(node &head,int x)//从尾部插入节点
{
p=(struct node*)malloc(sizeof(struct node));
if(p==NULL)
{
printf("内存申请失败,退出");
exit(0);
}
p->data=x;
p->next=NULL;
q->next=p;
q=p;
}
int node_length(node &head)//输出链表长度
{
int length=0;
p=head.next;
if(p==NULL) return length;
else
{
do
{
length++;
p=p->next;
} while(p);
}
return length;
}
void print_node(node &head)//输出链表
{
printf("输出此时链表:\n");
p=head.next;
if(p==NULL) {printf("The node is null.\n");return;}
else
{
while(p)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
}
void clear_node(node &head)//清空链表
{
p=head.next;
head.next=NULL;
while(p)
{
q=p;
p=p->next;
free(q);
}
}
void new_insert(node &head,int i,int a)//在第i个位置插入新整型元素 a
{
int count=0;
q=(struct node*)malloc(sizeof(struct node));
q->data=a;
p=&head;
if(i<0) { printf("Invalid,Position Error.\n");return;}
else
{
while(p&&count<i-1)
{
p=p->next;
count++;
}
if(!p||count>i-1)
{
printf("Invalid,Position Error.\n");
return;
}
else
{
q->next=p->next;
p->next=q;
}
printf("the number %d is inserted successfully.\n",a);
}
}
void delete_node(node &head,int i)//删除第i个节点
{
p=&head;
while(p->next&&--i)
p=p->next;
if(i) printf("Invalid,Position Error.\n");
else
{
q=p->next;
p->next=q->next;
free(q);
printf("Deleted successful.\n");
}
}
void invert_order(node &head)//将链表反转
{
node *This,*prev;
p=head.next;
This=NULL;
while(p)
{
prev=This;
This=p;
p=p->next;
This->next=prev;
}
head.next=This;
}
void sorted(node &head)//调用快速排序将链表升序排序
{
int i=0;
int length=node_length(head);
int *array=(int *)malloc(length*sizeof(int));
p=head.next;
while(p)
{
*(array+i)=p->data;
p=p->next;
i++;
}
qsort(array,length,sizeof(int),comp);
p=head.next;i=0;
while(p)
{
p->data=*(array+i);
p=p->next;
i++;
}
}
int main()
{
int number,i,a,choice,end=0;
Lnode.next=NULL;
q=&Lnode;
printf("初始化输入整型数据:\n");
while(scanf("%d",&number)!=EOF) //将数据存入链表,输入完数据后按Enter键再Ctrl+Z结束
{
head_insert(Lnode,number);//从头插入 即逆序插入
/* tail_insert(number); 从尾端 插入即正序插入 */
}
invert_order(Lnode);
while(!end)
{
printf("\n");
printf("To insert the number-----------------------------1\n");
printf("To delete the number-----------------------------2\n");
printf("To output the length of node---------------------3\n");
printf("To output the data of node-----------------------4\n");
printf("To resort the number in increasing order----------5\n");
printf("To end the operation ----------------------------6\n");
printf("Please input the number of the operation :");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("Please input the position you want to insert and the number: ");
scanf("%d%d",&i,&a);
new_insert(Lnode,i,a);
}
break;
case 2:
{
printf("Please input the position of the number you want to delete:");
scanf("%d",&i);
delete_node(Lnode,i);
}break;
case 3:
printf("The length of the node is: %d\n",node_length(Lnode));
break;
case 4:
print_node(Lnode);
break;
case 5:
sorted(Lnode);
break;
case 6:
end=1;
break;
default:
printf("Invalid,Please input the number of the operation :\n");
break;
}
}
clear_node(Lnode);
return 0;
}
附:在Windows下,输入数据完毕后先按Enter键,再按Ctrl+Z键,最后按Enter键即可结束输入;在Linux下,输入完毕后按Ctrl+D键可结束输入。