c语言:单链表的实现(一) 创建,插入,删除,查找

本文详细介绍了如何使用C语言实现单链表,包括创建、插入、删除和查找元素的操作。通过示例代码展示了如何进行这些基本操作,并提供了主函数供用户交互操作链表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<stdio.h>
#include<math.h>
#define NULL 0
#define LEN sizeof(struct Student)
using namespace std;
typedef struct Student
{
	int length;
	int num;
	struct Student *next;
}Student; 
typedef Student *Linklist;    //头指针
void Error(char *s)           //错误指示
{
	std::cout << s << endl;
	exit(1);
}
Student *creat(void)          //创建链表
{
	Student *head;
	Student *p, *q;
    int	n = 0;
	 q = (Student*)malloc(LEN);
	 p = q;
	 cout << "请输入元素:" << endl;
	 cin >> p->num;
	head = NULL;
	while (p->num != 0)
	{
		n = n + 1;
		if (n == 1)
		{
			head = p;
			head->length = 1;
		}
		else
		{
			q->next = p;
			q = p;
			p = (Student*)malloc(LEN);
			cin >> p->num;
			head->length++;
		}
	}
	q->next = NULL;
	return head;
	cout << "链表创建成功!" << endl;
}

void Insertlist(Linklist &L, int i, int stu)
//向链表中第i个位置插入元素stu
{
	Student *p =L;              //将指针头传给指针p  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值