[数据结构]在链表指定位置插入结点(链表)

在链表指定位置插入结点(链表)

问题描述 :

输入若干(不超过100个)非负整数,创建一个不带头结点的单向链表。
再输入一个位置index以及一个数据data,程序中首先创建一个新结点s,s的数据成员为data,然后调用函数insertNode将s插入到链表的指定位置index处,最后输出结果链表。
请编写insertNode函数,完成插入操作。insertNode函数的原型如下:

struct student *insertNode(struct student *head, struct student *s, int index)

输入说明 :

首先输入若干非负整数,每个整数作为数据放入一个链表结点中,输入-1表示输入结束。
然后输入若干组整数,每组一行,每行包含两个整数,第一个表示需要插入位置index,第二个为新结点的数据。如:输入“1 5”表示在链表的第一个位置(最前面)插入一个结点,结点的数据为5。

输出说明 :

对于每组输入,输出插入结点之后的结果链表。输出的信息以head开头,以tail结尾,以“–>”分隔。具体见输出范例。
如果输入的index超出了链表的范围(如果链表中已有n个结点,则index的有效范围为 1<= index <= n+1),则不插入,输出原链表。如果是空链表,则直接输出“head–>tail”。

输入范例 :

1 2 3 -1
1 10
3 20
输出范例 :

head–>10–>1–>2–>3–>tail
head–>10–>1–>20–>2–>3–>tail

#include <stdio.h>
#include <stdlib.h>
struct student
{
	int  num;
	struct student  *next;
};
//从键盘读入数据创建链表,新结点插入到尾部
struct student *createByTail()
{
	struct student *head;
	struct student *p1,*p2;
	int n;
	n=0;
	p1=p2=(struct student*)malloc(sizeof(struct student));
	scanf("%d",&p1->num);
	head=NULL;  //首先置链表为空链表
	while(p1->num!=-1)    //num为-1,意味着用户输入结束
	{
		n=n+1;
		if(n==1)            //创建第一个结点
			head=p1;
		else
			p2->next=p1;
		p2=p1;            //p2始终指向最后一个结点(即尾指针)
		p1=(struct student*)malloc(sizeof(struct student)); //p1指向新结点
        scanf("%d",&p1->num);
	}
	p2->next=NULL;  //最后一个结点的next赋值为NULL
	return head;
}
//输出链表中的信息(num)
void  displayLink(struct student *head)
{
	struct student *p;
    p=head;
	printf("head-->");
	while(p!= NULL)
	{
		printf("%d-->",p->num);
		p=p->next;
	}
	printf("tail\n");
}
//在链表中第index处插入s指针所指向的结点。index从1开始。
//由于可能插在第一个结点,所以函数返回头指针给主调函数
struct student *insertNode(struct student *head, struct student *s, int index)
{
    struct student *p;
    p=head;
    int len=0;
    while(p!=NULL){
        len++;
        p=p->next;
    }
    
    if(index>len+1 || index<=0){//超出长度
        return head;
    }else{//不超出长度 
        p=head;
        int tag=1;
        while(tag!=index){
            p=p->next;
            tag++;
        }
        
        struct student *q;
        if(p==head){
            s->next=head;
            head=s;
        }else{
            q=head;
            while(q->next!=p){
                q=q->next;
            }
            q->next=s;
            s->next=p;
        }
        
    }
    return head;

}
int main()
{
    struct student *head;
    int index,data;
	head=createByTail();
	while(scanf("%d%d", &index,&data) != EOF)
    {
        struct student * s = (struct student *) malloc(sizeof(struct student));
        s->num = data;
        head=insertNode(head, s, index);
        displayLink(head);
    }
    return 0;
}
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
双向链表 (Doubly Linked List) 是一种常见的数据结构,它的每个结点都有两个指针,分别指向前一个结点和后一个结点。这种结构使得双向链表可以在 O(1) 时间内进行插入、删除和查找操作。 按位置查找双向链表的代码实现如下: ```c++ #include <iostream> using namespace std; struct Node { int val; Node* prev; Node* next; }; class DoublyLinkedList { private: Node* head; Node* tail; public: DoublyLinkedList() { head = tail = nullptr; } void insert(int pos, int val) { Node* newNode = new Node(); newNode->val = val; if (head == nullptr) { head = tail = newNode; return; } if (pos <= 0) { newNode->next = head; head->prev = newNode; head = newNode; return; } Node* cur = head; int i = 0; while (cur != nullptr && i < pos) { cur = cur->next; i++; } if (cur == nullptr) { newNode->prev = tail; tail->next = newNode; tail = newNode; } else { newNode->prev = cur->prev; newNode->next = cur; cur->prev->next = newNode; cur->prev = newNode; } } Node* find(int pos) { if (pos < 0) return nullptr; Node* cur = head; int i = 0; while (cur != nullptr && i < pos) { cur = cur->next; i++; } return cur; } }; int main() { DoublyLinkedList dll; dll.insert(0, 1); dll.insert(1, 2); dll.insert(2, 3); Node* node = dll.find(1); if (node != nullptr) { cout << node->val << endl; } else { cout << "Not found" << endl; } return 0; } ``` 上面的代码中,我们定义了一个 `DoublyLinkedList` 类,其中包含了插入和查找操作。在插入操作中,我们首先判断链表是否为空,如果为空,则直接将新结点作为链表头部;否则,我们遍历链表找到插入位置,然后将新结点插入到该位置。 在查找操作中,我们同样遍历链表找到指定位置结点,并返回该结点的指针。注意,在双向链表中,我们可以从头部或尾部开始遍历,这里我们选择从头部开始遍历。 在主函数中,我们创建了一个双向链表插入了三个结点,并查找了第二个结点,输出结果为 `2`。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值