目录
作业1:完成尾删函数:int list_delete_tail(Linklist *L);
功能函数:
主函数:
运行结果:
作业2:完成按位置修改函数 : int list_update_pos(Linklist *L, int pos, datatype e);
功能函数:
主函数:
运行结果:
作业3:完成按值修改函数: int list__update_value(Linklist *L, datatype old_e, datatype new_e);
功能函数:
主函数:
运行结果:
作业4:完成链表反转函数:void list_reverse(Linklist *L);(头插法)
功能函数:
主函数:
运行结果:
全部代码↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
linklist.c
linklist.h
main.c
作业1:完成尾删函数:int list_delete_tail(Linklist *L);
功能函数:
int list_delete_tail(Linklist *L)
{
if (NULL==L|| list_empty(L))
{
printf("删除失败n");
return -1;
}
Linklist *q=find_node(L,L->len-1);
Linklist *p=q->next;
q->next=NULL;
free(p);
p=NULL;
L->len--;
printf("尾删成功n");
}
主函数:
//尾删
list_delete_tail(L);
list_show(L);
运行结果:
int list_delete_tail(Linklist *L)
{
if (NULL==L|| list_empty(L))
{
printf("删除失败n");
return -1;
}
Linklist *q=find_node(L,L->len-1);
Linklist *p=q->next;
q->next=NULL;
free(p);
p=NULL;
L->len--;
printf("尾删成功n");
}
主函数:
//尾删
list_delete_tail(L);
list_show(L);
运行结果:
作业2:完成按位置修改函数 : int list_update_pos(Linklist *L, int pos, datatype e);
功能函数:
//按位置修改函数
int list_update_pos(Linklist *L ,int pos,datatype new_e)
{
//判断逻辑
if (NULL==L|| list_empty(L)||pos<1||pos>L->len)
{
printf("修改失败n");
return -1;
}
Linklist *q=find_node(L,pos);
q->data=new_e;
}
主函数:
//按位置修改函数值
list_update_pos(L,1,'Z');
list_show(L);
运行结果:
作业3:完成按值修改函数: int list__update_value(Linklist *L, datatype old_e, datatype new_e);
功能函数:
//安值修改函数
int list_update_value(Linklist *L,datatype old_e,datatype new_e)
{
Linklist *p=L;
while(p->next!=NULL)
{
p=p->next;
if(p->data==old_e)
{
p->data=new_e;
printf("修改成功n");
return 1;
}
}printf("未找到n");
return -1;
}
主函数:
//按值修改函数
list_update_value(L,'Z','Y');
list_show(L);
list_update_value(L,'D','Y');
list_show(L);
运行结果:
作业4:完成链表反转函数:void list_reverse(Linklist *L);(头插法)
功能函数:
//链表反转函数
void list_reverse(Linklist *L)
{
Linklist *q=L->next;
L->next=NULL;
while(q!= NULL)
{
list_insert_head(L,q->data);
q=q->next;
}
//list_show(p);
}
主函数:
//反转
list_reverse(L);
list_show(L);
运行结果:
全部代码↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
linklist.c
#include
#include
#include"linklist.h"
//创建
Linklist *list_create()
{
Linklist *L=(Linklist*)malloc(sizeof(Linklist));
if(NULL==L)
{
printf("创建失败n");
return NULL;
}//初始化
L->len =0;
L->next=NULL;
printf("创建成功n");
return L;
}
//判空
int list_empty(Linklist *L)
{
return NULL==L->next ?1:0;//1表示空 0表示非空
}
//头插
int list_insert_head(Linklist *L,datatype e)
{
//判断条件
if (NULL==L)
{
printf("所给链表不合法n");
return -1;
}
//申请节点
Linklist *p=(Linklist *)malloc(sizeof(Linklist));
if(NULL==p)
{
printf("节点申请失败n");
return -1;
}
//插入数据
p->data =e;
p->next=NULL;
//完成头插
p->next = L->next;
L->next = p;
//表的变化
L->len++;
printf("头插成功n");
}
//遍历
void list_show(Linklist *L)
{
//判断逻辑
if (NULL==L || list_empty(L))
{
printf("表空 遍历失败n");
}
//遍历逻辑
printf("链表元素分别是:");
Linklist *q= L->next;
while (q!=NULL)
{
printf("%ct",q->data);
q=q->next;
}
printf("n");
}
//申请节点函数
Linklist *node_buy(datatype e)
{
Linklist *p=(Linklist*)malloc(sizeof(Linklist));
if (NULL==p)
{
printf("节点申请失败n");
}
//将数据存到节点中
p->data=e;
p->next=NULL;
return p;
}
//尾插
int list_insert_tail(Linklist *L,datatype e)
{
//判断逻辑
if (NULL==L)
{
printf("所给链表不合法n");
return -1;
}
//申请节点
Linklist *p=node_buy(e);
//遍历指针指向最后一个节点
Linklist *q=L;
while (q->next != 0)
{
q=q->next;
}
//完成尾插逻辑
q->next=p;
//表的变化
L->len++;
}
//按任意位置查找返回查找的节点
Linklist *find_node(Linklist *L,int pos)
{//判断逻辑
if (NULL==L || pos<0 ||pos>L->len)
{
printf("查找失败n");
return NULL;
}
//查找节点
Linklist *q=L;
for (int i=1; i<=pos; i++)
{
q=q->next;
}
return q;//将找到的节点返回
}
//任意位置插
int list_insert_pos(Linklist *L,int pos,datatype e)
{
//判断逻辑
if (NULL==L||pos<1||pos>L->len+1)
{
printf("插入失败n");
return -1;
}
Linklist *p=node_buy(e);//申请节点存放数据
Linklist *q=find_node(L,pos-1);//查找要插入位置的前驱节点
p->next=q->next;//插入逻辑
q->next=p;
//表的变化
L->len++;
printf("插入成功n");
return 0;
}
//头删
int list_delete_head(Linklist *L)
{
//判断逻辑
if (NULL==L||list_empty(L))
{
printf("删除失败n");
return -1;
}
//头删
Linklist *p=L->next;
L->next=p->next;
free(p);
p=NULL;
//表的变化
L->len--;
printf("头删除成功n");
return 0;
}
//任意删
int list_delete_pos(Linklist *L,int pos)
{
//判断逻辑
if (NULL==L|| list_empty(L)||pos<1||pos>L->len)
{
printf("删除失败n");
return -1;
}
//找到要删除位置的前驱
Linklist *q=find_node(L,pos-1);
//删除逻辑
Linklist *p=q->next;
q->next=p->next;
//释放空间
free(p);
p=NULL;
//表的变化
L->len--;
}
//按值查找返回第一个查找成功的位置
int list_search_value(Linklist *L,datatype e)
{
if (NULL==L || list_empty(L))
{
//printf("查找失败n");
return -1;
}
//查找逻辑
Linklist *q=L->next;
for (int i=1; i<=L->len; i++)
{
if (q->data==e)
{
return i;
}
q=q->next;
}
return 0;
}
//销毁链表
void list_free(Linklist *L)
{
if (NULL==L)
{
return;
}
//不断调用头删,将节点进行删除
while(L->next != NULL)
{
list_delete_head(L);
}
//将头结点释放
free(L);
L=NULL;
printf("释放表成功n");
return;
}
//尾删
int list_delete_tail(Linklist *L)
{
if (NULL==L|| list_empty(L))
{
printf("删除失败n");
return -1;
}
Linklist *q=find_node(L,L->len-1);
Linklist *p=q->next;
q->next=NULL;
free(p);
p=NULL;
L->len--;
printf("尾删成功n");
}
//按位置修改函数
int list_update_pos(Linklist *L ,int pos,datatype new_e)
{
//判断逻辑
if (NULL==L|| list_empty(L)||pos<1||pos>L->len)
{
printf("修改失败n");
return -1;
}
Linklist *q=find_node(L,pos);
q->data=new_e;
}
//安值修改函数
int list_update_value(Linklist *L,datatype old_e,datatype new_e)
{
Linklist *p=L;
while(p->next!=NULL)
{
p=p->next;
if(p->data==old_e)
{
p->data=new_e;
printf("修改成功n");
return 1;
}
}printf("未找到n");
return -1;
}
//链表反转函数
void list_reverse(Linklist *L)
{
Linklist *q=L->next;
L->next=NULL;
while(q!= NULL)
{
list_insert_head(L,q->data);
q=q->next;
}
//list_show(p);
}
linklist.h
#ifndef __LINKLIST_H__
#define __LINKLIST_H__
typedef char datatype;
typedef struct Node
{
union{
datatype data;//数据域
int len;
};
struct Node *next;//指针域
}Linklist;
//创建
Linklist *list_create();
//申请节点函数
Linklist *node_buy(datatype e);
//判空
int list_empty(Linklist *L);
//头插
int list_insert_head(Linklist *L,datatype e);
//遍历
void list_show(Linklist *L);
//尾插
int list_insert_tail(Linklist *L,datatype e);
//任意插
int list_insert_pos(Linklist *L,int pos,datatype e);
//按位置查找
Linklist *find_node(Linklist *L,int pos);
//头删
int list_delete_head(Linklist *L);
//尾删
int list_delete_tail(Linklist *L);
//任意删
int list_delete_pos(Linklist *L,int pos);
//按值查找返回第一个查找成功的位置
int list_search_value(Linklist *L,datatype e);
//按位置查找修改
int list_update_pos(Linklist *L ,int pos,datatype new_e);
//安值修改
int list_update_value(Linklist *L,datatype old_e,datatype new_e);
//销毁链表
void list_free(Linklist *L);
//链表反转
void list_reverse(Linklist *L);
#endif
main.c
#include
#include
#include"linklist.h"
int main(int argc, const char *argv[])
{
Linklist *L=list_create();
if (NULL==L)
{
return -1;
}
//调用头插函数
list_insert_head(L,'Q');
list_insert_head(L,'W');
list_insert_head(L,'E');
list_insert_head(L,'R');
printf("L->len=%dn",L->len);
//调用遍历函数
list_show(L);
//调用尾插函数
list_insert_tail(L,'A');
list_insert_tail(L,'B');
list_show(L);
//调用任意位置插入
list_insert_pos(L,1,'X');
list_show(L);
//调用头删函数
list_delete_head(L);
list_show(L);
//调用任意删函数
list_delete_pos(L,1);
list_show(L);
//按值查找返回第一个查找成功的位置
int res=list_search_value(L,'W');
if (res == -1)
{
printf("查找失败n");
}else if (res == 0)
{
printf("没找到n");
}else if (res>0)
{
printf("查找成功,在第%d个位置n",res);
}
list_show(L);
//尾删
list_delete_tail(L);
list_show(L);
//按位置修改函数
list_update_pos(L,1,'Z');
list_show(L);
//按值修改函数
list_update_value(L,'Z','Y');
list_show(L);
list_update_value(L,'D','Y');
list_show(L);
//反转
list_reverse(L);
list_show(L);
return 0;
}
#include#include #include"linklist.h" //创建 Linklist *list_create() { Linklist *L=(Linklist*)malloc(sizeof(Linklist)); if(NULL==L) { printf("创建失败n"); return NULL; }//初始化 L->len =0; L->next=NULL; printf("创建成功n"); return L; } //判空 int list_empty(Linklist *L) { return NULL==L->next ?1:0;//1表示空 0表示非空 } //头插 int list_insert_head(Linklist *L,datatype e) { //判断条件 if (NULL==L) { printf("所给链表不合法n"); return -1; } //申请节点 Linklist *p=(Linklist *)malloc(sizeof(Linklist)); if(NULL==p) { printf("节点申请失败n"); return -1; } //插入数据 p->data =e; p->next=NULL; //完成头插 p->next = L->next; L->next = p; //表的变化 L->len++; printf("头插成功n"); } //遍历 void list_show(Linklist *L) { //判断逻辑 if (NULL==L || list_empty(L)) { printf("表空 遍历失败n"); } //遍历逻辑 printf("链表元素分别是:"); Linklist *q= L->next; while (q!=NULL) { printf("%ct",q->data); q=q->next; } printf("n"); } //申请节点函数 Linklist *node_buy(datatype e) { Linklist *p=(Linklist*)malloc(sizeof(Linklist)); if (NULL==p) { printf("节点申请失败n"); } //将数据存到节点中 p->data=e; p->next=NULL; return p; } //尾插 int list_insert_tail(Linklist *L,datatype e) { //判断逻辑 if (NULL==L) { printf("所给链表不合法n"); return -1; } //申请节点 Linklist *p=node_buy(e); //遍历指针指向最后一个节点 Linklist *q=L; while (q->next != 0) { q=q->next; } //完成尾插逻辑 q->next=p; //表的变化 L->len++; } //按任意位置查找返回查找的节点 Linklist *find_node(Linklist *L,int pos) {//判断逻辑 if (NULL==L || pos<0 ||pos>L->len) { printf("查找失败n"); return NULL; } //查找节点 Linklist *q=L; for (int i=1; i<=pos; i++) { q=q->next; } return q;//将找到的节点返回 } //任意位置插 int list_insert_pos(Linklist *L,int pos,datatype e) { //判断逻辑 if (NULL==L||pos<1||pos>L->len+1) { printf("插入失败n"); return -1; } Linklist *p=node_buy(e);//申请节点存放数据 Linklist *q=find_node(L,pos-1);//查找要插入位置的前驱节点 p->next=q->next;//插入逻辑 q->next=p; //表的变化 L->len++; printf("插入成功n"); return 0; } //头删 int list_delete_head(Linklist *L) { //判断逻辑 if (NULL==L||list_empty(L)) { printf("删除失败n"); return -1; } //头删 Linklist *p=L->next; L->next=p->next; free(p); p=NULL; //表的变化 L->len--; printf("头删除成功n"); return 0; } //任意删 int list_delete_pos(Linklist *L,int pos) { //判断逻辑 if (NULL==L|| list_empty(L)||pos<1||pos>L->len) { printf("删除失败n"); return -1; } //找到要删除位置的前驱 Linklist *q=find_node(L,pos-1); //删除逻辑 Linklist *p=q->next; q->next=p->next; //释放空间 free(p); p=NULL; //表的变化 L->len--; } //按值查找返回第一个查找成功的位置 int list_search_value(Linklist *L,datatype e) { if (NULL==L || list_empty(L)) { //printf("查找失败n"); return -1; } //查找逻辑 Linklist *q=L->next; for (int i=1; i<=L->len; i++) { if (q->data==e) { return i; } q=q->next; } return 0; } //销毁链表 void list_free(Linklist *L) { if (NULL==L) { return; } //不断调用头删,将节点进行删除 while(L->next != NULL) { list_delete_head(L); } //将头结点释放 free(L); L=NULL; printf("释放表成功n"); return; } //尾删 int list_delete_tail(Linklist *L) { if (NULL==L|| list_empty(L)) { printf("删除失败n"); return -1; } Linklist *q=find_node(L,L->len-1); Linklist *p=q->next; q->next=NULL; free(p); p=NULL; L->len--; printf("尾删成功n"); } //按位置修改函数 int list_update_pos(Linklist *L ,int pos,datatype new_e) { //判断逻辑 if (NULL==L|| list_empty(L)||pos<1||pos>L->len) { printf("修改失败n"); return -1; } Linklist *q=find_node(L,pos); q->data=new_e; } //安值修改函数 int list_update_value(Linklist *L,datatype old_e,datatype new_e) { Linklist *p=L; while(p->next!=NULL) { p=p->next; if(p->data==old_e) { p->data=new_e; printf("修改成功n"); return 1; } }printf("未找到n"); return -1; } //链表反转函数 void list_reverse(Linklist *L) { Linklist *q=L->next; L->next=NULL; while(q!= NULL) { list_insert_head(L,q->data); q=q->next; } //list_show(p); }
linklist.h
#ifndef __LINKLIST_H__
#define __LINKLIST_H__
typedef char datatype;
typedef struct Node
{
union{
datatype data;//数据域
int len;
};
struct Node *next;//指针域
}Linklist;
//创建
Linklist *list_create();
//申请节点函数
Linklist *node_buy(datatype e);
//判空
int list_empty(Linklist *L);
//头插
int list_insert_head(Linklist *L,datatype e);
//遍历
void list_show(Linklist *L);
//尾插
int list_insert_tail(Linklist *L,datatype e);
//任意插
int list_insert_pos(Linklist *L,int pos,datatype e);
//按位置查找
Linklist *find_node(Linklist *L,int pos);
//头删
int list_delete_head(Linklist *L);
//尾删
int list_delete_tail(Linklist *L);
//任意删
int list_delete_pos(Linklist *L,int pos);
//按值查找返回第一个查找成功的位置
int list_search_value(Linklist *L,datatype e);
//按位置查找修改
int list_update_pos(Linklist *L ,int pos,datatype new_e);
//安值修改
int list_update_value(Linklist *L,datatype old_e,datatype new_e);
//销毁链表
void list_free(Linklist *L);
//链表反转
void list_reverse(Linklist *L);
#endif
main.c
#include
#include
#include"linklist.h"
int main(int argc, const char *argv[])
{
Linklist *L=list_create();
if (NULL==L)
{
return -1;
}
//调用头插函数
list_insert_head(L,'Q');
list_insert_head(L,'W');
list_insert_head(L,'E');
list_insert_head(L,'R');
printf("L->len=%dn",L->len);
//调用遍历函数
list_show(L);
//调用尾插函数
list_insert_tail(L,'A');
list_insert_tail(L,'B');
list_show(L);
//调用任意位置插入
list_insert_pos(L,1,'X');
list_show(L);
//调用头删函数
list_delete_head(L);
list_show(L);
//调用任意删函数
list_delete_pos(L,1);
list_show(L);
//按值查找返回第一个查找成功的位置
int res=list_search_value(L,'W');
if (res == -1)
{
printf("查找失败n");
}else if (res == 0)
{
printf("没找到n");
}else if (res>0)
{
printf("查找成功,在第%d个位置n",res);
}
list_show(L);
//尾删
list_delete_tail(L);
list_show(L);
//按位置修改函数
list_update_pos(L,1,'Z');
list_show(L);
//按值修改函数
list_update_value(L,'Z','Y');
list_show(L);
list_update_value(L,'D','Y');
list_show(L);
//反转
list_reverse(L);
list_show(L);
return 0;
}
#include#include #include"linklist.h" int main(int argc, const char *argv[]) { Linklist *L=list_create(); if (NULL==L) { return -1; } //调用头插函数 list_insert_head(L,'Q'); list_insert_head(L,'W'); list_insert_head(L,'E'); list_insert_head(L,'R'); printf("L->len=%dn",L->len); //调用遍历函数 list_show(L); //调用尾插函数 list_insert_tail(L,'A'); list_insert_tail(L,'B'); list_show(L); //调用任意位置插入 list_insert_pos(L,1,'X'); list_show(L); //调用头删函数 list_delete_head(L); list_show(L); //调用任意删函数 list_delete_pos(L,1); list_show(L); //按值查找返回第一个查找成功的位置 int res=list_search_value(L,'W'); if (res == -1) { printf("查找失败n"); }else if (res == 0) { printf("没找到n"); }else if (res>0) { printf("查找成功,在第%d个位置n",res); } list_show(L); //尾删 list_delete_tail(L); list_show(L); //按位置修改函数 list_update_pos(L,1,'Z'); list_show(L); //按值修改函数 list_update_value(L,'Z','Y'); list_show(L); list_update_value(L,'D','Y'); list_show(L); //反转 list_reverse(L); list_show(L); return 0; }



