
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char datatype;
// 定义结点结构体
typedef struct Node
{
// 数据域:
union
{
int len; // 头结点的数据域:链表长度
datatype data; // 其他结点的数据域:数据元素
};
// 指针域:下一个结点的地址
struct Node *next;
// 指针域:上一个结点的地址
struct Node *prev;
} *DoubleLink;
int insert_head(DoubleLink L, datatype e);
DoubleLink create_node();
DoubleLink create_head();
void output(DoubleLink L);
int insert_rear(DoubleLink L, datatype e);
int delete_head(DoubleLink L);
int delete_rear(DoubleLink L);
int delete_by_pos(DoubleLink L, int pos);
datatype search_by_pos(DoubleLink L,int pos);
int update_by_sub(DoubleLink L, int pos, datatype e);
#endif
main.c
#include "head.h"
int main(int argc, const char *argv[])
{
DoubleLink L = create_head();
int n;
datatype e;
printf("(头插入)请输入需要插入的数量:\n");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("请输入插入的值:\n");
scanf(" %c", &e);
insert_head(L, e);
}
puts("输出:");
output(L);
printf("(尾插入)请输入需要插入的数量:\n");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("请输入插入的值:\n");
scanf(" %c", &e);
insert_rear(L, e);
}
puts("输出:");
output(L);
// 头删除
// delete_head(L);
// delete_head(L);
// 尾删除
// delete_rear(L);
// delete_rear(L);
// 按位置删除
printf("请输入要删除的位置:\n");
int pos;
scanf("%d", &pos);
delete_by_pos(L, pos);
output(L);
// 按位置修改
printf("请输入要修改的位置:\n");
scanf("%d", &pos);
printf("请输入新的数据:\n");
scanf(" %c", &e);
update_by_sub(L, pos, e);
output(L);
printf("请输入要查找的位置:\n");
scanf("%d", &pos);
char out=search_by_pos(L, pos);
printf("搜索的结果是:%c\n",out);
return 0;
}
fun.c
#include "head.h"
/**
* 创建头结点
*/
DoubleLink create_head()
{
DoubleLink L = (DoubleLink)malloc(sizeof(struct Node));
if (L == NULL)
{
return NULL;
}
// L结点的数据域
L->len = 0;
// L的后继指针
L->next = NULL;
// L的前驱指针
L->prev = NULL;
return L;
}
/**
* 创建结点
*/
DoubleLink create_node()
{
DoubleLink p = (DoubleLink)malloc(sizeof(struct Node));
if (p == NULL)
{
return NULL;
}
// p结点的数据域
p->data = 0;
// p的后继指针
p->next = NULL;
// p的前驱指针
p->prev = NULL;
}
/**
* 头插
*/
int insert_head(DoubleLink L, datatype e)
{
// 判断头结点是否储存
if (L == NULL)
{
return -1;
}
// 创建新结点
DoubleLink p = create_node();
if (p == NULL)
{
return -1;
}
// p的数据域
p->data = e;
// p的指针域
p->next = L->next;
p->prev = L;
if (L->next != NULL)
{
L->next->prev = p;
}
L->next = p;
L->len++;
return 0;
}
void output(DoubleLink L)
{
DoubleLink p = L;
// 正
while (p->next)
{
p = p->next;
printf("%c\t", p->data);
}
printf("\n");
// // 反
// while (p->prev)
// {
// printf("%c\t", p->data);
// p = p->prev;
// }
printf("\n");
}
/**
* 头插
*/
int insert_rear(DoubleLink L, datatype e)
{
// 判断头结点是否储存
if (L == NULL)
{
return -1;
}
// 创建新结点
DoubleLink p = L;
if (p == NULL)
{
return -1;
}
while (p->next)
{
p = p->next;
}
// 在p后面插入新结点s
DoubleLink s = create_node();
if (s == NULL)
{
return -1;
}
// s的数据域
s->data = e;
// s的指针域
s->next = p->next;
s->prev = p;
p->next = s;
L->len++;
return 0;
}
/**
* 头删除
*/
int delete_head(DoubleLink L)
{
// 判断头结点是否存在
// 判断链表是否为空
if (L == NULL || L->len == 0)
{
printf("删除失败!\n");
return -1;
}
// 头删
DoubleLink q = L->next;
L->next = q->next;
if (q->next != NULL)
{
q->next->prev = L;
}
free(q);
q = NULL;
L->len--;
}
int delete_rear(DoubleLink L)
{
// 判断头结点是否存在,判断链表是否为空
if (L == NULL || L->len == 0)
{
printf("删除失败!\n");
return -1;
}
DoubleLink p = L;
while (p->next)
{
p = p->next;
}
p->prev->next = NULL;
free(p);
p = NULL;
return 0;
}
/**
* 按位置插入
*/
int insert_by_pos(DoubleLink L, int pos, datatype e)
{
// 判断头结点是否存在,位置是否合法
if (L == NULL || pos < 1 || pos > L->len + 1)
{
printf("插入失败!\n");
return -1;
}
// 找到pos-1的位置,起名字为p
DoubleLink p = L;
for (int i = 0; i < pos - 1; i++)
{
p = p->next;
}
// 创建新结点
DoubleLink s = create_node();
if (s == NULL)
{
return -1;
}
s->data = e;
}
/**
* 按照位置删除,删除成功返回0,失败返回-1
*/
int delete_by_pos(DoubleLink L, int pos)
{
// 判断结点,位置是否合法
if (L == NULL || pos < 1 || pos > L->len)
{
printf("参数不合法");
return -1;
}
// 循环到pos-1的位置
DoubleLink p = L;
for (int i = 0; i < pos - 1; i++)
{
p = p->next;
}
DoubleLink q = p->next;
p->next = q->next;
p->next = q->next;
if (q->next != NULL)
q->next->prev = p;
free(q);
q = NULL;
}
/**
* 按位置查找,找到返回数据,找不到返回-1
*/
datatype search_by_pos(DoubleLink L, int pos)
{
// 判断结点,位置是否合法
if (L == NULL || pos < 1 || pos > L->len)
{
printf("参数不合法");
return -1;
}
// 循环p到搜索的位置
DoubleLink p = L;
for (int i = 0; i < pos; i++)
{
p = p->next;
}
//printf("数据%c",p->data);
return p->data;
}
/**
* 按位置修改
*/
int update_by_sub(DoubleLink L, int pos, datatype e)
{
// 判断结点,位置是否合法
if (L == NULL || pos < 1 || pos > L->len)
{
printf("参数不合法");
return -1;
}
// 循环p到修改的位置
DoubleLink p = L;
for (int i = 0; i < pos; i++)
{
p = p->next;
}
p->data=e;
return 0;
}
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef float datatype;
// 定义结点结构体
typedef struct Node
{
// 数据域:
union
{
int len; // 头结点的数据域:链表长度
datatype data; // 其他结点的数据域:数据元素
};
// 指针域:下一个结点的地址
struct Node *next;
} *LoopLink;
void output(LoopLink L);
int insert_head(LoopLink L, datatype e);
LoopLink create_Node();
LoopLink create_head();
int delete_head(LoopLink L);
int delete_rear(LoopLink L);
#include "fun.c"
#endif
main.c
#include "head.h"
int main(int argc, const char *argv[])
{
LoopLink L = create_head();
// 头插
int n;
datatype e;
puts("请输入要插入的数量(头插):");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
puts("请输入要插入的值(float):");
scanf("%f", &e);
insert_head(L, e);
}
output(L);
puts("请输入要插入的数量(尾插):");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
puts("请输入要插入的值(float):");
scanf("%f", &e);
insert_rear(L, e);
}
output(L);
delete_head(L);
puts("头删后");
output(L);
delete_rear(L);
puts("尾删后");
output(L);
return 0;
}
fun.c
#include "head.h"
/**
* 创建头结点
*/
LoopLink create_head()
{
LoopLink L = (LoopLink)malloc(sizeof(struct Node));
if (L == NULL)
return NULL;
L->len = 0;
L->next = L;
return L;
}
LoopLink create_Node()
{
LoopLink L = (LoopLink)malloc(sizeof(struct Node));
if (L == NULL)
return NULL;
L->data = 0;
L->next = NULL;
return L;
}
int insert_head(LoopLink L, datatype e)
{
LoopLink s = create_Node();
if (s == NULL || L == NULL)
{
return -1;
printf("插入失败!\n");
}
s->data = e;
s->next = L->next;
L->next = s;
L->len++;
return 0;
}
int insert_rear(LoopLink L, datatype e)
{
LoopLink s = create_Node();
if (s == NULL || L == NULL)
{
return -1;
printf("插入失败!\n");
}
LoopLink p = L;
// 移动p到尾部
while (p->next != L)
{
p = p->next;
}
s->data = e;
s->next = L;
p->next = s;
}
void output(LoopLink L)
{
LoopLink p = L;
while (p->next != L)
{
p = p->next;
printf("%.2f\t", p->data);
}
puts("\n");
}
int delete_head(LoopLink L)
{
LoopLink p = L->next;
L->next = p->next;
free(p);
p = NULL;
return 0;
}
int delete_rear(LoopLink L)
{
LoopLink p = L;
while (p->next->next!=L)
{
p=p->next;
}
LoopLink s = p->next;
p->next=L;
free(s);
s = NULL;
return 0;
}
5万+

被折叠的 条评论
为什么被折叠?



