C语言实现带头结点的单链表的创建以及简单使用

 其功能实现有

创建链表

头插法尾插法

中间插入

链表的显示

节点的删除

链表的清空以及销毁

链表的转置

链表的排序

等功能

代码如下

//链表的创建以及使用
//带头节点的链表

#include<stdio.h>
#include<stdlib.h>

typedef struct LNode{
	int num;
	struct LNode * next; 
}LNode,*linklist;

void create_link();//创建
void create_node();
void insert_head_node();
void insert_tail_node();
void insert_midfront_node();
void insert_midtail_node();
void insert_node_while();
void insert_node_sort();
void display_link();//显示
void clear_link();
void delete_node();//删除
void destroy_link();//销毁链表
void reverse_link();//颠倒
int find_link_length();//求长度
void link_sort();//排序
LNode * find_link_node();//查找


void main()
{
	
	linklist head=NULL;
	create_link(&head);
	//create_node(&new_node);
	//insert_head_node(&head);
	insert_tail_node(&head);
	//display_link(head);
	//insert_midfront_node(&head);
	//insert_midtail_node(&head);
	insert_node_while(&head);
	//insert_node_sort(&head);
	//display_link(head);
        //delete_node(&head);
        display_link(head);
	//reverse_link(&head);
        //clear_link(&head);
	int length=find_link_length(head);
	//printf("%d\n",length);
	//LNode * find_node=find_link_node(head);
	//printf("%d\n",find_node->num);
	link_sort(&head);
	display_link(head);
	destroy_link(&head);
	//display_link(head);
        printf("end\n");
}

void link_sort(linklist *head)//插入排序
{
	if((*head)->next==NULL||(*head)->next->next==NULL)
	return ;
	LNode * pend=(*head)->next;
        LNode * p=pend->next;
	while(p!=NULL)
	{
		LNode * t = (*head)->next;
		LNode * tend = *head;
		while(t!=p&&t->num>=p->num)
		{
			t=t->next;
			tend=tend->next;
		}
		if(t==p)
			pend=p;
		else
		{
			pend->next=p->next;
                        p->next=t;
			tend->next=p;
		}
		p=pend->next;
	}
	printf("link_sort success!!!\n");

}

/*
void link_sort(linklist * head)//插入排序
{

	if((*head)->next==NULL||(*head)->next->next==NULL)
		return;
        LNode * pend=(*head)->next;//指向第一个节点
	LNode * p=pend->next;//指向第二个节点
	while(p!=NULL)
	{
		LNode * tmp = (*head)->next;
		LNode * pre = (*head);
		while(tmp!=p && p->num>=tmp->num)//找到插入位置
		{
			tmp = tmp->next;
			pre=pre->next;
		}
		if(tmp == p)
			pend = p;
		else
		{
			pend->next = p->next;
			p->next=tmp;
			pre->next=p;
		}
		p=pend->next;
	}
	printf("link_sort success!!!\n");

}
//
void link_sort(linklist * head)//排序
{
	LNode *p1,*p2,*p3,*p4;
	
	int temp;
	int i,j;
	int link_length=find_link_length(*head);
	for(i=1;i<link_length;i++)
	{
		p1=*head;
		p2=(*head)->next;
		for(p3=p2->next,j=0;j<link_length-i;j++,p1=p1->next,p2=p1->next,p3=p2->next,p4=p3->next)
		{
			p4=p3->next;
			if(p2->num>p3->num)
			{
				p2->next=p4;
				p3->next=p2;
				p1->next=p3;
			}
		}

	}



}
*/


LNode * find_link_node(linklist head)//按照内容查找
{
	LNode *p;
	p=head->next;
	int x;
	printf("请输入你要查找的内容:");
	scanf("%d",&x);
	while(p!=NULL&&p->num!=x)
	{
		p=p->next;
	}
	if(p==NULL)
	{
		printf("链表中无包含此内容节点!!!\n");
		return 0;
	}
	if(p->num==x)
	{
		printf("find_link_node success!!!\n");
		return p;
	}

}
int find_link_length(linklist head)
{
	int length=0;
	LNode * p;
	p=head->next;
	while(p!=NULL)
	{
		length++;
		p=p->next;
	}
        printf("find_length success!!!length = %d\n",length);
        return length;
}

void create_link(linklist *head)
{
	*head = (linklist)malloc(sizeof(LNode));
	(*head)->next=NULL;
	printf("link create success!\n");

}

void create_node(linklist * new_node)
{
	*new_node=(linklist)malloc(sizeof(LNode));
	printf("node create success!\n");

}

void delete_node(linklist * head)
{
	LNode * p;
	LNode * q;
	int x;
        printf("请输入删除内容:");
	scanf("%d",&x);
	p=(*head)->next;
	q=(*head);
	if((*head)->next==NULL)
	{
		printf("empty!!!\n");
		return;
	}

	while(p!=NULL&&p->num!=x)
	{
		q=p;
		p=p->next;
	}
	if(p==NULL)
	{
		printf("未找到!!!\n");
	}
	else
	{
		q->next=p->next;
		free(p);
		printf("delete success!!!\n");
	}

}

