8579 链式线性表的基本操作

时间限制:1000MS  代码长度限制:10KB
提交次数:5567 通过次数:2176

题型: 编程题   语言: G++;GCC

Description

 编写算法,创建一个含有n个元素的带头结点的单链表L并实现插入、删除、遍历操作。本题目提供部分代码,请补全内容。

#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1 
#define ElemType int

typedef struct LNode
{
 int data;
 struct LNode *next;
}LNode,*LinkList;

int CreateLink_L(LinkList &L,int n){
// 创建含有n个元素的单链表
  LinkList p,q;
  int i;
  ElemType e;
  L = new LNode;
  L->next = NULL;              // 先建立一个带头结点的单链表
  q = L;
  for (i=0; i<n; i++) {
    scanf("%d", &e);
    p = new LNode;  // 生成新结点
    // 请补全代码

  }
  return OK;
}

int LoadLink_L(LinkList &L){
// 单链表遍历
 LinkList p = L->next;
 if(___________________________)printf("The List is empty!"); // 请填空
 else
 {
	 printf("The LinkList is:");
	 while(___________________________)    // 请填空
	 {
		printf("%d ",p->data); 
		___________________________    // 请填空
	 }
 }
 printf("\n");
 return OK;
}

int LinkInsert_L(LinkList &L,int i,ElemType e){
// 算法2.9
// 在带头结点的单链线性表L中第i个位置之前插入元素e
// 请补全代码

}

int LinkDelete_L(LinkList &L,int i, ElemType &e){
// 算法2.10
// 在带头结点的单链线性表L中,删除第i个元素,并用e返回其值
// 请补全代码

}

int main()
{
 LinkList T;
 int a,n,i;
 ElemType x, e;
 printf("Please input the init size of the linklist:\n");
 scanf("%d",&n);
 printf("Please input the %d element of the linklist:\n", n);
 if(___________________________)     // 判断链表是否创建成功,请填空
 {
	 printf("A Link List Has Created.\n");
	 LoadLink_L(T);
 }
 while(1)
	{
		printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
		scanf("%d",&a);
		switch(a)
		{
			case 1: scanf("%d%d",&i,&x);
				  if(___________________________) printf("Insert Error!\n"); // 判断i值是否合法,请填空
				  else printf("The Element %d is Successfully Inserted!\n", x); 
				  break;
			case 2: scanf("%d",&i);
				  if(___________________________) printf("Delete Error!\n"); // 判断i值是否合法,请填空
				  else printf("The Element %d is Successfully Deleted!\n", e);
				  break;
			case 3: LoadLink_L(T);
				  break;
			case 0: return 1;
		}
	}
}



 

输入格式

测试样例格式说明:
根据菜单操作:
1、输入1,表示要实现插入操作,紧跟着要输入插入的位置和元素,用空格分开
2、输入2,表示要实现删除操作,紧跟着要输入删除的位置
3、输入3,表示要输出顺序表的所有元素
4、输入0,表示程序结束


 

输入样例

3
3 6 9
3
1
4 12
2
1
3
0


 

输出样例

Please input the init size of the linklist:
Please input the 3 element of the linklist:
A Link List Has Created.
The LinkList is:3 6 9 
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The LinkList is:3 6 9 
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 12 is Successfully Inserted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 3 is Successfully Deleted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The LinkList is:6 9 12 
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:


 

作者

 yqm
 

Version: 

2

#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1 
#define ElemType int

typedef struct LNode
{
	int data;
	int length=0;
	struct LNode* next;
}LNode, * LinkList;

int CreateLink_L(LinkList& L, int n) {
	// 创建含有n个元素的单链表
	LinkList p, q;
	int i;
	ElemType e;
	L = new LNode;
	L->next = NULL;              // 先建立一个带头结点的单链表
	q = L;
	for (i = 0; i < n; i++) {
		scanf("%d", &e);
		p = new LNode;  // 生成新结点
		// 请补全代码
		p->data = e;
		p->next = NULL;
		q->next = p;
		q = p;
		L->length++;
	}
	return OK;
}

int LoadLink_L(LinkList& L) {
	// 单链表遍历
	LinkList p = L->next;
	if (p==NULL)printf("The List is empty!"); // 请填空
	else
	{
		printf("The LinkList is:");
		while (p!=NULL)    // 请填空
		{
			printf("%d ", p->data);
			p = p->next;    // 请填空
		}
	}
	printf("\n");
	return OK;
}

int LinkInsert_L(LinkList& L, int i, ElemType e) {
	// 算法2.9
	// 在带头结点的单链线性表L中第i个位置之前插入元素e
	// 请补全代码
	LinkList p = L,p0=new LNode;
	if (i<1 || i>L->length + 1) return ERROR;
	for (int j = 1; j < i; j++)
		p = p->next;
	p0->data = e;
	p0->next = p->next;
	p->next = p0;
	L->length++;
	return OK;
}

int LinkDelete_L(LinkList& L, int i, ElemType& e) {
	// 算法2.10
	// 在带头结点的单链线性表L中,删除第i个元素,并用e返回其值
	// 请补全代码
	LinkList p = L;
	if (i<1 || i>L->length) return ERROR;
	for (int j = 1; j < i; j++)
		p = p->next;
	e = p->next->data;
	p->next = p->next->next;
	L->length--;
	return OK;
}

int main()
{
	LinkList T;
	int a, n, i;
	ElemType x, e;
	printf("Please input the init size of the linklist:\n");
	scanf("%d", &n);
	printf("Please input the %d element of the linklist:\n", n);
	if (CreateLink_L(T,n))     // 判断链表是否创建成功,请填空
	{
		printf("A Link List Has Created.\n");
		LoadLink_L(T);
	}
	while (1)
	{
		printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
		scanf("%d", &a);
		switch (a)
		{
		case 1: scanf("%d%d", &i, &x);
			if (!LinkInsert_L(T,i,x)) printf("Insert Error!\n"); // 判断i值是否合法,请填空
			else printf("The Element %d is Successfully Inserted!\n", x);
			break;
		case 2: scanf("%d", &i);
			if (!LinkDelete_L(T,i,e)) printf("Delete Error!\n"); // 判断i值是否合法,请填空
			else printf("The Element %d is Successfully Deleted!\n", e);
			break;
		case 3: LoadLink_L(T);
			break;
		case 0: return 1;
		}
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值