关于单链表的增、减、改、查函数

#include
#include#define T 1
#define F -1 typedef int Type; struct Node
{
Type value;
struct Node *next;
}; int init(struct Node **head); //初始化
int insert_head(struct Node *head, Type value); // 头插法
int insert_tail(struct Node *head, Type value); // 尾插法
int insert(struct Node *head, Type index, Type x); // 在中间插入
int delete(struct Node *head, Type index); // 按位删除
int delete_value(struct Node *head, Type x); // 按值删除
int change_index(struct Node *head, Type index, Type x); //按位改变
int change_value(struct Node *head, Type old_value, Type new_value); //按值改变
void search_index(struct Node *head, Type index); // 按位查值
void search_value(struct Node *head, Type value); //按值查位
int length(struct Node *head); // 输出长度
int print(struct Node *head); // 输出函数
int main()
{
int i;
int ret;
struct Node *head;
ret = init(&head);

for (i = 0; i <= 5; i++)
{
insert_head(head, i);
}
print(head); for (i = 0; i <= 5; i++)
{
insert_tail(head, i);
}
print(head); printf("%d\n", length(head));
delete(head,2);
delete(head,length(head) - 1);
delete(head,0);
print(head); insert(head, 3,99);
insert(head, 0,99);
insert(head, length(head),99);
print(head);

delete_value(head, 0);
print(head); change_index(head, 4, 101);
print(head); change_value(head, 99, 100);
print(head);

search_index(head, 7);

search_value(head, 100); return 0;
} int init(struct Node **head)
{
struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
if (NULL == newnode)
{
return F;
}
newnode->value = https://www.it610.com/article/0;
newnode->next = NULL;
(*head) = newnode;
return T;
} int insert_head(struct Node *head, Type value)
{
struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
if (NULL == newnode)
{
return F;
}

newnode->value = https://www.it610.com/article/value;
newnode->next = head->next;
head->next = newnode;

return T;
} int insert(struct Node *head, Type index, Type x)
{
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 = https://www.it610.com/article/x;
newnode->next = head->next;
head->next = newnode;

return T;
}int insert_tail(struct Node *head, Type value)
{
struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
if (NULL == newnode)
{
return F;
}

newnode->value = https://www.it610.com/article/value;
newnode->next = NULL;

while (NULL != head->next)
{
head = head->next;
}

head->next = newnode; return T;
} int delete(struct Node *head, Type index)
{
int i;
struct Node *temp;
if (index < 0 || index >= length(head))
{
printf("out of range\n");
return F;
}
for (i = 0; i < index; i++)
{
head = head->next;
}
temp = head->next->next;
free(head->next);
head->next = temp;

return T;
} int delete_value(struct Node *head, Type x)
{
int i;
struct Node *temp;
while (NULL != head->next)
{
if(head->next->value =https://www.it610.com/article/= x)
{
temp = head->next->next;
free(head->next);
head->next = temp;
}
else
{
head = head->next;
}
}
} int change_index(struct Node *head, Type index, Type x)
{
int i;
if (index < 0 || index >= length(head))
{
printf("out of range\n");
return F;
}
for (i = 0; i <= index; i++)
{
head = head->next;
}
head->value = https://www.it610.com/article/x;

return T;
} int change_value(struct Node *head, Type old_value, Type new_value)
{
int count = 0;
while (head->next != NULL)
{
if(head->next->value =https://www.it610.com/article/= old_value)
{
count = 1;
head->next->value = https://www.it610.com/article/new_value;
}
head = head->next;
}
if (count == 0)
{
printf("not find\n");
}

return T;
}

void search_index(struct Node *head, Type index)
{
int i;
if (index < 0 || index >= length(head))
{
printf("out of range\n");
}
for (i = 0; i <= index; i++)
{
head = head->next;
}
printf("%d\n",head->value);

}

void search_value(struct Node *head, Type value)
{
int count = 0;
while (head->next != NULL)
{
if (head->next->value =https://www.it610.com/article/= value)
{
printf("%d",count);
}
head = head->next;
count++;
}
printf("\n");
}

int length(struct Node *head)
{
int count = 0;
while (NULL != head->next)
{
count++;
head = head->next;
}

return count;
}
int print(struct Node *head)
{
while (head->next != NULL)
{
printf("%d", head->next->value);
head = head->next;
}
printf("\n");
}
程序运行结果如下:
关于单链表的增、减、改、查函数
文章图片

    推荐阅读