单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。
这下面是我自己打的,语句运用不当之处还请指正!
#include <stdio.h>
#include <stdlib.h>
#define T 1
#define F 1
typedef int Elementype;//类型转换
typedef int Status;
struct Node//定义一个结构体
{
Elementype value;
struct Node* next;
};
Status insert_head(struct Node* p,Elementype value);
void print(struct Node* p);
Status init (struct Node** p);
Status insert_tail(struct Node* p,Elementype value);
Status insert_index(struct Node* p,int index,Elementype value);
Status delete_index(struct Node* p,int index);
Status delete_value(struct Node* p,Elementype value);
Status update_index(struct Node* p,int index,Elementype value);
Status update_value(struct Node* p,Elementype old_value,Elementype new_value);
Status query_index(struct Node* p,int index);
Status query_value(struct Node* p,Elementype value);
int main()
{
struct Node* head;
init(&head);
int i;
for(i = 0;i < 10;i++)
{
insert_head(head,i+1);
}
print(head);
insert_tail(head,8);
print(head);
insert_index(head,3,77);
print(head);
delete_index(head,5);
print(head);
delete_value(head,8);
print(head);
update_index(head,5,8);
print(head);
update_value(head,8,5);
print(head);
query_index(head,1);
query_value(head,5);
return 0;
}
Status init(struct Node** p)//初始化头指针
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));//定义一个新变量,并申请分配内存空间
if(NULL == newnode)
{
return F;
}
newnode->next = NULL;
*p = newnode;
return T;
}
Status insert_head(struct Node* p,Elementype value)//头插
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
newnode->value = value;
newnode->next = p->next;
p->next = newnode;//自己指向自己
return T;
}
void print(struct Node* p)//打印函数
{
while(NULL != p->next)
{
printf("%d ",p->next->value);
p = p->next;
}
printf("\n");
}
Status insert_tail(struct Node* p,Elementype value)//尾插
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
newnode->value = value;
newnode->next = NULL;
while(NULL != p->next)
{
p = p->next;
}
p->next = newnode;
return T;
}
int length(struct Node* p)//元素长度
{
int len = 0;
while(NULL != p->next)
{
len++;
p = p->next;
}
return len;
}
Status insert_index(struct Node* p,int index,Elementype value)//在指定位置插入
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
int i;
if(index < 0 || index > length(p)-1)
{
printf("error");
return F;
}
newnode->value = value;
for(i = 0;i < index;i++)
{
p = p->next;
}
newnode->next = p->next;
p->next = newnode;
return T;
}
Status delete_index(struct Node* p,int index)//指定位置删除
{
int i = 0;
if(i < 0||i >= index)
{
printf("the index is error!");
return F;
}
for(i = 0;i < index;i++)
{
p = p->next;
}
p->next = p->next->next;
struct Node* temp = p->next;
p->next = temp->next;
free(temp);
return T;
}
Status delete_value(struct Node* p,Elementype value)//删除指定元素
{
int i = 0;
if(NULL == p->next)
{
return F;
}
for(i = 0;i < length(p);i++)
{
p = p->next;
if(value == p->next->value)
{
p->next = p->next->next;
}
}
struct Node* temp = p->next;//释放游离的内存空间
p->next = temp->next;
free(temp);
return T;
}
Status update_index(struct Node* p,int index,Elementype value)//定向更新
{
struct Node*newnode = (struct Node*)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
newnode->value = value;
int i = 0;
for(i = 0;i < index -1;i++)
{
p = p->next;
}
newnode->next = p->next->next;
struct Node* temp = p->next;
p->next = temp->next;
free(temp);
p->next = newnode;
return T;
}
Status update_value(struct Node* p,Elementype old_value,Elementype new_value)//更新指定元素
{
int i;
struct Node* head = p;
for(i = 0;i < length(head);i++)
{
if(old_value == p->next->value)
{
p->next->value = new_value;
}
p = p->next;
}
return T;
}
Status query_index(struct Node* p,int index)//定向读取某个元素
{
if(index < 0 || index > length(p) - 1)
{
printf("index is error\n");
return F;
}
int i;
int value;
for(i = 0;i < index;i++)
{
p = p->next;
}
value = p->next->value;
printf("the index is %d\n",value);
}
Status query_value(struct Node* p,Elementype value)//读取某个元素及其位置
{
printf("the index of query_value(%d) are : ",value);
int i = 0;
int flag = 0;
while(p->next != NULL)
{
if(p->next->value == value)
{
flag++;
printf("%d ",i);
}
i++;
p = p->next;
}
if(flag == 0)
{
printf("NOT FOUND\n");
return F;
}
printf("\n");
return T;
}
下面是老师的(结构非常规整,膜拜):
/*****************************************************
> File name: 3单链表.c
> Author: Mr.YUAN
> 日期: 2018-08-01 17:48
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#define T 1
#define F 0
typedef int Elementype;
typedef int Status;//函数的返回类型
struct Node//链表结点结构体
{
Elementype value;
struct Node* next;//自己指向自己的指针
};
Status init(struct Node** p);
Status insert_head(struct Node* p,Elementype value);
void print(struct Node* p);
Status insert_tail(struct Node* p,Elementype value);
Status insert_index(struct Node* p,int index,Elementype value);
Status delete_index(struct Node* p,int index);
Status delete_value(struct Node* p,Elementype value);
Status update_index(struct Node* p,int index,Elementype value);
Status update_value(struct Node* p,Elementype old_value,Elementype new_value);
Status query_index(struct Node* p,int index);
Status query_value(struct Node* p,Elementype value);
int main()
{
struct Node* head;
init(&head);
int i;
for(i = 0;i < 10;i++)
{
insert_head(head,i+1);
}
print(head);
for(i = 0;i < 10;i++)
{
insert_tail(head,i+1);
}
print(head);
insert_index(head,2,99);
print(head);
delete_index(head,2);
print(head);
delete_value(head,1);
print(head);
update_index(head,2,99);
print(head);
update_value(head,2,99);
print(head);
query_index(head,15);
query_value(head,99);
query_value(head,100);
return 0;
}
Status init(struct Node** p)
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
newnode->next = NULL;
*p = newnode;
return T;
}
Status insert_head(struct Node* p,Elementype value)
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
newnode->value = value;
newnode->next = p->next;
p->next = newnode;
return T;
}
void print(struct Node* p)
{
while(NULL != p->next)
{
printf("%d ",p->next->value);
p = p->next;
}
printf("\n");
}
Status insert_tail(struct Node* p,Elementype value)
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
newnode->value = value;
newnode->next = NULL;
while(NULL != p->next)
{
p = p->next;
}
p->next = newnode;
return T;
}
int length(struct Node* p)
{
int len = 0;
while(NULL != p->next)
{
len++;
p = p->next;
}
return len;
}
Status insert_index(struct Node* p,int index,Elementype value)
{
if(index < 0 || index > length(p) - 1)// 0 <= index <= length-1
{
printf("index is error\n");
return F;
}
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
newnode->value = value;
int i;
for(i = 0;i < index;i++)
{
p = p->next;
}//p指向了index的前一个结点
newnode->next = p->next;
p->next = newnode;
return T;
}
Status delete_index(struct Node* p,int index)//p必须指向链表的开头
{
if(index < 0 || index > length(p) - 1)
{
printf("index is error\n");
return F;
}
int i;
for(i = 0;i < index;i++)
{
p = p->next;
}
struct Node* temp = p->next;
p->next = temp->next;//p->next->next;
free(temp);
// struct Node* temp = p->next->next;
// free(p->next);
// p->next = temp;
return T;
}
Status delete_value(struct Node* p,Elementype value)
{
int i = 0;
struct Node* head = p;//记录链表的头结点
while(NULL != p->next)
{
if(value == p->next->value)
{
delete_index(head,i);
}
else
{
i++;
p = p->next;
}
}
return T;
}
Status update_index(struct Node* p,int index,Elementype value)
{
if(index < 0 || index > length(p) - 1)
{
printf("index is error\n");
return F;
}
int i;
for(i = 0;i < index ;i++)
{
p = p->next;
}
p->next->value = value;
return T;
}
Status update_value(struct Node* p,Elementype old_value,Elementype new_value)
{
while(p->next != NULL)
{
if(old_value == p->next->value)
{
p->next->value = new_value;
}
p = p->next;
}
}
Status query_index(struct Node* p,int index)
{
if(index < 0 || index > length(p) - 1)
{
printf("index is error\n");
return F;
}
int i;
for(i = 0;i < index;i++)
{
p = p->next;
}
printf("query_index(%d) : %d\n",index,p->next->value);
return T;
}
Status query_value(struct Node* p,Elementype value)
{
printf("the index of query_value(%d) are : ",value);
int i = 0;
int flag = 0;
while(p->next != NULL)
{
if(p->next->value == value)
{
flag++;
printf("%d ",i);
}
i++;
p = p->next;
}
if(flag == 0)
{
printf("NOT FOUND\n");
return F;
}
printf("\n");
return T;
}