在链表指定位置插入结点

在链表指定位置插入结点

问题描述:
输入若干(不超过100个)非负整数,创建一个不带头结点的单向链表。
再输入一个位置index以及一个数据data,程序中首先创建一个新结点s,s的数据成员为data,然后调用函数insertNode将s插入到链表的指定位置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<iostream>
using namespace std;

struct ListNode{
	int num;
	struct ListNode *next;
};

ListNode *createByTail(){
    ListNode *head;
    ListNode *p1,*p2;
    int n=0,num;
    cin>>num;
    head=NULL;
    if(num==-1){
    	return NULL;
	}
    while(num!=-1){
        p1=new ListNode;
        p1->num=num;
        n=n+1;
        if(n==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        cin>>num;
    }
    p2->next=NULL;
    return head;
}

ListNode *insert(ListNode *head,int locate,int data){
	ListNode *p=head,*pre=head,*p1;
	int n=0;
	while(p){
		n++;
		p=p->next;
	}
	if(locate<=0||locate>n+1){
		return head;
	}
	p=head;
	if(locate==1){
		p1=new ListNode;
		p1->num=data;
		p1->next=head;
		head=p1;
		return head;
	}
	int count=1;
	while(count!=locate-1){
		count++;
		p=p->next;
	}
	p1=new ListNode();
	p1->num=data;
	p1->next=p->next;
	p->next=p1;
	return head;
}

void  displayLink(ListNode *head){
    ListNode *p;
    p=head;
    cout<<"head-->";
    while(p!= NULL){
        cout<<p->num<<"-->";
        p=p->next;
    }
    cout<<"tail\n";
}
int main(){
	ListNode *head=createByTail();
	int index,data;
	while(cin>>index>>data){
		head=insert(head,index,data);
	    displayLink(head);
	}	
}
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是Python代码,用于在链表指定位置插入元素: ```python class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def insert(self, data, position): if position < 0: print("Invalid position") return new_node = Node(data) if position == 0: new_node.next = self.head self.head = new_node return current = self.head current_position = 0 while current_position < position - 1: current = current.next current_position += 1 if current is None: break if current is None: print("Invalid position") return new_node.next = current.next current.next = new_node def display(self): current = self.head while current: print(current.data, end=" ") current = current.next print() # Create a linked list linked_list = LinkedList() # Insert elements linked_list.insert(1, 0) linked_list.insert(2, 0) linked_list.insert(3, 1) # Display the linked list linked_list.display() ``` 在上面的代码中,我们首先定义了`Node`类和`LinkedList`类。`Node`类表示链表中的节点,它包含一个数据项和一个指向下一个节点的指针。`LinkedList`类表示链表本身,它包含一个指向链表头部的指针。 我们定义了一个`insert()`方法,它接受两个参数:要插入的数据和要插入位置。如果要插入位置小于0,则打印一条错误消息并返回。否则,我们创建一个节点,并检查要插入位置是否为0。如果是,则将节点插入链表头部。否则,我们遍历链表,找到要插入位置(如果它是有效的)。一旦找到位置,我们将节点插入链表中。 最后,我们定义了一个`display()`方法,它遍历链表并打印每个节点的数据项。我们创建一个链表对象,并插入一些元素。然后,我们调用`display()`方法以显示链表中的元素。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值