/*************************************************************************
> File Name: 3.c
> Author: zz
> Mail: zzzkkk@outlook.com
> Created Time: 2017年05月20日 星期六 14时40分05秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#define T 1
#define F -1
typedef int Status;
typedef int ElementType;
struct Node
{
ElementType value;
struct Node* next;
};
Status init(struct Node** head);
Status insert_head(struct Node* head, ElementType value);
Status insert_tail(struct Node* head, ElementType value);
Status insert_index(struct Node*head, ElementType value, int index);
Status delete_index(struct Node* head, int index);
Status delete_value(struct Node* head, ElementType value);
Status update_index(struct Node*head, int index, ElementType value);
void update_value(struct Node*head, ElementType old_value, ElementType new_value);
void query_value(struct Node* head, ElementType value);
Status query_index(struct Node* head, int index);
int length(struct Node* head);
void print(struct Node* head);
int main()
{
int i;
int ret = 0;
struct Node* head = NULL;
ret = init(&head);
if (F == ret)
{
return 1;
}
for (i = 0; i < 10; i++)
{
insert_head(head, i);
}
for (i = 0; i < 10; i++)
{
insert_tail(head, i);
}
print(head);
printf("length = %d\n", length(head));
delete_index(head, length(head) - 1);
delete_index(head, 0);
delete_index(head, 5);
print(head);
insert_index(head, 99, 5);
insert_index(head, 99, 0);
insert_index(head, 99, length(head));
print(head);
delete_value(head, 0);
print(head);
update_index(head, 4, -1);
print(head);
update_value(head, 99, 100);
print(head);
query_value(head, 100);
query_value(head, 99);
query_index(head, 10);
return 0;
}
Status init(struct Node** head)
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
if (NULL == newnode)
{
return F;
}
newnode->value = 0;
newnode->next = NULL;
(*head) = newnode;
return T;
}
Status insert_head(struct Node* head, ElementType value)
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
if (NULL == newnode)
{
return F;
}
newnode->value = value;
newnode->next = head->next;
head->next = newnode;
return T;
}
Status insert_tail(struct Node* head, ElementType value)
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
if (NULL == newnode)
{
return F;
}
newnode->value = value;
newnode->next = NULL;
while (head->next != NULL)
{
head = head->next;
}
head->next = newnode;
return T;
}
Status insert_index(struct Node*head, ElementType value, int index)
{
if (index < 0 || index > length(head))
{
printf("out of range\n");
return F;
}
int i;
for (i = 0; i < index; i++)
{
head = head->next;
}
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
if (NULL == newnode)
{
return F;
}
newnode->value = value;
newnode->next = head->next;
head->next = newnode;
return T;
}
Status delete_index(struct Node* head, int index)
{
if (index < 0 || index >= length(head))
{
printf("out of range\n");
return F;
}
int i;
for (i = 0; i < index; i++)
{
head = head->next;
}
struct Node* temp = head->next->next;
free(head->next);
head->next = temp;
return T;
}
Status delete_value(struct Node* head, ElementType value)
{
int len = length(head);
int i;
for (i = 0; i < len; i++)
{
printf("i = %d value = %d\n", i, head->next->value);
if (head->next->value == value)
{
struct Node* temp = head->next->next;
free(head->next);
head->next = temp;
}
else
{
head = head->next;
}
}
return T;
}
Status update_index(struct Node*head, int index, ElementType value)
{
if (index < 0 || index >= length(head))
{
printf("out of rangr\n");
return F;
}
int i;
for (i = 0; i <= index; i++)
{
head = head->next;
}
head->value = value;
return T;
}
void update_value(struct Node*head, ElementType old_value, ElementType new_value)
{
int i;
while (head->next != NULL)
{
if (head->next->value == old_value)
{
head->next->value = new_value;
}
head = head->next;
}
}
void query_value(struct Node* head, ElementType value)
{
int count = 0;
int index = 0;
while (head->next != NULL)
{
if (head->next->value == value)
{
count++;
printf("find %d : %d\n", value, index);
}
head = head->next;
index++;
}
if (0 == count)
{
printf("not found %d\n", value);
}
}
Status query_index(struct Node* head, int index)
{
if (index < 0 || index >= length(head))
{
printf("out of rangr\n");
return F;
}
int i;
for (i = 0; i <= index; i++)
{
head = head->next;
}
printf("index = %d value =%d\n", index, head->value);
return T;
}
int length(struct Node* head)
{
int count = 0;
while (head->next != NULL)
{
count++;
head = head->next;
}
return count;
}
void print(struct Node* head)
{
while (head->next != NULL)
{
printf("%d ", head->next->value);
head = head->next;
}
printf("\n");
}
单链表 增删改查
最新推荐文章于 2023-03-07 16:38:20 发布