链表操作

#include <stdio.h>
#include <malloc.h> 
/*                                                    
链表节点的定义:
*/
struct node
{
	int data;//节点内容
	node *next;//下一个节点
};


//创建单链表(带有头结点) 
struct node *create()
{
	int i=0;//链表中数据个数
	node *head,*p,*q;
	int x=0;
	head=(node*)malloc(sizeof(node));//创建头结点
	while(1)
	{
		printf("please input the data: (0 end)");  
		scanf("%d",&x); 
		if(0 == x)              
            break;    	//输入0结束
		p=(node*)malloc(sizeof(node));
		p->data=x;
		if(++i==1)
		{
			head->next=p;//连接到head的后面
		}
		else
		{
			q->next=p;//连接到链表尾端
		}
		q=p;
	}
	q->next=NULL;//链表的最后一个指针为NULL
	return head;
}

/*                                                   
编程实现单链表的测长:
返回单链表的长度
*/
int length(node *head)
{
	int len=0;
	node *p;
	p=head->next;
	while(p!=NULL)
	{
		len++;
		p=p->next;
	}
	return len;
}

//实现单链表的打印
void print(node *head)
{
	node *p;
	int index=0;
	if(head->next==NULL)//链表为空
	{
		printf("link is empty\n");
		return;
	}
	p=head->next;
	while(p!=NULL)
	{
		printf("the %dth node is:%d\n",++index,p->data);
		p=p->next;
	}
}

/*                                              单链表节点的查找
查找单链表pos位置的节点,返回节点指针
pos从0开始,0返回head节点
*/
struct node *search_node(node *head,int pos)
{
	node *p=head->next;
	if(pos<0)
	{
		printf("incorrect position to search node!\n");
		return NULL;
	}
	if(pos==0)
	{
		return head;
	}
	if(p==NULL)
	{
		printf("Link is empty\n");
		return NULL;
	}
	while(--pos)
	{
		if((p=p->next)==NULL)
		{
			printf("incorrect position to search node\n");
			break;		 //超出链表返回
		}
	}
	return p;
}
/*                                      单链表节点的插入
在单链表中某位置(pos个节点)后面插入节点
1.插入到链表首部
2.插入到链表中间
3.插入到蓝标尾部
单链表某个位置插入节点,返回链头指针,pos从0开始计算,0表示插入到head节点后面
*/
struct node *insert_node(node *head,int pos,int data)
{
	node *item=NULL;
	node *p;
	item=(node *)malloc(sizeof(node));
	item->data=data;
	if(pos==0)//插入到链表头后面
	{
		head->next=item;
		//return head;
	}
	p=search_node(head,pos);	//获得pos的节点指针
	if(p!=NULL)
	{
		item->next=p->next;
		p->next=item;
	}
	return head;
}


/*                                   单链表节点的删除
删除单链表pos位置的节点,返回头指针
pos从开始计算,1表示删除head后面的第一个节点
*/
struct node *delete_node(node *head,int pos)
{
	node *item=NULL;
	node *p=head->next;
	if(p==NULL)
	{
		printf("link is empty\n");
		return NULL;
	}
	p=search_node(head,pos-1);//获得pos-1位置的节点指针
	if(p!=NULL && p->next!=NULL)
	{
		item=p->next;
		p->next=item->next;
		delete item;
	}
	return head;
}
 


/*                                       将单链表逆序
单链表的逆置
遍历一遍单链表,利用一个辅助指针存储遍历过程中当前指针指向下一个元素,
然后将当前元素的指针反转,利用已经存储的指针往后继续遍历
*/
struct node *reverse(node *head)
{
	node *p,*q,*r;
	if(head->next==NULL)//链表为空
	{
		return head;
	}
	p=head->next;
	q=p->next;//保存原第二个节点
	p->next=NULL;//原第一个节点为末节点
	while(q!=NULL)//遍历各个节点的next指针反转
	{
		r=q->next;	//记录剩下的链表
		q->next=p;
		p=q;
		q=r;
	}
	head->next=p;//新的第一个节点为原末节点
	return head;
}

int main()
{
	node *head=create();//创建单链表
	printf("Length:%d\n",length(head));//测单链表的长度
	head = insert_node(head,2,5);//在第二个节点的后面添加5
	printf("在第二个节点的后面添加5:\n");
	print(head);//打印单链表
	head = delete_node(head,2);//删除第二个节点
	printf("删除第二个节点:\n");
	print(head);
	printf("将该链表逆序:\n"); 
	reverse(head);
	print(head);
	
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值