单链表的代码结构
定义结构体表示单链表->初始化单链表->尾部插入/中间插入->删除节点->打印链表
尾插图
中间插入图
删除图
头文件,基本的头文件都在里面,可能有些遗漏,自己可以添加。
#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);
}
个人总结,大神可以给点建议。