数据结构之单链表——带有节点的单链表的创建、插入和删除(C/C++)

// 带有头节点的单链表的创建、插入和删除
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

typedef int DataType;

typedef struct LNode{	
	DataType data;
	struct LNode *next;
}LNode,*LinkList;

// 这里的函数参数必须是链表的指针的地址,否则为局部变量无法将链表指针带回主程序
// 创建带头节点的单链表
void CreatListF( LinkList *head)
{
	*head=(LNode*)malloc(sizeof(LNode));
	(*head)->next=NULL;

	if(!*head)
		printf("debug\n");
}

// 从小到大插入
void InsertList(LinkList head,DataType data)
{
	LinkList p=head,q;
	LNode *s;
	
	q=p;
	p=p->next;
	while(p && p->data<data)
	{
		q=p;
		p=p->next;
	}
	s=(LNode*)malloc(sizeof(LNode));
	s->data=data;
	
	s->next=p;
	q->next=s;
	
}

// 删除,注意函数查不到的情况
int DeleteList(LinkList head,DataType data)
{
	LinkList p=head,q;
	q=p;
	p=p->next;
	while(p && p->data!=data)
	{
		q=p;
		p=p->next;
	}
	
	if(!p)
		return 0;// 为找不到到
	
	q->next=p->next;
	
	free(p);// 释放指针
	
	p=NULL;// 释放后指针指向NULL
	
	return 1;
}
void PrintList(LinkList head)
{
	LinkList p=head;
	while(p->next != NULL)
	{		
		p=p->next;
		printf("%d->",p->data);
	}
	printf("end\n");
}

// 主程序
void main()
{
	LinkList head=NULL;
	CreatListF(&head);// 这里很重要
	
	PrintList(head);

	InsertList(head,10);
	PrintList(head);	
	InsertList(head,20);
	PrintList(head);
	InsertList(head,30);
	PrintList(head);
	InsertList(head,15);
	PrintList(head);
	
	DeleteList(head,15);
	PrintList(head);
	
	DeleteList(head,15);
	PrintList(head);
}
运行结果:

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是带有节点单链表创建插入删除和查找功能的 C 语言实现: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点 typedef struct Node { int data; struct Node *next; } Node; // 创建带有节点单链表 Node* createList() { Node *head = (Node*)malloc(sizeof(Node)); // 创建节点 head->next = NULL; // 初始化头节点的指针域为 NULL return head; // 返回头节点 } // 在链表末尾插入节点 void insert(Node *head, int data) { Node *p = head; while (p->next != NULL) { // 找到链表末尾 p = p->next; } Node *newNode = (Node*)malloc(sizeof(Node)); // 创建节点 newNode->data = data; // 初始化新节点的数据域 newNode->next = NULL; // 初始化新节点的指针域为 NULL p->next = newNode; // 将新节点插入到末尾 } // 删除链表中第一个值为 data 的节点 void delete(Node *head, int data) { Node *p = head->next; // 指向第一个节点 Node *prev = head; // 指向第一个节点的前一个节点,初始为头节点 while (p != NULL && p->data != data) { // 查找第一个值为 data 的节点 prev = p; p = p->next; } if (p != NULL) { // 若找到节点,则删除节点 prev->next = p->next; free(p); } } // 查找链表中第一个值为 data 的节点,返回节点指针;若未找到,则返回 NULL Node* find(Node *head, int data) { Node *p = head->next; // 指向第一个节点 while (p != NULL && p->data != data) { // 查找第一个值为 data 的节点 p = p->next; } return p; } // 输出链表中所有节点的数据 void printList(Node *head) { Node *p = head->next; // 指向第一个节点 while (p != NULL) { // 遍历链表 printf("%d ", p->data); p = p->next; } printf("\n"); } int main() { Node *head = createList(); // 创建带有节点单链表 insert(head, 1); // 在链表末尾插入节点 insert(head, 2); insert(head, 3); insert(head, 4); insert(head, 5); printList(head); // 输出链表中所有节点的数据:1 2 3 4 5 delete(head, 3); // 删除链表中第一个值为 3 的节点 printList(head); // 输出链表中所有节点的数据:1 2 4 5 Node *p = find(head, 4); // 查找链表中第一个值为 4 的节点 printf("%d\n", p->data); // 输出节点的数据:4 return 0; } ``` 上述代码中,我们使用 `typedef` 关键字定义了链表节点类型 `Node`,其中包含数据域 `data` 和指针域 `next`,用于指向下一个节点。在创建链表时,我们需要先创建一个头节点,其数据域可以不使用,而其指针域应该初始化为 NULL。在插入节点时,我们需要遍历链表,找到末尾节点,然后将新节点插入到末尾节点的后面。在删除节点时,我们需要遍历链表,找到第一个值为 data 的节点,并将其从链表中删除。在查找节点时,我们需要遍历链表,找到第一个值为 data 的节点,并返回其指针;若未找到,则返回 NULL。最后,在程序结束时,我们需要释放链表中所有节点的内存空间。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值