单链表的基本操作

3 篇文章 0 订阅

单链表的代码结构
定义结构体表示单链表->初始化单链表->尾部插入/中间插入->删除节点->打印链表

尾插图
在这里插入图片描述
中间插入图
在这里插入图片描述
删除图
在这里插入图片描述
头文件,基本的头文件都在里面,可能有些遗漏,自己可以添加。

#ifndef _COMMONHEADER_H_
#define _COMMONHEADER_H_

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <strings.h>
#include <time.h>
#include <errno.h>

#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include <semaphore.h>
#include <signal.h>

#include <pthread.h>

#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>

#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <dirent.h>
#define BUF_SIZE 1024
#include <linux/input.h>
#include <sys/mman.h>
#endif
#include "myhead.h"
//定义结构体表示单向循环链表
struct siglelist
{
	int data;
	char name[10];
	double weight; //前面三个都是属于数据域里面的数据成员
	struct siglelist *next; //指针域
};

//封装链表的初始化
struct siglelist *list_init()
{
	struct siglelist *myhead=malloc(sizeof(struct siglelist));
	myhead->next=myhead;
	return myhead;
}

//尾部插入
int insert_tail(int newdata,char *newname,double newweight,struct siglelist *head)
{
	//找到链表的尾部
	struct siglelist *p=head;
	while(p->next!=head)
		p=p->next; //把赋值=  翻译成指向
	//准备好新的节点
	struct siglelist *newnode=malloc(sizeof(struct siglelist));
	newnode->data=newdata;
	strcpy(newnode->name,newname);
	newnode->weight=newweight;
	newnode->next=NULL;
	
	//p的下一个节点指向newnode
	p->next=newnode;
	newnode->next=head;
}

//中间插入,把newdata插入到olddata的后面
int insert_mid(int newdata,int olddata,struct siglelist *head)
{
	//找到olddata所在的节点
	struct siglelist *p=head;
	while(p->next!=head)
	{
		p=p->next;
		if(p->data==olddata)
			break;
	}
	//准备新节点
	struct siglelist *newnode=malloc(sizeof(struct siglelist));
	newnode->data=newdata;
	strcpy(newnode->name,"赵六");
	newnode->weight=70.5;
	newnode->next=NULL;
	
	newnode->next=p->next;
	p->next=newnode;
}

//单链表的删除,用两个指针解决
int remove_list(int deldata,struct siglelist *head)
{
	int flag=0; //标记是否找到要删除的数据
	//找到要删除的节点
	struct siglelist *q=head;
	struct siglelist *p=head->next;
	while(p->next!=head)
	{
		if(p->data==deldata)
		{
			flag=1;
			q->next=p->next;
			p->next=NULL;
			free(p);
			p=q->next;
		}	
		else
		{
			p=p->next;
			q=q->next;
		}
	}
	//发现前面的循环漏掉了最后一个数据没有判断,没有删除
	if(p->next==NULL && p->data==deldata)
	{
		//把最后一个数据删除即可
		q->next=NULL;
		free(p);
		p=NULL;
		return 0;
	}
	if(flag==0 && p->next==NULL && p->data!=deldata)
	{
		printf("没有你要删除的数据!\n");
		return -1;
	}
}
//打印链表
int show_list(struct siglelist *head)
{
	struct siglelist *p=head;
	while(p->next!=head)
	{
		p=p->next;
		printf("当前遍历的节点中存放的数据是:%d\n",p->data);
		printf("当前遍历的节点中存放的数据名字是:%s\n",p->name);
		printf("当前遍历的节点中存放的数据体重是:%lf\n",p->weight);
	}
}
int main()
{
	int m,n;
	char newname[10];
	double newweight;
	int i;
	//准备头节点
	struct siglelist *mylist=list_init();
	//尾插一部分数据
	for(i=0; i<3; i++)
	{
		printf("输入尾插数据!\n");
		scanf("%d",&m);
		printf("输入尾插数据名字!\n");
		scanf("%s",newname);
		printf("输入尾插数据体重!\n");
		scanf("%lf",&newweight);
		insert_tail(m,newname,newweight,mylist);
	}
	
	//打印 
	printf("=====尾部插入========\n");
	show_list(mylist);
	
	//中间插入
	//insert_mid(23,55,mylist);
	//打印 
	//printf("=====中间插入========\n");
	//show_list(mylist);
	
	//删除数据
	remove_list(1,mylist);
	printf("=====删除完毕========\n");
	show_list(mylist); 
	
	
}

个人总结,大神可以给点建议。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灬逆时针灬诺言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值