单链表的建立,查找,插入,删除,测长,打印,逆置操作实现

本文详细介绍了如何实现单链表的基本操作,包括创建链表、查找节点、在指定位置插入新节点、删除特定节点、计算链表长度、遍历打印链表元素以及链表的逆置过程。这些操作对于理解数据结构中的链表概念至关重要。
摘要由CSDN通过智能技术生成
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<conio.h>

using namespace std;

typedef struct student
{
	int data;
	struct student *next;
}node;


//建立单链表,头插入法;
node *creatlist()
{
	node *head, *p, *s;
	int x; 

	//申请新的存储空间,创立头节点;
	head = (node*)malloc(sizeof(node));
	head->next=NULL;  //重要   指针需要初始化;
	s = head->next;
	
	cout << "用头插法建立单链表, 请输入链表数据, 以 ctrl+z 结束" << endl;

	while ( cin>>x)
	{
		p = (node*)malloc(sizeof(node));
		p->data = x;
		head->next = p;
		p->next = s;
		s = p;
	}

	return(head);
}
2.  单链表中节点的查找操作

//查找给定的值;
node *locate(node *head, int x)
{
	node *p;
	p = head->next;
	while (p != NULL && p->data != x)
	{
		p=p->next;
	}
	if (p == NULL)
		cout << "链表中不存在这个数" << endl;
    return (p);
	
}
3.  单链表上的插入操作
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值