头文件
#ifndef __HEAD_H__
#define __HEAD_H__
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//给字符串 datatype[20] 起名字为 datatype;
typedef char datatype[20];
//定义结构体-双向链表
typedef struct Node
{
union
{
int len; //头结点记录长度
datatype data;//普通节点记录数据
};
//定义指针域
struct Node *next;//记录下一个地址
struct Node *last;//记录上一个地址
}*doublelist;
doublelist create_head();
doublelist create_node();
int insert_head(doublelist list,datatype e);
int insert_rear(doublelist list,datatype e);
void output(doublelist list);
int delete_head(doublelist list);
int delete_rear(doublelist list);
int insert_pos(doublelist list,int pos, datatype e);
int amend_pos(doublelist list,int pos ,datatype e);
int delete_pos(doublelist list,int pos);
int search_pos(doublelist list,int pos);
#endif
自定义函数
#include"head.h"
/*
* function: 定义头节点
* @param [ in] 无
* @param [out]
* @return 成功输出地址。失败输出NULL;
*/
doublelist create_head()
{
doublelist list = (doublelist)malloc(sizeof(struct Node));
if(list == NULL)
return NULL;
//若成功,初始化
list->len = 0;
list->next = NULL;
list->last = NULL;
return list;
}
/*
* function: 创建普通节点
* @param [ in] 无
* @param [out]
* @return 成功输入地址,失败输出NULL;
*/
doublelist create_node()
{
doublelist p = (doublelist)malloc(sizeof(struct Node));
if(p == NULL)
return NULL;
//若成功,初始化
strcpy(p->data ,"");
p->next = NULL;
p->last = NULL;
return p;
}
/*
* function: shu输入数据-头插
* @param [ in] 链表,元素
* @param [out]
* @return 成功返回0;失败返回-1;
*/
int insert_head(doublelist list,datatype e)
{
doublelist p = create_node();
if(p == NULL ||list == NULL)
{
printf("头插失败\n");
return -1;
}
//若成功,数值域
strcpy(p->data,e);
//指针域
p->next = list->next;
p->last = list;
list->next = p;
if(p->next)
{
p->next->last = p;
}
//长度加一
list->len++;
return 0;
}
/*
* function: w尾部插入
* @param [ in] 链表,元素
* @param [out]
* @return 成功返回0;失败返回-1
*/
int insert_rear(doublelist list,datatype e)
{
doublelist p = create_node();
if(p == NULL ||list == NULL)
{
printf("头插失败\n");
return -1;
}
//若成功,数值域
strcpy(p->data,e);
//地址域
doublelist s = list;
while(s->next)
{
s = s->next;
}
s->next = p;
p->last = s;
list->len++;
return 0;
}
/*
* function: 输出
* @param [ in] 链表
* @param [out]
* @return 无
*/
void output(doublelist list)
{
if(list == NULL||list->len == 0)
{
printf("输出失败\n");
return ;
}
//从头遍历
doublelist p = list;
printf("从头遍历\n");
while(p->next)
{
p = p->next;
printf("%s\t",p->data);
}
puts("");
//从尾部遍历
/* printf("从尾部遍历\n");
while(p->last != NULL)
{
printf("%s\t",p->data);
p = p->last;
}
puts("");*/
}
/*
* function: 头删除
* @param [ in] 链表
* @param [out]
* @return 成功返回0;失败返回-1;
*/
int delete_head(doublelist list)
{
if(list == NULL || list->len == 0)
{
printf("头删除失败\n");
return -1 ;
}
doublelist p = list->next;
list->next = p->next;
if(p->next)
{
p->next->last = list;
}
free(p);
p = NULL;
list--;
return 0;
}
/*
* function: 尾部删除
* @param [ in] 链表
* @param [out]
* @return 成功返回0;失败返回-1;
*/
int delete_rear(doublelist list)
{
if(list == NULL || list->len == 0)
{
printf("尾部删除失败\n");
return -1 ;
}
doublelist p = list;
while(p->next->next)
{
p = p->next;
}
doublelist q = p->next;
p->next = q->next;
free(q);
q = NULL;
list--;
return 0;
}
/*
* function: 按位置插入
* @param [ in] 链表,位置,元素
* @param [out]
* @return 成功返回0;失败返回-1
*/
int insert_pos(doublelist list,int pos, datatype e)
{
if(list == NULL ||list->len == 0|| pos <1 ||pos > list->len+1)
{
printf("插入失败\n");
return -1;
}
doublelist p = list;
for(int i = 0;i<pos-1;i++)
{
p = p->next;
}
doublelist s = create_node();
strcpy(s->data , e);
s->next = p->next;
s->last = p;
if(p->next)
{
p->next->last = s;
}
p->next = s;
list->len++;
return 0;
}
/*
* function: 按位置修改
* @param [ in] 链表,位置,元素
* @param [out]
* @return 成功返回0;失败返回-1
*/
int amend_pos(doublelist list,int pos ,datatype e)
{
if(list == NULL ||list->len == 0|| pos<1||pos > list->len)
{
printf("按位置修改失败\n");
return -1;
}
doublelist p = list;
for(int i = 0;i<pos ;i++)
{
p = p->next;
}
strcpy(p->data , e);
return 0;
}
/*
* function: 按位置删除
* @param [ in] 链表,位置
* @param [out]
* @return 成功返回0;失败返回-1;
*/
int delete_pos(doublelist list,int pos)
{
if(list == NULL ||list->len == 0|| pos<1||pos>list->len)
{
printf("按位置删除失败\n");
return -1;
}
doublelist p = list;
for(int i = 0;i<pos-1 ;i++)
{
p = p->next;
}
doublelist s = p->next;
p->next = s->next;
if(s->next)
{
s->next->last = p;
}
free(s);
s = NULL;
list->len--;
return 0;
}
/*
* function: 按位置查找
* @param [ in] 链表,位置
* @param [out]
* @return 成功返回0;失败返回-1
*/
int search_pos(doublelist list,int pos)
{
if(list == NULL ||list->len == 0|| pos<1||pos>list->len)
{
printf("按位置查找失败\n");
return -1;
}
doublelist p = list;
for(int i = 0;i<pos ;i++)
{
p = p->next;
}
printf("第%d个元素是%s",pos,p->data);
return 0;
}
主函数
#include"head.h"
int main(int argc, const char *argv[])
{
doublelist list = create_head();
int n;
datatype e;
printf("请输入元素个数:");
scanf("%d",&n);
for(int i = 0;i<n;i++)
{
printf("请输入元素:");
scanf(" %s",e);
insert_rear(list,e);
}
output(list);
//头删除
printf("头删除结果:\n");
delete_head(list);
output(list);
//尾部删除
printf("尾部删除结果:\n");
delete_rear(list);
output(list);
//a按位置插入
int pos;
printf("请输入插入位置:");
scanf("%d",&pos);
printf("请输入插入元素:");
scanf(" %s",e);
insert_pos(list,pos,e);
output(list);
//按位置修改
printf("请输入修改位置:");
scanf("%d",&pos);
printf("请输入修改元素:");
scanf(" %s",e);
amend_pos(list,pos,e);
output(list);
//按位置删除
printf("请输入删除位置:");
scanf("%d",&pos);
delete_pos(list,pos);
output(list);
//按位置查找
printf("请输入查找位置:");
scanf("%d",&pos);
search_pos(list,pos);
return 0;
}