删除链表节点

该程序实现了一个动态链表,根据输入的数值进行有序插入。用户可以选择删除节点的方式,按位置或按值。程序首先创建一个空链表,然后不断接收输入的分数并按升序插入。当输入0时停止。接着,用户选择删除方式,输入位置或值,程序会相应删除节点。最后,程序打印链表内容。
摘要由CSDN通过智能技术生成

#if 1
#include <stdio.h>
#include <stdlib.h>
struct Student {
	int num;
	float score;
	struct Student* next;
};
void output(struct Student* head);
int main() {
	struct Student* head = NULL,*prep = NULL;
	struct Student* node,*tail = NULL;
	struct Student* p; 
	enum direction { left=-1,right=1}dire;//1 代表从正方向,也就是尾巴开始插入
	int i,j,length;
	int del_case,location;//删除节点的方式
	double value;//要删除的数值
	double a;

	/*printf("please input the direction that you want to insert!\n");//选择插入方向
	scanf_s("%d", &dire);*/
	scanf_s("%lf", &a);


	//node = (struct Student*)malloc(sizeof(struct Student));
	//node->num = 001;
	//node->score = 89.5 ;
	//node->next = NULL;//动态创建一个节点,并且赋值,指向空
	//head = node;//节点给头
	//tail = node;
	length = 0;
	j = 0;
	//下面的代码段插入节点
	do	{
		node = (struct Student*)malloc(sizeof(struct Student));
		 
		node->score = a;
		node->next = NULL;//动态创建一个节点,并且赋值,指向空 
		length++;
		
		if (head==NULL)
		{
			head = node;
			tail = node;
			prep = node;
		}
		else
		{
			p = head;
			if (node->score > tail->score)//比最后一个节点的值还大的话,就直接插入到尾部,????(是为什么这个判断很重要,但是下面那个判断不重要)
			{ 
				tail->next = node;//先移动指向
				tail = node; 
				
			}
			else if(node->score < head->score)
			{
				node->next = head;//当前的头成为新插入的节点的next
				head = node;
			 	
			}
			else
			{
				//printf("tail-1 = %lf,tail = %lf\n", p->score, tail->score);
				//(tail - 1)->next = node;//看来结构体指针不能使用加减号,得单独定义一个前指针和后指针
				//node->next = tail;
				 
					while (node->score > p->score)
					{
						prep = p;
						p = p->next;
					}

					prep->next = node;
					node->next = p;
				 

			} 

		 

			//if (dire==right)//无序插入节点,主要是区分插入方向
			//{
			//	tail->next = node;//先移动指向
			//	tail = node; 
			//}
			//else
			//{
			//	node->next = head;//当前的头成为新插入的节点的next
			//	head = node;//新插入的节点成为头结点
			//}
			  
		}
		scanf_s("%lf", &a); 
	} while (a != 0);
	 
	printf("please input the way you want to delte,case 1 means delete location\n");
	scanf_s("%d", &del_case);
	//下面的代码段删除节点,分为两种,一种是删除固定编号位置处的节点,一种是删除某个读入的数值
	switch (del_case)
	{
		case 1:
			printf("please input the location you want you delete:\n");
			scanf_s("%d", &location);
			if (location> length)
			{
				printf("not exist this location\n");
				break;
			}
			else
			{
				p = head;
				prep = head;
				if (location == 1)
				{
					head = head->next;
				}
				else
				{
					for (i = 1; i < location; i++)
					{
						prep = p;
						p = p->next;//已经到了指定位置了					
					}
					prep->next = p->next;
					delete p;
				}
				
			}
			break;
		case 2:
			printf("please input the value you want you delete:\n");
			scanf_s("%lf", &value);
			p = head;
			prep = p;
			while (true)
			{
				if (p->score == value)
				{
					if (p==head)
					{
						head = head->next;
					}
					else
					{
						prep->next = p->next;
						delete p;
					}
					
					
					break;
				}
				else
				{
					prep = p;
					p = p->next;
				}
					
			}
			break;
	}
	
	//node = (struct Student*)malloc(sizeof(struct Student));
	//node->num = 002;
	//node->score = 90;
	//node->next = NULL;//同上一个节点的创建方式,重新创了一个节点
	//head->next = node;//节点给头的next

	//node = (struct Student*)malloc(sizeof(struct Student));
	//node->num = 003;
	//node->score = 85;
	//node->next = NULL;
	//head->next->next = node;
	 
	output(head);//传入头指针,进行链表打印
	return 0;
}
void output(struct Student* head)
{
	struct  Student* p;
	p = head;// 
	do {//使用函数,参数是链表头
		printf("%5.1f\n", p->score);
		p = p->next;

	} while (p != NULL);
}
#endif

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值