创建一个有序链表

#include <iostream>
using namespace std;

struct Node//节点结构体
{
	int n;
	Node *next;
};

void Insert(Node *H, int n);//有序插入节点函数
void Show(Node *H);//输出链表

int main(int argc, char const *argv[])
{
	int n;
	Node *HeadPointer;//定义头指针
	Node *HeadNode = new Node;//开辟头节点

	HeadPointer = HeadNode;//头指针指向头节点
	HeadNode->next = NULL;//头节点的下一个位置为空

	for (int i = 0; i < 10; i++)
	{
		cout << "input " << i+1 << "th num" << endl;
		cin >> n;
		Insert(HeadNode, n);//依次插入10个节点
	}
	Show(HeadPointer);//输入构造的有序链表
	return 0;
}

void Insert(Node *H, int n)//传递头节点指针和将要插入的元素
{
	Node *pre = H;//pre为传递过来的头节点的指针,
	Node *cur = H->next;//cur表示当前位置,这一句很重要,不然插入到最后时链表无法终止

	Node *_new = new Node;
	_new->n = n;//建立新的节点

	while (cur && n > cur->n)//如果链表不为空,且输入的元素比当前元素大
	{
		pre = cur;
		cur = cur->next;//循环得到中断位置的指针
	}

	_new->next = cur;
	pre->next = _new;//将新节点插入中断位置
}

void Show(Node *H)
{
	Node *p = H->next;//由于头节点数据区没有具体元素,所有输出时跳过头结点
	while (p)
	{
		cout << p->n << " ";
		p = p->next;
	}
}

好的,我可以为您介绍一下如何使用 C 语言创建一个有序链表,并对其进行增、删、查、改操作。 首先,我们需要定义链表节点的结构体,包括一个数据域和一个指向下一个节点的指针域,如下所示: ``` typedef struct node { int data; struct node *next; } Node; ``` 接下来,我们可以定义一个链表的头结点,以及一些操作函数。 创建有序链表: ``` Node *createList() { Node *head = NULL; int data; printf("请输入节点的值,输入-1结束:\n"); scanf("%d", &data); while (data != -1) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; if (head == NULL) { head = newNode; } else { Node *current = head; Node *previous = NULL; while (current != NULL && current->data < data) { previous = current; current = current->next; } if (previous == NULL) { newNode->next = head; head = newNode; } else { previous->next = newNode; newNode->next = current; } } printf("请输入节点的值,输入-1结束:\n"); scanf("%d", &data); } return head; } ``` 在这个函数中,我们首先定义头结点为 NULL,然后循环读入节点的值,创建新的节点,并将其插入到链表中。我们将节点按照从小到大的顺序插入,因此需要使用类似插入排序的算法。如果当前链表为空,则将新节点作为头结点。否则,从头结点开始遍历链表,找到插入位置,然后插入新节点。 插入节点: ``` void insertNode(Node *head, int data) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; if (head == NULL) { head = newNode; } else { Node *current = head; Node *previous = NULL; while (current != NULL && current->data < data) { previous = current; current = current->next; } if (previous == NULL) { newNode->next = head; head = newNode; } else { previous->next = newNode; newNode->next = current; } } } ``` 这个函数与创建链表函数类似,只是我们不需要重复输入节点的值。我们将要插入的节点按照同样的方式插入到链表中。 删除节点: ``` void deleteNode(Node *head, int data) { Node *current = head; Node *previous = NULL; while (current != NULL && current->data != data) { previous = current; current = current->next; } if (current == NULL) { printf("没有找到要删除的节点!\n"); } else { if (previous == NULL) { head = current->next; } else { previous->next = current->next; } free(current); } } ``` 这个函数首先遍历链表,找到要删除的节点。如果找到了,则将其从链表中删除并释放内存。否则,输出提示信息。 查找节点: ``` Node *findNode(Node *head, int data) { Node *current = head; while (current != NULL && current->data != data) { current = current->next; } return current; } ``` 这个函数遍历链表,找到要查找的节点并返回其指针。如果没有找到,则返回 NULL。 修改节点: ``` void modifyNode(Node *head, int oldData, int newData) { Node *current = findNode(head, oldData); if (current == NULL) { printf("没有找到要修改的节点!\n"); } else { current->data = newData; } } ``` 这个函数先调用查找节点的函数,找到要修改的节点。如果找到了,则将其数据域修改为新的值。否则,输出提示信息。 以上就是使用 C 语言创建有序链表并进行增、删、查、改操作的基本代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值