void insert_head_node(linklist * head)
{
	LNode *new_node;
	int x;
	printf("请输入你要插入几个节点:");
	scanf("%d",&x);
	for(int i=1;i<=x;i++)
	{
		new_node=(LNode *)malloc(sizeof(LNode));
		new_node->num=i;
		new_node->next= (*head)->next;
		(*head)->next=new_node;
	}
	printf("insert_head_node success!!!\n");

}

void insert_tail_node(linklist * head)
{
	LNode *new_node,*p;
	p=(*head);

	int x;
	printf("请输入你要插入的节点:");
	scanf("%d",&x);
	while(p->next!=NULL)
	{
		p=p->next;
	}
	for(int i=1;i<=x;i++)
	{
		new_node=(LNode *)malloc(sizeof(LNode));
		new_node->num=i;
		new_node->next=p->next;
		p->next=new_node;
		p=new_node;
	}
	printf("insert_tail_node success!\n");
}

void insert_midfront_node(linklist * head)
{
	LNode *new_node,*p,*q;
	new_node=(LNode *)malloc(sizeof(LNode));
	int x,y;
	printf("请输入节点内容:");
	scanf("%d",&x);
	printf("请输入插入位置数:");
	scanf("%d",&y);
	new_node->num=x;
	if((*head)->next==NULL)
	{
		new_node->next=(*head)->next;
		(*head)->next=new_node;
                
	}
	else
	{
		p=(*head)->next;
		q=(*head);
                while(p!=NULL&&p->num!=y)
		{
			q=p;
			p=p->next;

		}
		q->next=new_node;
		new_node->next=p;
	}	
}

void insert_midtail_node(linklist * head)
{
	LNode * new_node=NULL;
	new_node=(LNode *)malloc(sizeof(LNode));
	LNode *p;
	p=(*head)->next;
	int x,y;

	printf("请输入节点内容:");
	scanf("%d",&x);
	new_node->num=x;
	printf("请输入插入位置:");
	scanf("%d",&y);
	if((*head)->next==NULL)
	{
		new_node->next=(*head)->next;
		(*head)->next=new_node;
	}
	else
	{
		while(p->next!=NULL&&p->num!=y)
		{
			p=p->next;
		}
		new_node->next=p->next;
		p->next=new_node;
	}
 
}

void insert_node_while(linklist *head)
{
	 LNode * new_node=NULL;
         new_node=(LNode *)malloc(sizeof(LNode));
         LNode *p;
         
         int x,y;
         printf("请输入节点内容:");
         scanf("%d",&x);
         new_node->num=x;

         while(1)
	 {
         p=(*head)->next;
         printf("请输入插入位置:");
         scanf("%d",&y);
         if((*head)->next==NULL)
         {
                 printf("Empty!!!\n");
		 return ;
         }
	 else
	 {
                 while(p->next!=NULL&&p->num!=y)
                 {
                         p=p->next;
                 }
		 if(p->num==y)
		 {
			 new_node->next=p->next;
			 p->next=new_node;
			 return;
		 }
		 if(p->next==NULL)
		 {
			 printf("请重新输入!!!\n");
		 }

	 }
         }


}

void insert_node_sort(linklist * head)
{
	LNode * p,*q;
	LNode * new_node;
	new_node=(LNode *)malloc(sizeof(LNode));
	int x;
	printf("请输入节点内容:");
	scanf("%d",&x);
	new_node->num=x;
	p=(*head)->next;
	q=(*head);
	while(p!=NULL&&p->num<new_node->num)
	{
	       q=p;
	       p=p->next;
	}
	new_node->next=q->next;
	q->next=new_node;
        
}

void display_link(linklist head)
{
	linklist p=NULL;
	p=head->next;
        if(head->next==NULL)
	{
		printf("empty!!!\n");

	}
	while(p!=NULL)
	{
		printf("%d\n",p->num);
		p=p->next;
	}

	printf("display end!!!\n");
}



void clear_link(linklist *head)
{
	linklist p=NULL;
	while((*head)->next!=NULL)
	{
		p=(*head)->next;
		(*head)->next=p->next;
		free(p);
	}
	printf("clean_link success!!!");
}


void reverse_link(linklist *head)//时间复杂度为n 空间复杂度为1
{
	LNode *p1=NULL;
	LNode *p2=NULL;
        LNode *p3=NULL;
	if((*head)->next==NULL)
	{
		printf("Link Empty!!!\n");
		return;
	}
	if((*head)->next->next==NULL)
	{
		printf("reverse success!!!\n");
		return;
	}
	p1=(*head)->next;
	p2=p1->next;
	p3=p2->next;
	p1->next=NULL;
	while(p3!=NULL)
	{
		p2->next=p1;
		p1=p2;
		p2=p3;
		p3=p3->next;
	}
	p2->next=p1;
	(*head)->next=p2;
	printf("reverse success!!!\n");
	display_link(*head);

}

void destroy_link(linklist * head)
{
	LNode *p,*q;
	p=(*head)->next;
	q=(*head)->next;
	while(p)
	{
		q=p;
		p=p->next;
                free(q);
	}
	free(*head);
	*head=NULL;
	printf("destroy success!!!\n");
}



  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值