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

 
#include <stdio.h>
#include <stdlib.h>
#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 = 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 = 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 = 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 = 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 == 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 = 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 == old_value)
        {
            count = 1;
            head->next->value = 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 == 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");
}

程序运行结果如下:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值