链表的插入、删除、排序的程序

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node
{
	int data;
	struct node * pNext;
}*pNode,Node;
//#define bool int;	//#define的意思是单纯的替换,与别名没有关系,而且C语言中没有bool数据类型
typedef int bool;	//typedef的意思就是别名,或者是声明结构体数据类型的变量,int 用bool来替换
#define true 0
#define false 1
pNode create_list();
void traverse_list(const pNode);
bool is_empty(const pNode);
int length_list(const pNode);
bool insert_list(pNode, int, int);
bool delete_list(pNode, int);
void sort1_list(const pNode);
void sort2_list(const pNode);
int find_list(const pNode, int);
//void sort3_list(const pNode, int left, int right);
int main(void)
{
	pNode pHead=NULL;
	pHead=create_list();
	if(is_empty(pHead))
	{
		puts("链表不为空!");
	}
	else
		puts("链表为空!");
	printf("链表的长度为:%d
",length_list(pHead));
	puts("-----------------------------------链表值为--------------------------------");
	traverse_list(pHead);
	puts("-----------------------------------冒泡排序--------------------------------");
	sort1_list(pHead);
	traverse_list(pHead);
	puts("-----------------------------------普通排序--------------------------------");
	sort2_list(pHead);
	traverse_list(pHead);
	//puts("------------------------------------快速排序-----------------------------");
	//sort3_list(pHead,1,5);
	//traverse_list(pHead);
	puts("-----------------------------------插入链表--------------------------------");
	insert_list(pHead, 5, 77);
	traverse_list(pHead);
	puts("-----------------------------------删除链表--------------------------------");
	delete_list(pHead,3);
	traverse_list(pHead);
	puts("-----------------------------------查找链表---------------------------------");
	find_list(pHead,49);
	return 0;
}
pNode create_list()
{
	int len,val,i;	//特别注意:C语言中,变量的定义必须放在前面,否则编译无法识别,会报错。
	pNode pTail;
	pNode pHead=(pNode)malloc(sizeof(Node));
	if(pHead==NULL)
	{
		printf("动态内存分配失败!
");
		exit(-1);
	}
		pTail=pHead;
		pTail->pNext=NULL;
	printf("请输入有效节点个数:len=");
	scanf("%d",&len);
	for(i=0;i<len;++i)
	{
		pNode pNew=(pNode)malloc(sizeof(Node));
		if(pNew==NULL)
		{
			printf("动态内存分配失败!");
			exit(-1);
		}
		printf("请输入数第%d个据域的值val=",i+1);
		scanf("%d",&val);
		pNew->data=val;
		pTail->pNext=pNew;
		pTail=pNew;
		pTail->pNext=NULL;
	}
	return pHead;
}
void traverse_list(const pNode pHead){
	pNode p=pHead->pNext;
	while(p!=NULL)
	{
		printf("%d	",p->data);
		p=p->pNext;
	}
	putchar('
');
	return;
}
bool is_empty(const pNode pHead)
{
	if(pHead->pNext==NULL)
		return true;
	else
		return false;
}
int length_list(const pNode pHead)
{
	int count=0;
	pNode p=pHead;
	while(p!=NULL)
	{
		count++;
		p=p->pNext;
	}
	return count;
}
bool insert_list(pNode pHead, int pos, int val)	//位置从有效节点1开始
{
	int count=length_list(pHead);
	int i=1;
	pNode p=pHead;
	pNode q=(pNode)malloc(sizeof(Node));
	if(q==NULL)
	{
		puts("内存分配失败!");
		exit(-1);
	}
	q->data=val;
	if(pos<2 || pos>count+1)
	{
		puts("链表当中不存在该位置!");
		return false;
	}
	while(i<pos-1)
	{
		++i;
		p=p->pNext;
	}
	q->pNext=p->pNext;
	p->pNext=q;
	return true;
}
bool delete_list(pNode pHead, int pos)
{
	int count=length_list(pHead);
	int i=1;
	pNode p=pHead->pNext;
	pNode q;
	if(pos<2 || pos>count)
	{
		puts("链表当中不存在该位置!");
		return false;
	}
	while(i<pos-1)
	{
		++i;
		p=p->pNext;
	}
	printf("删除的值是:%d
",p->pNext->data);
	q=p->pNext;
	p->pNext=q->pNext;
	free(q);
	//p->pNext=p->pNext->pNext;
	//free(p->pNext);//不能这么释放,这是错误的
	return true;
}
//冒泡排序
void sort1_list(const pNode pHead)
{
	int count=length_list(pHead);
	pNode p;
	int i,j;
	/*
		这是定义变量,不叫逗号表达式。需要是表达式才行。
		逗号表达式只取最后的那个表达式的值作为整个表达式的值,
		但是前边的表达式也要运行,左结合性,从左往右运行的。
		这个链表的for循环一定要用i,j;否则上限没法表示。
	*/
	for(i=1,p=pHead->pNext;i<count-1;++i,p=p->pNext)
		for(j=1,p=pHead->pNext;j<count-i;++j,p=p->pNext)
		{
			if(p->data > p->pNext->data)
			{
				int temp;
				temp=p->data;
				p->data=p->pNext->data;
				p->pNext->data=temp;
			}
		}
		return;
}
//普通排序,注意第三方的使用非常重要
void sort2_list(const pNode pHead)
{
	int count=length_list(pHead);
	pNode p,q;
	int i,j;
	for(i=1,p=pHead->pNext;i<count-1;++i,p=p->pNext)//第一个for循环表示比较多少次
		for(j=i+1,q=p->pNext;j<count;++j,q=q->pNext)//第二个for循环表示和哪个对象比较
		{
			if(p->data > q->data)
			{
				int temp;
				temp=p->data;
				p->data=q->data;
				q->data=temp;
			}
		}
		return;
}
int find_list(const pNode pHead, int val)
{
	pNode p=pHead->pNext;
	int count=length_list(pHead);
	int flag=1,flag2=0;
	while(p !=NULL )
	{
		if(p->data==val)
		{
			printf("你要查找的数的位置:%d
",flag);
			flag2++;
		}
		p=p->pNext;
		++flag;
	
	}
		if(flag2==0)
		puts("链表中没有查找的值!");
	return flag;

}
//快速排序
/*
void sort3_list(const pNode pHead, int left, int right)
{
	pNode p=pHead->pNext;
	pNode q=pHead->pNext;
	pNode r=NULL;
	int i=left;
	int j=right;
	int key=p->data;
	int count=length_list(pHead);
	while(q != NULL)
		q=q->pNext;
	if(i>=j)
		exit(-1);
	while(i<j)
	{
		//for(i=left,p=pHead->pNext;i<j,q->data<key;--j)
		while(i<j && q->data<key )
		{
			--j;
			r->pNext=q;
			q=r;
			r=NULL;
			break;
		}
		p->data=q->data;
		//a[i]=a[j];
		while(i<j && p->data>key )
		{
			++i;
			p=p->pNext;
			break;
		}
		q->data=p->data;
	}
	p->data=key;
	sort3_list(pHead, left, i-1);
	sort3_list(pHead, j+1, right);
}
*/

  

原文地址:https://www.cnblogs.com/fengkui/p/6117932.html