单链表操作

1、单链表

以下这个程序实现了单链表的创建、添加、删除、打印等功能。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <conio.h>
using namespace std;

/*定义结构体*/
typedef struct student
{
	int data;
	struct student *next;
}node;


/*创建一个只有头结点的空链表*/
struct student *create_head()
{
	struct student *head;
	head=(struct student*)malloc(sizeof (struct student) );
	if(head==NULL)
	//小心别漏这个
	{
		printf("申请头结点失败!\n");
		return NULL;
	}
	head->next=NULL;
	return head;
}


//单链表测长
int length(node *head)
{
	int n=0;
	node *p;
	p=head;
	while(p!=NULL)
	{
		p=p->next;
		n++;
	}
	return(n);
}

/*打印单链表*/
void print(struct student *head)
{
	struct student *p;
	printf("	链表如下:	\n");
	p=head->next;
	while(p!=NULL)
	{
		printf("%.1f\n",p->data);
		p=p->next;
	}
}


/*删除链表中data为 num 的结点*/
node  *delete_note(struct student *head,int num)
{
	node *p1,*p2;
	p1=head;
	while(num!=p1->data&&p1->next!=NULL)
	{
		p2=p1;
		p1=p1->next;   	
	}
	/*最后找到的节点为保存在p1中,p2是
	要删除的节点的前一个节点	*/
	
	if(num==p1->data)
	{
		if(p1==head)
		{
			head=p1->next;
			free(p1);
		}
		else
			p2->next=p1->next;
	}
	else
		{
			printf("找不到符合删除要求的结点!!!\n");
		}
	return head;
}

/*将num插入链表,使链表保持升序*/
struct student *insert(struct student *head,int num)
{
	node *p0,*p1,*p2;
	p1=head;
	p0=(node *)malloc(sizeof(node));
	p0->data=num;
	while(p1->next!=NULL&&p0->data>p1->data)
	{
		p2=p1;
		p1=p1->next;
	}
	if(p0->data<=p1->data)
	{
		if(p1==head)
			{
			p0->next=p1;
			head=p0;
		}
		else
			{p2->next=p0;
			p0->next=p1;
		}
	}
	else
	{
		p1->next=p0;
		p0->next=NULL;
	}
	return head ;
}


/*释放链表*/
void free_list(struct student *head)
{
	struct student *p=head ;
	printf("释放链表:\n");
	while(p!=NULL)
	{
		head=head->next;
		free(p);
		p=head;
	}
	printf("释放链表成功!\n");
}


/*实现单链表的排序*/
node *sort(node *head)
{
	node *p,*p1,*p2;
	int n,temp;
	n=length(head);
	if(head==NULL||head->next==NULL)
		return head;
	p=head;
	for(int j=1;j<n;++j)
	{
		p=head;
		for(int i=0;i<n-j;++i)
		{
			if(p->data>p->next->data)
				{
				temp=p->data;
				p->data=p->next->data;
				p->next->data=temp;
			}
			p=p->next;
		}
	}
	return head;
}

/*完整的有头结点链表操作程序*/
void main()
{
	struct student *p , *head ;
	char c;
	int num ;
	printf("有头结点链表操作程序:\n");
	head=create_head();
	while(1)
	{
		printf("   I:插入结点(自动升序)
				P:输出链表				
				D:删除结点
				E:释放链表并退出程序!	");
		c=getch();
		switch(c)
		{
			case'I':
				printf("请分别输入要插入学生的学号和分数:\n");
				scanf("%d",&num);
				p=(struct student*)malloc( sizeof(struct student) );
				if(p==NULL)
				{
					printf("申请该结点失败!!!\n");
					exit (0) ;
				}
				p->num=num;
				p->score=score;
				//给 p 赋值
				insert(head,p);
				printf("插入成功!\n");
				break;
			case'P':
				print(head);
				break;
			
			case'D':
				printf("请输入要删除的学生的学号:\n");
				scanf("%d",&num);
				delete_note(head,num);
				break;
			case'E':
				free_list(head);
				exit (0);
		}
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值