单向链表的,头删,头插,尾删,尾插,按位置插入,按位置删除
头文件:
#ifndef __lianbiao__
#define __lianbiao__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef union{
int length;
int data;
}info;
typedef struct Linklist{
info dt;
struct Linklist *next;
}Chained;
Chained* creat_head(void);
void add_tail(Chained*);
void add_head(Chained*);
void remove_head(Chained*);
void remove_tail(Chained*);
void remove_place(Chained*,int);
void add_place(Chained*,int);
void print_data(Chained*);
#endif
功能实现:
#include "./lianbiao.h"
//创建头结点
Chained* creat_head(void){
Chained *c = (Chained*) malloc(sizeof(Chained));
if(NULL==c)
return NULL;
c->dt.length=0;
c->next=NULL;
return c;
}
//头插
void add_head(Chained *c){
Chained *c1 = (Chained*) malloc(sizeof(Chained));
if(NULL==c1){
printf("存储空间不足,操作失败!\n");
return ;
}
c->dt.data++;
c1->next=c->next;
c->next = c1;
printf("请输入数据\n");
scanf("%d",&c1->dt.data);
}
//头删
void remove_head(Chained *c){
if(NULL==c){
printf("无数据,无法删除!\n");
return ;
}
c->dt.data--;
Chained *c1 = c->next->next;
free(c->next);
c->next = c1;
}
//尾插
void add_tail(Chained* c){
Chained *c1 = (Chained*) malloc(sizeof(Chained));
if(NULL==c1){
printf("存储空间不足,操作失败!\n");
return ;
}
c->dt.data++;
for(;c->next!=NULL;c=c->next);
c->next = c1;
c1->next = NULL;
printf("请输入数据\n");
scanf("%d",&c1->dt.data);
}
//尾删
void remove_tail(Chained* c){
if(NULL==c->next){
printf("无数据,无法删除!\n");
return ;
}
c->dt.data--;
for(;c->next->next!=NULL;c=c->next);
free(c->next);
c->next=NULL;
}
//按位置删除
void remove_place(Chained* c,int place){
if(NULL==c->next){
printf("无数据,无法删除!\n");
return ;
}
if(place>=c->dt.length){
remove_tail(c);
return;
}else if(place<=1){
remove_head(c);
return;
}
c->dt.length--;
for(int i=0;i<place-1;i++)
c=c->next;
Chained *c1 = c;
c=c->next->next;
free(c1->next);
}
//按位置插入
void add_place(Chained* c,int place){
if(NULL==c){
printf("存储空间不足,操作失败!\n");
return ;
}
if(place>c->dt.length){
add_tail(c);
return;}
else if(place<=0){
add_head(c);
return;
}
c->dt.length++;
for(int i=0;i<place-1;i++)
c=c->next;
Chained *c1 = (Chained*) malloc(sizeof(Chained));
c1->next=c->next;
printf("请输入数据\n");
scanf("%d",&c1->dt.data);
c->next = c1;
}
//遍历
void print_data(Chained* c){
Chained *c1 = c;
if(NULL==c1->next){
printf("当前数据为空\n");
return ;
}
for(;c->next!=NULL;c=c->next)
printf("%d\n",c->next->dt.data);
}
主函数:
#include "./lianbiao.h"
int main(int argc, const char *argv[])
{
Chained* head = creat_head();
int b=1;
while(b==1){
printf("请选择你想要执行的操作!\n");
printf("0:退出\n1:头插\n2:头删\n3:尾插\n4:尾删\n5:按位置删除\n6:按位置插入\n7:打印\n");
int a=0;
scanf("%d",&a);
switch(a){
case 1:{
add_head(head);
break;
}
case 2:{
remove_head(head);
break;
}
case 3:{
add_tail(head);
break;
}
case 4:{
remove_tail(head);
break;
}
case 5:{
int place;
printf("请输入想要删除数据的位置\n");
scanf("%d",&place);
remove_place(head,place);
break;
}
case 6:{
int place;
printf("请输入想要增加数据的位置\n");
scanf("%d",&place);
add_place(head,place);
break;
}
case 7:{
print_data(head);
printf("当前数据有:%-3d个\n",head->dt.data);
break;
}
case 0:{
b=0;break;
}
}
printf("-----------------------------------------\n");
}
return 0;
}