7-5 插入元素

输入n个整数(1<=n<100),并在指定位置p处插入新元素,输出最后的结果.

输入格式:

每个测试包含2行输入.第1行n,p,q,其中p,q分别表示需要插入的位置和需要插入的元素值(1<=p<=n+1, 且q符合int型数据范围). 第2行表示n个整数. n,p,q均为0时,表示测试结束.

输出格式:

输出最后的结果

输入样例:

在这里给出一组输入。例如

5 1 4
1 2 3 4 5
6 3 9
2 4 6 8 0 2
0 0 0

输出样例:

在这里给出相应的输出。例如

4 1 2 3 4 5
2 4 9 6 8 0 2

 代码(有注释)

#include<stdio.h>
int main()
{
	int a,d,f,g,i,t,j;
	int b[100],c[200];
	
	while(1)                      //老规矩运用while循环 
	{
		scanf("%d %d %d",&d,&f,&g);    //f,g分别表示需要插入的位置和需要插入的元素值,d表示输入数组的数据个数 
		if(d==0&&f==0&&g==0)break;     //当d,f,g都为0时结束while循环 
		
		for(i=0;i<d;i++){
			scanf("%d",&b[i]);         //输入数据到b[100]中 
		}
		
		for(j=0;j<d;j++){              //输出数组的数据                        
			if(j==f-1){                //当数组输出到需要插入的元素的位置时 
				printf("%d ",g);       //输出需要插入的元素 
			}
			if(j<d-1){                 //输出剩下的数据 
				printf("%d",b[j]);     //这里不建议使用“printf("%d ",b[j]);”,至于为什么你可以尝试一下 
				printf(" ");
			}else{
				printf("%d",b[j]);     //去除末尾多余的空格 
			}	
		}
		printf("\n");
		
	}
	return 0;	
}

代码(无注释)

#include<stdio.h>
int main()
{
	int a,d,f,g,i,t,j;
	int b[100],c[200];
	
	while(1)
	{
		scanf("%d %d %d",&d,&f,&g);
		if(d==0&&f==0&&g==0)break;
		for(i=0;i<d;i++){
			scanf("%d",&b[i]);
		}
		
		for(j=0;j<d;j++){
			if(j==f-1){
				printf("%d ",g);
			}
			if(j<d-1){
				printf("%d",b[j]);
				printf(" ");
			}else{
				printf("%d",b[j]);
			}	
		}
		printf("\n");
		
	}
	return 0;	
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表是一种常见的数据结构,它由一些节点组成,每个节点包含一个数据项和指向下一个节点的指针。链表中的数据项可以是任何类型的数据,例如整数、字符串等。链表插入元素和删除元素是链表操作中常见的两种操作。 链表插入元素 链表插入元素操作可以分为两种情况: 1. 在链表头插入元素 在链表头插入元素是最简单的一种情况,因为只需要将新元素插入到链表头部即可。具体步骤如下: (1)创建一个新节点,将需要插入元素赋值给新节点的数据项。 (2)将新节点的指针指向原链表的头节点。 (3)将新节点设置为链表的新头节点。 示例代码: ``` struct Node { int data; struct Node* next; }; void insertAtBeginning(struct Node** head_ref, int new_data) { /* 1. create new node */ struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); /* 2. put in the data */ new_node->data = new_data; /* 3. Make next of new node as head */ new_node->next = (*head_ref); /* 4. move the head to point to the new node */ (*head_ref) = new_node; } ``` 2. 在链表中间或尾部插入元素 在链表中间或尾部插入元素需要先找到要插入位置,然后将新元素插入到该位置之后。具体步骤如下: (1)创建一个新节点,将需要插入元素赋值给新节点的数据项。 (2)遍历链表,找到要插入位置。 (3)将新节点的指针指向该位置的下一个节点。 (4)将该位置的节点的指针指向新节点。 示例代码: ``` void insertAfter(struct Node* prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf("the given previous node cannot be NULL"); return; } /* 2. create new node */ struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); /* 3. put in the data */ new_node->data = new_data; /* 4. Make next of new node as next of prev_node */ new_node->next = prev_node->next; /* 5. move the next of prev_node as new_node */ prev_node->next = new_node; } ``` 链表删除元素 链表删除元素操作也可以分为两种情况: 1. 删除链表头元素 删除链表头元素需要将链表的头指针指向下一个节点即可。具体步骤如下: (1)如果链表为空,则返回。 (2)将头节点指针指向下一个节点。 (3)释放原头节点的内存空间。 示例代码: ``` void deleteAtBeginning(struct Node** head_ref) { /* 1. check if the list is empty */ if (*head_ref == NULL) return; /* 2. Store head node */ struct Node* temp = *head_ref; /* 3. Change head to next node */ *head_ref = (*head_ref)->next; /* 4. free the old head node */ free(temp); } ``` 2. 删除链表中间或尾部元素 删除链表中间或尾部元素需要先找到要删除的节点,然后将该节点的前一个节点的指针指向该节点的下一个节点即可。具体步骤如下: (1)如果链表为空,则返回。 (2)遍历链表,找到要删除的节点。 (3)将该节点的前一个节点的指针指向该节点的下一个节点。 (4)释放该节点的内存空间。 示例代码: ``` void deleteNode(struct Node** head_ref, int key) { /* 1. check if the list is empty */ if (*head_ref == NULL) return; /* 2. store head node */ struct Node* temp = *head_ref, *prev; /* 3. if head node itself holds the key to be deleted */ if (temp != NULL && temp->data == key) { *head_ref = temp->next; /* change head */ free(temp); /* free old head */ return; } /* 4. search for the key to be deleted, keep track of the previous node as we need to change 'prev->next' */ while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } /* 5. if key was not present in linked list */ if (temp == NULL) return; /* 6. unlink the node from linked list */ prev->next = temp->next; /* 7. free memory */ free(temp); } ``` 以上就是链表插入元素和删除元素的详细介绍和示例代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值