NBUOJ 1800 找位置(链表)

NBUOJ 1800 找位置

Description

做操的时间到了,小明在教室还在思考刚刚老师讲的一道题目,当他想通这个题时,同学们都已经在操场上排好队了,他赶快跑到操场上找到自己的班级队伍,希望尽快找到自己的位置,准备做操,小明记得自己是排在第x学号同学的后面。你能不能来帮小明找到自己的位置呢?

Input

第一行输入: n x y(小明不在时队伍的长度、第x同学的学号、小明的学号)

第二行输入:n个同学的学号(6位,第一位可能为0,最后一位可能为X,每个学号后有空格)

Output

n+1个学号的顺序(小明加入队伍后)每个学号后有空格

Sample Input

4 134123 123483
134812 134123 023133 04583X

Sample Output

134812 134123 123483 023133 04583X

知识点

链表的基本操作(创建,插入,删除,输出)

源码

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

typedef struct node{
	string id; //学号
	struct node* next; //下一位同学
}NODE;

void create_linkedlist(NODE *h,int n)  //创建链表,其中n为同学个数
{
	NODE *p = h;
	NODE *r = new NODE;
	p->next = r;
	p = r;
	for(int i=0;i<n;i++)
	{
		cin>>p->id;
		if(i==n-1)
		{
			p->next = NULL;
			break;
		}
		r = new NODE;
		p->next = r;
		p = r;
	}

}

void print_linkedlist(NODE *h,int n) //输出链表,其中n为小明插入后的同学个数
{
	NODE *p = h->next;
	for(int i=0;i<n;i++)
	{
		cout<<p->id<<" ";
		p=p->next;
	}
	cout<<endl;
}

void insert_linkedlist(NODE *h,string s,string n)  //在某同学后插入小明,s为某同学的学号,n为小明学号 
{
	NODE *p = h->next;
	NODE *r;
	while(p)
	{
		if(p->id == s)
		{
			r = new NODE;
			r->id = n;
			r->next = p->next;
			p->next = r;
			break;
		}
		p=p->next;
	}
}

int main()
{
	NODE *h;
	int n;
	string s1,s2;
	h = new NODE;
	scanf("%d",&n);
	cin>>s1>>s2;
	create_linkedlist(h,n);
	insert_linkedlist(h,s1,s2);
	print_linkedlist(h,n+1);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表是一种常用的数据结构,它由一系列的节点组成,每个节点都包含了一个数据元素及一个指向下一个节点的指针。 要在C语言中插入链表的指定位置,首先需要定义链表节点的结构体,包括数据元素和指向下一个节点的指针。然后创建新节点,并赋值给新节点的数据元素。接下来,需要遍历链表到要插入的位置的前一个节点,并将新节点的指针指向该位置原来的后一个节点。最后,将前一个节点的指针指向新节点,完成插入操作。 具体的插入操作可以用以下代码实现: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点的结构体 typedef struct Node { int data; struct Node* next; } Node; // 插入指定位置的节点 void insertAtPosition(Node** head, int position, int value) { Node* newNode = (Node*)malloc(sizeof(Node)); // 创建新节点 newNode->data = value; // 给新节点赋值 // 如果插入位置链表头部 if (position == 0) { newNode->next = *head; *head = newNode; return; } Node* current = *head; int i; // 遍历链表到要插入位置的前一个节点 for (i = 0; current != NULL && i < position - 1; i++) { current = current->next; } // 如果不到要插入的位置,返回错误 if (current == NULL) { printf("插入位置无效.\n"); return; } // 插入新节点 newNode->next = current->next; current->next = newNode; } // 遍历链表并打印节点的值 void printList(Node* head) { Node* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { Node* head = NULL; // 链表头部 // 在链表尾部插入节点 insertAtPosition(&head, 0, 1); insertAtPosition(&head, 1, 3); insertAtPosition(&head, 2, 5); // 打印链表 printf("链表元素:"); printList(head); return 0; } ``` 以上代码通过`insertAtPosition`函数实现了在链表的指定位置插入节点,并通过`printList`函数打印链表的元素。在`main`函数中测试了在链表尾部插入节点,并打印链表的元素。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值