今日任务:
思维导图:
实现 代码:(多文件)
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int datatype;
typedef struct Linklist
{
union {
int len;
datatype data;
};
struct Linklist* next;
}Node,*NodeP;
NodeP head_create();
NodeP create();
int output(NodeP head);
int tail_insert(NodeP head,datatype data);
int head_insert(NodeP head,datatype data);
int tail_delete(NodeP head);
int head_delete(NodeP head);
int pos_insert(NodeP head,datatype data,int pos);
int pos_delete(NodeP head,int pos);
int pos_update(NodeP head,datatype data,int pos);
int value_index(NodeP head,datatype data);
int value_delete(NodeP head,datatype data);
int inversion(NodeP head);
int free_linklist(NodeP head);
#endif
fun.c
#include "head.h"
/*
* function: 传参 空指针判定
* @param [ in]
* @param [out]
* @return
*/
int void_point(NodeP p){
if(NULL==p){
puts("how dare you give me null point.");
return -1;
}
return 0;
}
/*
* function: 头结点创建
* @param [ in]
* @param [out]
* @return
*/
NodeP head_create(){
NodeP head=(NodeP)malloc(sizeof(Node));
if(head==NULL){
puts("how dare you give me null place.");
return NULL;
}
head->len=0;
head->next=NULL;
return head;
}
/*
* function: 节点创建
* @param [ in]
* @param [out]
* @return
*/
NodeP create(datatype data){
NodeP new=(NodeP)malloc(sizeof(Node));
if(new==NULL){
puts("how dare you give me null place.");
return NULL;
}
new->data=data;
new->next=NULL;
return new;
}
/*
* function: 输出
* @param [ in]
* @param [out]
* @return
*/
int output(NodeP head){
if(void_point(head))
return -1;
while(head->next!=NULL){
printf("%d\t",head->next->data);
head=head->next;
}
puts("output done.");
}
/*
* function: 节点尾插
* @param [ in]
* @param [out]
* @return
*/
int tail_insert(NodeP head,datatype data){
if(void_point(head))
return -1;
//找到尾部节点
NodeP p=head;
while(p->next!=NULL)
p=p->next;
p->next=create(data);
head->len++;
puts("tail insert success.");
return 0;
}
/*
* function: 节点头插
* @param [ in]
* @param [out]
* @return
*/
int head_insert(NodeP head,datatype data){
if(void_point(head))
return -1;
NodeP new=create(data);
new->next=head->next;
head->next=new;
head->len++;
puts("head insert success");
}
/*
* function: 尾删
* @param [ in]
* @param [out]
* @return
*/
int tail_delete(NodeP head){
if(void_point(head))
return -1;
if(head->next==NULL){
puts("there is no assigment to delete.");
return -1;
}
//找到最后一个节点,free并len--,前一节点指向null
NodeP p=head;
while(p->next->next!=NULL)
p=p->next;
free(p->next);
p->next=NULL;
head->len--;
puts("tail delete success");
return 0;
}
/*
* function: 头删
* @param [ in]
* @param [out]
* @return
*/
int head_delete(NodeP head){
if(void_point(head))
return -1;
if(head->next==NULL){
puts("there is no assigment to delete.");
return -1;
}
NodeP p=head->next;
free(head->next);
head->next=p->next;
p=NULL;
head->len--;
puts("head delete success");
return 0;
}
/*
* function: 指定位置添加
* @param [ in]
* @param [out]
* @return
*/
int pos_insert(NodeP head,datatype data,int pos){
if(void_point(head))
return -1;
if(pos>head->len+1||pos<1){
puts("your position is illegal.");
return -1;
}
NodeP p=head;
while(pos--!=1)
p=p->next;
NodeP new=create(data);
new->next=p->next;
p->next=new;
head->len++;
puts("pos insert success");
}
/*
* function: 指定位置删除
* @param [ in]
* @param [out]
* @return
*/
int pos_delete(NodeP head,int pos){
if(void_point(head))
return -1;
if(pos<1||pos>head->len){
puts("your position is illegal.");
return -1;
}
NodeP p=head;
while(pos--!=1)
p=p->next;
NodeP x=p->next;
p->next=p->next->next;
free(x);
x=NULL;
head->len--;
puts("pos delete success");
return 0;
}
/*
* function: 指定位置修改
* @param [ in]
* @param [out]
* @return
*/
int pos_update(NodeP head,datatype data,int pos){
if(void_point(head))
return -1;
if(pos<1||pos>head->len){
puts("your position is illegal.");
return -1;
}
NodeP p=head;
while(pos--)
p=p->next;
p->data=data;
puts("pos update success");
return 0;
}
/*
* function: 按值查找下表
* @param [ in]
* @param [out]
* @return
*/
int value_index(NodeP head,datatype data){
if(void_point(head))
return -1;
if(head->len==0){
puts("linklist is null");
return -1;
}
int index=0;
NodeP p=head;
while(p->next!=NULL){
p=p->next;
index++;
if(p->data==data){
printf("the index your want to find is%d\n",index);
return index;
}
}
puts("can't find your value");
return 0;
}
/*
* function: 按值删除
* @param [ in]
* @param [out]
* @return
*/
int value_delete(NodeP head,datatype data){
if(void_point(head))
return -1;
if(head->len==0){
puts("linklist is null");
return -1;
}
NodeP p=head;
while(p->next!=NULL){
if(p->next->data==data){
pos_delete(head,value_index(head,data));
puts("value delete success");
return 0;
}
p=p->next;
}
puts("no value your want to delete");
}
/*
* function: 循环逆置
* @param [ in]
* @param [out]
* @return
*/
int inversion(NodeP head){
if(void_point(head))
return -1;
if(head->len==0){
puts("linklist is NULL");
return -1;
}
if(head->len==1){
puts("there is no deed");
return 0;
}
//逆置
//将第二个节点作为尾节点,但是得先记录一下,还得用于每次循环的头插的第一个元素
NodeP p=head->next->next;
head->next->next=NULL;
//定义一个节点,用于方便循环调用后面的元素头插,
NodeP k=p;
puts("debug..");
output(p);
while(p!=NULL){
p=p->next;
k->next=head->next;
head->next=k;
k=p;
}
puts("inversion success.");
return 0;
}
/*
* function: 释放链表
* @param [ in]
* @param [out]
* @return
*/
int free_linklist(NodeP head){
if(void_point(head))
return -1;
NodeP p=NULL;
while(head!=NULL){
p=head;
head=head->next;
free(p);
p=NULL;
}
puts("free success.");
return 0;
}
main.c文章来源:https://www.toymoban.com/news/detail-682377.html
#include "head.h"
int main(int argc, const char *argv[])
{
NodeP p=head_create();
tail_insert(p,10);
tail_insert(p,20);
tail_insert(p,30);
tail_insert(p,40);
tail_insert(p,50);
head_insert(p,99);
head_insert(p,88);
output(p);
//pos_insert(p,66,0);
//output(p);
//pos_delete(p,8);
//output(p);
//pos_update(p,66,8);
//output(p);
//tail_delete(p);
//output(p);
//head_delete(p);
//output(p);
//value_delete(p,77);
//output(p);
//inversion(p);
//output(p);
free_linklist(p);
p=NULL;
return 0;
}
不好,眼花了,没看到实现单项循环链表,ji文章来源地址https://www.toymoban.com/news/detail-682377.html
到了这里,关于数据结构day05(单链表)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!