使用c语言的单链列表操作: 创建,删除,插入,反向,排序等.

电脑杂谈  发布时间:2020-05-12 12:22:12  来源:网络整理

c语言链表排序_c语言单链表排序_链表c语言

链接到本文:

最近了解了数据结构单链接列表的基本操作: 创建,删除,插入,反向c语言单链表排序,排序等.

c语言链表排序_c语言单链表排序_链表c语言

如果有任何不当或错误c语言单链表排序,请纠正我,非常感谢!

#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;
}

链表c语言_c语言链表排序_c语言单链表排序

 附:在Windows下,输入数据完毕后先按Enter键,再按Ctrl+Z键,最后按Enter键即可结束输入;在Linux下,输入完毕后按Ctrl+D键可结束输入。

c语言链表排序_c语言单链表排序_链表c语言

wahack

链表c语言_c语言链表排序_c语言单链表排序

原始文章2喜欢3访问2682

关注私人消息


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-207434-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...