1.单向循环:我对这里学习的几个函数的功能做了一个思路的整理,希望和大家共同进步,如有错误恭请指正!
下面是对这些函数的一些总结,也是本篇的精华所在,后面的双向链表完成了老师留下的三个任务
Status init(Node* p);//赋初值,需要对指针做修改,对指针的地址进行操作**p
Status insert_head(Node p,Elementype value);//插入函数首先申请地址,并返回结果,之后处理指针(前后的p->next指向)
Status insert_tail(Node p,Elementype value);//申请地址同上
Status insert_index(Node p,int index,Elementype value);//先判断插入位置的合法性,申请空间同上,把指针循环index次,处理指针
void print(Node p);//利用for循环完成链表输出
int length(Node p);//在同上函数功能相似,将输出改为自定义的变量计数即可,注意返回值
Status delete_index(Node p,int index);//判断删除位置合理性,循环到位置,处理指针
Status delete_value(Node p,Elementype value);//遍历顺序表,找到对应值,调用上面的函数(i计数),注意处理连续对应值
Status update_index(Node p,int index,Elementype value);//判断更换位置的合理性,循环到对应位置,直接赋值
Status update_value(Node p,Elementype old_name,Elementype new_name);//遍历顺序表,找到相应值,赋新值
Status query_index(Node p,int index);//判断位置的合理性,循环到指定位置打印该值
Status query_value(Node p,Elementype value);//while遍历顺序表并用i计数,判断对应值,输出i
/*****************************************************
> File name: 4xunhuan.c
> Author: Li Shang
> 日期: 2018-08-02 15:38
*****************************************************/#include <stdio.h>
#include <stdlib.h>#define F 0
#define T 1typedef int Status;
typedef int Elementype;
typedef struct node* Node;struct node
{
Elementype value;
struct node* next;
};Status init(Node* p);//赋初值,需要对指针做修改,**p
Status insert_head(Node p,Elementype value);//插入函数首先申请地址,并返回结果,之后处理指针
Status insert_tail(Node p,Elementype value);//申请地址同上
Status insert_index(Node p,int index,Elementype value);//先判断插入位置的合法性,申请空间,把指针循环到位置,处理指针
void print(Node p);
int length(Node p);
Status delete_index(Node p,int index);//判断删除位置合理性,循环到位置,处理指针
Status delete_value(Node p,Elementype value);//遍历顺序表,找到对应值,调用上面的函数(i计数),注意处理连续对应值
Status update_index(Node p,int index,Elementype value);//判断更换位置的合理性,循环到对应位置,直接赋值
Status update_value(Node p,Elementype old_name,Elementype new_name);//遍历顺序表,找到相应值,赋新值
Status query_index(Node p,int index);//判断位置的合理性,循环到指定位置打印该值
Status query_value(Node p,Elementype value);//while遍历顺序表并用i计数,判断对应值,输出iint main()
{
int i;
Node head;
init(&head);
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);update_index(head,2,6);
print(head);
insert_index(head,2,8);
print(head);delete_index(head,2);
print(head);delete_value(head,6);
print(head);
update_value(head,8,66);
print(head);query_value(head,3);
query_index(head,6);
return 0;
}Status init(Node* p)
{
Node newnode = (Node)malloc(sizeof(struct node));//动态空间
if(NULL == newnode)
{
return F;
}
newnode->next = newnode;
*p = newnode;
return T;
}Status insert_head(Node p,Elementype value)
{
Node newnode = (Node)malloc(sizeof(struct node));
if(NULL == newnode)
{
return F;
}
newnode -> value = value;
newnode -> next = p -> next;
p -> next = newnode;
return T;
}Status insert_tail(Node p,Elementype value)
{
Node head = p;
Node newnode = (Node)malloc(sizeof(struct node));
if(NULL == newnode)
{
return F;
}
while(head != p->next)
{
p = p->next;
}
newnode->value = value;
newnode->next = p->next;
p->next = newnode;
return T;
}Status insert_index(Node p,int index,Elementype value)
{
int i;
if(index < 0 || index > length(p) - 1)
{
printf("index is error\n");
return F;
}
Node newnode = (Node)malloc(sizeof(struct node));
if(NULL == newnode)
{
return F;
}
for(i = 0;i < index;i++)
{
p = p->next;
}
newnode->value = value;
newnode->next = p->next;
p->next = newnode;
return T;
}void print(Node p)
{
Node head = p;
while(head != p->next)
{
printf("%d ",p->next->value);
p = p->next;
}
printf("\n");
}int length(Node p)
{
Node head = p;
int len = 0;
while(head != p->next)
{
len++;
p = p->next;
}
return len;
}Status delete_index(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;
}
Node temp = p->next->next;
free(p->next);
p->next = temp;
return T;}
Status delete_value(Node p,Elementype value)
{
int i = 0;
Node head = p;
printf("value = %d\n",value );
while(head != p->next)
{
if (value == p->next->value)
{
delete_index(head,i);
printf("delete address is %d\n",i );
}
else
{
i++;
p = p->next;
}
}
return T;
}Status update_index(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(Node p,Elementype old_name,Elementype new_name)
{
Node head = p;
while(head != p->next)
{
if(p->next->value == old_name)//又错了一次,赋值和等于注意区别
{
p->next->value = new_name;
}
p = p->next;
}
return T;
}Status query_index(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("index %d is %d\n",i,p->next->value );
}Status query_value(Node p,Elementype value)
{
Node head = p;
int i = 0;
int flag = 0;
printf("want value is %d\n",value);
while(head != p->next)
{
if(value == p->next->value)
{
printf("%d ",i);
flag++;
}
i++;
p = p->next;
}
if(flag == 0)
{
printf("no found\n");
}
printf("\n");
return T;
}
2.双向链表,在单向链表的基础上对一些函数代码进行修改并调试成功
/*****************************************************
> File name: 5shuangxiang.c
> Author: Li Shang
> 日期: 2018-08-03 15:56
*****************************************************/#include <stdio.h>
#include <stdlib.h>#define T 1
#define F 0typedef int Elementype;
typedef int Status;struct Node
{
struct Node* prior;
Elementype value;
struct Node* next;
};typedef struct Node* node;
Status init(node* p);
Status insert_head(node p,Elementype value);
void printN(node p);
void printP(node p);
int length(node p);
Status insert_tail(node p,Elementype value);
Status insert_index(node p,int index,Elementype value);
Status delete_index(node p,int index);
Status delete_value(node p,Elementype value);int main()
{
node head;
init(&head);
int i;
for (i = 0; i < 10; i++)
{
insert_head(head,i+1);
}
printN(head);
printP(head);for (i = 0; i < 10; i++)
{
insert_tail(head,i+1);
}
printN(head);
printP(head);insert_index(head,5,99);
printN(head);delete_index(head,6);
printN(head);delete_value(head,6);
printN(head);
return 0;
}Status init(node* p)
{
node newnode = (node)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
newnode->next = newnode;
newnode->prior = newnode;
*p = newnode;
}Status insert_head(node p,Elementype value)
{
node newnode = (node)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
newnode->value = value;
newnode->next = p->next;
p->next = newnode;
newnode->prior = p;
newnode->next->prior = newnode;
}Status insert_tail(node p,Elementype value)
{
node newnode = (node)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
newnode->value = value;
/*node head = p;
while(head != p->next)
{
p = p->next;
}
newnode->next = head;
p->next = newnode;
head->prior = newnode;
newnode->prior = p;*/
newnode->next = p;
p->prior->next = newnode;
newnode->prior = p->prior;
p->prior = newnode;
}void printN(node p)
{
node head = p;
while(head != p->next)
{
printf("%d ",p->next->value);
p = p->next;
}
printf("\n");
}void printP(node p)
{
node head = p;
while(head != p->prior)
{
printf("%d ",p->prior->value);
p = p->prior;
}
printf("\n");
}int length(node p)
{
int i = 0;
node head = p;
while(head != p->next)
{
i++;
p = p->next;
}
return i;
}Status insert_index(node p,int index,Elementype value)
{
if (index < 0 || index > length(p) - 1)
{
printf("index is error\n");
return F;
}
node newnode = (node)malloc(sizeof(struct Node));
if (NULL == newnode)
{
return F;
}
int i;
for(i = 0;i < index;i++)
{
p = p->next;
}
newnode->value = value;
newnode->next = p->next;
p->next = newnode;
newnode->prior = p;
newnode->next->prior = newnode;
return T;}
Status delete_index(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;
}
node temp = p->next->next;
free(p->next);
p->next = temp;
temp->next->prior = p;
return T;
}Status delete_value(node p,Elementype value)
{
int i = 0;
node head = p;
while(head != p->next)
{
if (value == p->next->value)
{
delete_index(head,i);
}
else
{
i++;
p = p->next;
}
}
return T;
}