数据结构示例之链表在指定节点后插入新元素

以下是“链表在指定节点后插入新元素”的简单示例:

1. 用c语言实现的版本

#include<stdio.h>
#include<stdlib.h>

typedef   char  datatype;
typedef	  struct	node{
	datatype	  data;
	struct 	node  *next;
} listnode;
typedef  listnode  *linklist;
listnode  *p;

/* 创建链表,从表头插入新元素 */
linklist  createlist(void)
{
	char ch;
	linklist  head;
	listnode  *p;
	head = NULL; /*初始化为空*/

	printf("请输入字符序列:\n");
	ch = getchar();
	while (ch != '\n')
	{
		p = (listnode*)malloc(sizeof(listnode));/*分配空间*/
		p->data = ch; /*数据域赋值*/
		p->next = head; /*指定后继指针*/
		head = p; /*head指针指定到新插入的结点上*/
		ch = getchar();
	}
	return head;
}

/*在第i个节点后,插入x*/
int insertnode(linklist head, char x, int i)
{
	int j = 0;
	listnode  * p, *s;
	p = head;

	while (p && j <(i - 1))
	{
		p = p->next;
		++j;
	}

	if (!p || j>(i - 1))
	{
		return -1;
	}

	s = (linklist)malloc(sizeof(listnode));
	s->data = x;
	s->next = p->next;
	p->next = s;

	return 1;
}

void main()
{
	linklist list, head;
	int i;
	char x;

	/* 创建链表,从表头插入新元素 */
	list = createlist();
	head = list;
	printf("在插入前,输出链表中的元素\n");
	do
	{
		printf("%c", head->data);
		head = head->next;
	} while (head != NULL);

	printf("\n要在其后插入新节点的节点位置:");
	scanf("%d", &i);
	printf("要插入的值:");
	getchar();
	scanf("%c", &x);
	/*在第i个节点后,插入x*/
	int res = insertnode(list, x, i);
	if (res == -1)
	{
		printf("插入失败\n");
	}
	else
	{
		printf("[%c]插入成功\n", x);
	}

	printf("在插入后,输出链表中的元素\n");
	do
	{
		printf("%c", list->data);
		list = list->next;
	} while (list != NULL);

	printf("\n");
}

运行结果如下图所示:


  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表是一种常用的数据结构,它由一系列节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。链表的尾插入法是一种将新元素添加链表尾部的方法。 首先,我们需要定义一个节点结构体来表示链表节点: ```c typedef struct Node { int data; // 数据 struct Node* next; // 指向下一个节点的指针 } Node; ``` 然后,我们定义一个链表结构体来表示整个链表,并且保存头节点和尾节点的指针: ```c typedef struct LinkedList { Node* head; // 头节点指针 Node* tail; // 尾节点指针 } LinkedList; ``` 链表的尾部插入法的实现步骤如下: 1. 创建一个新节点,并为其分配内存。 2. 将新节点的数据设置为要插入的元素。 3. 将新节点的指针指向 NULL,表示它是链表的最后一个节点。 4. 如果链表为空,将新节点设置为头节点和尾节点。 5. 否则,将新节点追加到链表的尾部,即将当前尾节点的指针指向新节点,并将新节点设置为新的尾节点。 下面是一个示例代码来演示链表尾部插入法: ```c #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } Node; typedef struct LinkedList { Node* head; Node* tail; } LinkedList; // 初始化链表 void initLinkedList(LinkedList* list) { list->head = NULL; list->tail = NULL; } // 在链表尾部插入元素 void insertAtTail(LinkedList* list, int data) { // 创建新节点 Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; // 如果链表为空,设置新节点为头节点和尾节点 if (list->head == NULL) { list->head = newNode; list->tail = newNode; } else { // 否则,将新节点追加到尾部 list->tail->next = newNode; list->tail = newNode; } } // 打印链表元素 void printLinkedList(LinkedList* list) { Node* current = list->head; printf("Linked List: "); while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { LinkedList list; initLinkedList(&list); // 在尾部插入元素 insertAtTail(&list, 1); insertAtTail(&list, 2); insertAtTail(&list, 3); insertAtTail(&list, 4); // 打印链表元素 printLinkedList(&list); return 0; } ``` 这段代码创建了一个链表,并使用尾部插入插入了四个元素。最后,打印出链表的元素。 希望这个介绍可以帮助你理解链表尾部插入法的实现。如果有任何问题,请随时提问!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值