【数据结构_链表_List_0952】单链表插入实践的操作

建立长度为n的单链表,在第i个结点之前插入数据元素data。

第一行为自然数n,表示链式线性表的长度; 
第二行为n个自然数表示链式线性表各元素值; 
第三行为指定插入的位置i;第四行为待插入数据元素data。

指定插入位置合法时候,输出插入元素后的链式线性表的所有元素,元素之间用一个空格隔开。输入不合法,输出"error!"。

----------------------------

1

1

1

2

------------------------------

2 1




#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
struct node
{
	int data;
	struct node *next;
};
int main()
{
	struct node *head,*p,*q,*temp,*t;
	int num,insertnum,insert;
	while(cin>>num)
	{
	head=(struct node*)malloc(sizeof(struct node));
	p=head;
	for(int i=0;i<num;i++)
	{
		q=(struct node*)malloc(sizeof(struct node));
		cin>>q->data;
		p->next=q;
		p=q;
	}
	p->next=NULL;
	temp=head;
	cin>>insertnum>>insert;
	if(insertnum>num) 
	{
		cout<<"error!";
		break;
	}
	else
	{
		int j=0;
		while(j<insertnum-1)
		{
			j++;
			temp=temp->next;
		}
		if(temp==NULL) return false;//内存不存在,终结;
		t=(struct node *)malloc(sizeof(struct node));
		t->data=insert;
		t->next=temp->next;
		temp->next=t;
	}
	temp=head;
	while(temp->next!=NULL)
	{
		cout<<temp->next->data<<" ";
		temp=temp->next;
	}
	}
	return 0;
}

--------------------------

i下面贴一个不是我写的版本的版本

-------------------------

#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct node
{
  ElemType Data;
    struct node *next;
}SqList;
void creatlistR(SqList *&L,int n)
{
   SqList *r,*s;
   int i;
   L=(struct node*)malloc(sizeof(struct node));
   r=L;
   for(i=0;i<n;i++)
   {
       s=(node*)malloc(sizeof(node));
       scanf("%d",&s->Data);
       r->next=s;
       r=s;
   }
   r->next=NULL;
}
bool ListInsert( node *&L,int i,int date)
{
  SqList *p=L,*s;
  int j=0;
  while(j<i-1 && p!=NULL)
  {
      j++;
      p=p->next;
  }
  if(p == NULL)
  {
      return false;
  }
  else 
  {
      s=(struct node*)malloc(sizeof(struct node));
      s->Data=date;
      s->next=p->next;
      p->next=s;
      return true;
  }
}
int main()                              
{
    int n,i;
    scanf("%d",&n);
     SqList *L;
   creatlistR(L,n);
   scanf("%d",&i);
   ElemType data;
   scanf("%d",&data);
    
  if(ListInsert(L,i,data)==false)
      printf("error!");
  else
  {
    SqList *p=L->next;
    while(p!=NULL)
    {
 
        printf("%d ",p->Data);
        p=p->next;
    }
  }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C语言中,单链表是一种常见的数据结构,它由一系列节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。插入操作是向链表中添加新节点的过程。 要在单链表插入一个新节点,需要进行以下步骤: 1. 创建一个新节点,并为其分配内存空间。 2. 将要插入的数据赋值给新节点的数据域。 3. 将新节点的指针域指向原链表插入位置的下一个节点。 4. 将原链表插入位置的前一个节点的指针域指向新节点。 下面是一个示例代码,演示了如何在单链表插入一个新节点: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构 struct Node { int data; struct Node* next; }; // 在链表插入新节点 void insertNode(struct Node** head, int newData) { // 创建新节点 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = newData; newNode->next = NULL; // 如果链表为空,则将新节点作为头节点 if (*head == NULL) { *head = newNode; return; } // 找到插入位置的前一个节点 struct Node* prevNode = *head; while (prevNode->next != NULL) { prevNode = prevNode->next; } // 将新节点插入链表 prevNode->next = newNode; } // 打印链表 void printList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } printf("\n"); } int main() { struct Node* head = NULL; // 插入节点 insertNode(&head, 1); insertNode(&head, 2); insertNode(&head, 3); // 打印链表 printf("链表内容:"); printList(head); return 0; } ``` 这段代码创建了一个单链表,并在其中插入了三个节点。最后,通过调用`printList`函数,打印出链表的内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值