1_main.c
#include<stdio.h>
#include"./3_list.h"
int main()
{
list* head = create_list();
insertFront_list(head,11);
insertFront_list(head,22);
insertFront_list(head,33);
show_list(head);
insertBack_list(head,99);
insertBack_list(head,88);
insertBack_list(head,77);
insertBack_list(head,66);
show_list(head);
printf("%d\n",deleteFront_list(head));
show_list(head);
printf("%d\n",deleteBack_list(head));
show_list(head);
insertPos_list(head,3,55555555);
show_list(head);
printf("%d\n",deletePos_list(head,4));
show_list(head);
return 0;
}
2_list.c
#include<stdio.h>
#include<stdlib.h>
#include "./3_list.h"
list* create_list(void)
{
list* head = (list*)malloc(sizeof(list));
if(NULL == head)
{
printf("头结点创建失败\n");
return NULL;
}
head->db.len = 0;
head->next = NULL;
return head;
}
void insertFront_list(list* head,datatype num)
{
list* temp = (list*)malloc(sizeof(list));
if(NULL == temp)
{
printf("结点创建失败\n");
return;
}
temp->db.data = num;
temp->next = NULL;
temp->next = head->next;
head->next = temp;
head->db.len++;
return;
}
void insertBack_list(list* head,datatype num)
{
list* temp = (list*)malloc(sizeof(list));
if(NULL == temp)
{
printf("结点创建失败\n");
return;
}
temp->db.data = num;
temp->next = NULL;
list* p = head;
while(p->next != NULL)
{
p=p->next;
}
temp->next = p->next;
p->next = temp;
head->db.len++;
return;
}
void insertPos_list(list* head,int pos,datatype num)
{
list* temp = (list*)malloc(sizeof(list));
if(NULL == temp)
{
printf("结点创建失败\n");
return;
}
temp->db.data = num;
temp->next = NULL;
if(pos<=0 || pos>head->db.len+1)
{
printf("插入位置非法\n");
return;
}
list* p = head;
for(int i=0;i<pos-1;i++)
{
p=p->next;
}
temp->next = p->next;
p->next = temp;
head->db.len++;
return;
}
datatype deleteFront_list(list* head)
{
if(NULL == head->next || NULL == head)
{
printf("链表为空或不存在,无法头删\n");
return (datatype)-1;
}
list* temp = head->next;
datatype num = temp->db.data;
head->next=temp->next;
free(temp);
temp = NULL;
head->db.len--;
return num;
}
datatype deleteBack_list(list* head)
{
//判断空
//找倒数第二个结点
//按位置删除
//释放删除结点空间
if(NULL == head->next || NULL == head)
{
printf("链表为空或不存在,无法尾删\n");
return (datatype)-1;
}
list* p = head;
while(p->next->next !=NULL)
{
p=p->next;
}
list* temp = p->next;
datatype num = temp->db.data;
p->next = NULL;
free(temp);
temp = NULL;
head->db.len--;
return num;
}
datatype deletePos_list(list* head,int pos)
{
if(NULL == head->next || NULL == head)
{
printf("链表为空或不存在,无法头删\n");
return (datatype)-1;
}
if(pos<=0 || pos>head->db.len+1)
{
printf("删除位置非法\n");
return (datatype)-2;
}
list* p = head;
for(int i=0;i<pos-1;i++)
{
p = p->next;
}
list* temp = p->next;
datatype num = temp->db.data;
p->next = temp->next;
free(temp);
temp = NULL;
head->db.len--;
return num;
}
void show_list(list* head)
{
list* p = head;
while(p->next != NULL)
{
p=p->next;
printf("%d\t",p->db.data);
}
printf("\n");
return;
}
3_list.h
#ifndef __LIST_H__
#define __LIST_H__
typedef int datatype;
typedef struct ls
{
union
{
datatype data;
int len;
}db;
struct ls* next;
}list;
list* create_list(void);
void insertFront_list(list* head,datatype num);
void insertBack_list(list* head,datatype num);
void show_list(list* head);
datatype deleteBack_list(list* head);
datatype deleteFront_list(list* head);
void insertPos_list(list* head,int pos,datatype num);
datatype deletePos_list(list* head,int pos);
#endif
运行代码: