C语言实现链表的基本操作(创建,增删改查,合并,冒泡排序)

链表的创建,增,删,改,查,链表的合并和链表的冒泡排序

#include 
#include 
typedef struct Node
{
	int num;
	struct Node *next;
}node;
node *creat()//创建
{
	node *p1,*p2,*head;
	int n=0;
	p1=p2=malloc(sizeof(node));
	head=NULL;
	printf("\t\t\t\t\t请输入结点数据(输入0代表结束):\n");
	printf("\t\t\t\t\t");
	scanf("%d",&p1->num);
	if(!p1||!p2)
		exit(0);
	while(p1->num!=0)
	{
		n++;
		if(n==1)
			head=p1;
		else
			p2->next=p1;
		p2=p1;
		p1=malloc(sizeof(node));
		printf("\t\t\t\t\t");
		scanf("%d",&p1->num);
	}
	p2->next=NULL;
	return head;
}
node *insert(node *head)//链表尾部插入
{
	node *p1,*p2;
	p1=head;
	printf("\t\t\t\t\t请输入要插入的结点数据:");
	if(head==NULL)
	{
		p2=malloc(sizeof(node));
		head=p2;
		p2->next=NULL;
		scanf("%d",&p2->num);
	}
	else
	{
		while(p1->next!=NULL)
			p1=p1->next;
	p2=malloc(sizeof(node));
	if(!p2)
			return 0;
	p1->next=p2;
	p1=p2;
	p1->next=NULL;
	scanf("%d",&p2->num);
	}
	return head;
}
node *insert1(node *head)//链表头部插入
{
	node *p1,*p2;
	p1=head;
	printf("\t\t\t\t\t请输入要插入的结点数据:");
	if(head==NULL)
	{
		p2=malloc(sizeof(node));
		if(!p2)
			return 0;
		head=p2;
		p2->next=NULL;
		scanf("%d",&p2->num);
	}
	else
	{
		p2=malloc(sizeof(node));
		p2->next=p1;
		p1=p2;
		head=p2;
		scanf("%d",&p2->num);
	}
	return head;
}

node *delNode(node *head,int e)//删除
{
	node *p1,*p2;
	p1=head;
	if(head==NULL)
		exit(0);
	while(p1!=NULL&&p1->num!=e)
	{
		p2=p1;
		p1=p1->next;
	}
	if(p1==NULL)
		printf("\t\t\t\t\t未找到该结点,删除失败!!\n");
	if(p1->num==e)
	{
		if(p1==head)
			head=p1->next;
		else
		{
			p2->next=p1->next;
			p2=p1->next;
			free(p1);
		}
		printf("\t\t\t\t\t删除成功!!\n");
	}
	return head;
}
int seek(node *head,int x)//查找结点
{
	node *p1;
	int n=0;
	p1=head;
	if(head==NULL)
	{
		printf("\t\t\t\t\t链表为空哦!!\n");
		exit(0);
	}
	while(p1!=NULL)
	{
		n++;
		if(p1->num==x)
		{
			printf("\t\t\t\t\t成功找到该结点!!!,该结点在链表中第%d个位置\n\n",n);
			return n;
		}
		p1=p1->next;
	}
	printf("\t\t\t\t\t未找到该结点!!!\n");
	return 0;
}

void print(node *head)//打印
{
	node *p;
	p=head;
	printf("\n\t\t\t\t\t链表中的数据如下:\n");
	if(head!=NULL)
	{
		printf("\t\t\t\t\t");
	while(p!=NULL)
	{
		printf("%d ",p->num);
		p=p->next;
	}
	printf("\n");
	}
	else
		printf("\t\t\t\t\t链表为空!!!\n");
}
void sequence(node *head)//冒泡排序
{
	int i,j,a,n=0;
	node *p;
	p=head;
	while(p!=NULL)
	{
		n++;
		p=p->next;
	}
	if(p!=NULL)
	{
	for(i=0;inum>p->next->num)
				{
					a=p->num;
					p->num=p->next->num;
					p->next->num=a;
				}
				p=p->next;
			}
		}
	}
	}
}
node *mergeNode(node *head1,node *head2)
{
	node *La;
	La=head1;
	if(head1==NULL&&head2==NULL)
	{
		printf("\t\t\t\t\t两个链表都为空,合并失败!!!\n");
		return 0;
	}
	if(head1==NULL)
		return head2;
	if(head2==NULL)
		return head1;
	while(La->next!=NULL)
		La=La->next;
	La->next=head2;
	return head1;
}
void menu()//菜单
{
	node *p1,*p2,*p3,*p4,*p5;
	int a,b,c,d;
	printf("\t\t\t\t\t*************菜单系统*************\n");
	printf("\t\t\t\t\t*输入1:查看第一题\n");
	printf("\t\t\t\t\t*输入2:查看第二题\n");
	printf("\t\t\t\t\t*输入0:退出菜单系统\n");
	printf("\t\t\t\t\t**********************************\n");
	printf("\t\t\t\t\t请输入你的选项:");
	scanf("%d",&d);
	do
	{
		switch(d)
		{
		case 1:system("cls");
			p1=creat();
			print(p1);
			p2=insert1(p1);
			print(p2);
			printf("\t\t\t\t\t请输入要删除的数据:");
			scanf("%d",&b);
			p3=delNode(p2,b);
			print(p3);
			printf("\t\t\t\t\t请输入要查找的数据:");
			scanf("%d",&c);
			a=seek(p3,c);break;
		case 2:
			system("cls");
			printf("\t\t\t\t\tLa:\n");
			p4=creat();	
			printf("\n\t\t\t\t\tLb:\n");
			p5=creat();
			p3=mergeNode(p4,p5);
			sequence(p3);
			printf("\n\t\t\t\t\tLa和Lb合并后得到的Lc:\n");
			print(p3);break;
		case 0:break;
		}
		printf("\t\t\t\t\t请输入你的选项:");
		scanf("%d",&d);
	}while(0

你可能感兴趣的:(C语言)