⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 link(待填空).cpp

📁 用带表头的链表存放输入的数据
💻 CPP
字号:
// 这是使用应用程序向导生成的 VC++ 
// 应用程序项目的主项目文件。
/* 链表操作:插入、删除、输出、翻转
	用带表头的链表存放输入的数据,每读入一个数,按升序顺序插入到链表中,链表中允许两个结点有相同值。链表的头结点存放链表后面的结点个数,初始化时就生成头结点(初值为0)。链表翻转是把数据逆序(变成降序),注意,头结点不动。翻转后要再翻转一次,恢复升序后才能插入新元素,否则会出错。 */



#include <stdio.h>
#include "malloc.h"
#define null 0

struct node 
{ 
	float data;
	struct node *next;
};

void insert(float aa,struct node *vq)
{
	struct node *newnode,*p;
	newnode=(struct node *)malloc(sizeof(struct node));
	newnode->data=aa;
	p=vq;
	while ((p->next!=null)&&(p->next->data<aa)) 
		p = p->next/* 填空1 */;
	newnode->next=p->next;
	p->next = newnode/* 填空2 */;
	vq->data=vq->data+1;
}

void dele(float aa,struct node *vq)
{ 
	struct node *p,*q;

	p=vq;

	while ((p->next!=null)&&(p->next->data != aa))
		p=p->next/* 填空3 */;

	if ((p->next==null)||(p->next->data!=aa))
		printf("\n%5.1f is not in the link !",aa);
	else if (p->next->data==aa) 
	{ 
		q=p->next;
		p->next=q->next/* 填空4 */;
		free(q/* 填空5 */);
		vq->data=vq->data-1;
	}
}

void print(struct node *vq)
{
	struct node *p;

	printf("\nthe length of link is %4.0f",vq->data);

	p=vq->next;

	printf("\nthe link is:");

	while (p!=NULL/* 填空6 */) 
	{ 
		printf("%5.1f",p->data);

		p = p->next/* 填空7 */;
	}
}

void reverse(struct node **vq)
{ 
	struct node *q,*p,*temp;
	
	p = ( *vq )->next;
	
	( *vq )->next = p;

	temp = ( *vq )->next;
	
	while( p != null )
	{
		q = p;
		
		p = p->next;
		
		q->next = ( *vq )->next;
		
		( *vq )->next = q;
	}

	temp->next = NULL;
}

void main()
{ 
	int mark=1,op;
	float aa;
	struct node *vq;
	vq=(struct node *)malloc(sizeof(struct node));
	vq->data=0;
	vq->next=null;
	while (mark==1)
	{ 
		printf("\nWhich kind of operation will you select ? ");
		printf("\ninsert---1, delete---2, print---3, reverse---4, exit---0  :  ");
		scanf("%d",&op);
		switch (op)
		{ 
			case 0: mark=0; 
					break;
			case 1: printf("\nPlease input the new element:");
					scanf("%f",&aa);
					insert(aa,vq);
					print(vq);
					break;
			case 2: if (vq->data==0)
						printf("\n the link is null !",aa);
					else
					{	
						printf("\nPlease input the deleted element:");
						scanf("%f",&aa);
						dele(aa,vq); 
						print(vq);
					}
					break;
			case 3: print(vq); 
					break;
			case 4: reverse(&vq); 
					print(vq);
					break;
			default:printf("   input error!\n");
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -