单链表增删改查排序

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct Node
{
    int age;
    struct Node * next;
};

int len(struct Node *head)
{
    int i = 0;
    while(head->next != NULL)
    {
        head = head->next;
        i++;
    }
    return i;
}

int init(struct Node **head)
{
    struct Node *newnode = (struct Node*)malloc(sizeof(struct Node));
    if(newnode == NULL)
    {
        return 0;
    }
    newnode->age = 0;
    newnode->next = NULL;
    *head = newnode;
}

int insert_tail(struct Node *head,int x)
{
    struct Node *newnode = (struct Node*)malloc(sizeof(struct Node));
    if(newnode == NULL)
    {
        printf("insert tail!\n");
        return 0;
    }
    newnode->age = x;
    while(head->next != NULL)
    {
        head = head->next;
    }
    head->next = newnode;
}

int print(struct Node*head)
{
    while(head->next!=NULL)
    {
        printf("%d ",head->next->age);
        head = head->next;
    }
    printf("\n");
}

int insert_head(struct Node *head,int x)
{
    struct Node *newnode  =  (struct Node*)malloc(sizeof(struct Node));
    if(newnode == NULL)
    {
        printf("insert_head\n");
        return 0;
    }
    newnode->age = x;

    newnode->next = head->next;
    head->next = newnode;
}

int insert_index(struct Node *head,int index,int x)
{
    if(index < 1 || index > len(head) )
    {
        printf("insert_index:index\n");
        return 0;
    }
    struct Node * newnode  = (struct Node*)malloc(sizeof(struct Node));
    if(newnode == NULL)
    {
        printf("insert_index");
        return 0;
    }
    newnode->age = x;
    //head走到要插的前一个
    for(int i = 0; i < index-1; i++)
    {
        head = head->next;
    }
    //把newnode插到里面
    newnode->next = head->next;
    head->next = newnode;
}

int update_index(struct Node*head,int index, int x)
{
    int i;
    if(index < 1 || index > len(head) )
    {
        printf("update_index:index\n");
        return 0;
    }
    for(i = 0; i<index; i++)
    {
        head = head->next;
    }
    head->age = x;
}

int update_value(struct Node*head,int beform,int after)
{
    head = head->next;
    while(head->next != NULL)
    {
        if(head->age == beform)
        {
            head->age = after;
        }
        head = head->next;
    }
    if(head->age == beform)
    {
        head->age = after;
    }
}

int delete_index(struct Node*head,int index)
{
    if(index < 1 || index > len(head) )
    {
        printf("update_index:index\n");
        return 0;
    }
    int i;
	for(i = 0; i < index-1; i++)
	{
		head = head->next;
	}
    struct Node *ptr = head->next;
    head->next = ptr->next;
    free(ptr);
}

int delete_value(struct Node*head,int value)
{
    int length = len(head);
    int i;
    for(i = 0; i < length; i++)
    {
        if(head->next->age == value)
        {
            struct Node *ptr = head->next;
            head->next = ptr->next;
            free(ptr);
        }
        else
        {
            head = head->next;
        }
    }
}

int search_index(struct Node*head,int index)
{
    int i;
    if(index < 1 || index > len(head) )
    {
        printf("update_index:index\n");
        return 0;
    }
    for(i = 0; i<index; i++)
    {
        head = head->next;
    }
    printf("index:%d age:%d\n",index, head->age );
}

int search_value(struct Node*head,int value)
{
    int length = len(head);
    int i;
    int count = 0;
    head = head->next;
    for(i=1; i<length; i++)
    {
        if(head->age == value)
        {
            printf("value:%d index:%d\n",value,i);
            count++;
        }
        head = head->next;
    }
    /*判断最后一个值*/
    if(head->age == value)
    {
        printf("value:%d index:%d\n",value,i);
        count++;
    }
    if(count == 0)
    {
        printf("未找到!\n");
    }
    else
    {
        printf("一共找到%d个数\n", count);
    }
}

void sort(struct Node*head)
{
    int length = len(head);
    struct Node*p = head;
    int i,j;
    for(i = 0; i< length -1;i++)
    {
        head = p;
        for(j = 0; j<length - i - 1; j++)
        {
            if(head->next->age > head->next->next->age)
            {
                struct  Node*ptr1 = head->next;
                struct  Node*ptr2 = head->next->next;
                ptr1->next = ptr2->next;
                ptr2->next = ptr1;
                head->next = ptr2;
            }
                head = head->next;
        }
    }
}

void nixu1(struct Node*head)
{

}

void nixu2(struct Node*head)
{

}

struct Node* my_free(struct Node*head)
{
    while(head->next != NULL)
    {
        struct Node*ptr = head;
        head = head->next;
        free(ptr);
    }
    free(head);
    head = NULL;
    return head;
}


int main()
{
    struct Node *head;
    //尾插十个数
    init(&head);
    for(int i = 0; i<10; i++)
    {
        insert_tail(head,i);
    }
    print(head);
    //头插
    insert_head(head,88);
    print(head);
    printf("%d\n",len(head));//链表长度
    //中间插
    insert_index(head, 11,666);
    print(head);
    printf("%d\n",len(head));//链表长度
    //按位置更新
    update_index(head, 12, 88);
    update_index(head, 2, 88);
    print(head);
    //按值更新
    update_value(head,88,888);
    print(head);
    //按位置删除
    delete_index(head,1);
    print(head);
    //按值删除
    //delete_value(head,888);
    //print(head);
    //按位查找
    search_index(head,1);
    //按值查找
    search_value(head,898);
    //冒泡排序
    sort(head);
    print(head);
    //逆序(非递归)
    nixu1(head);
    print(head);
    //逆序(递归)
    nixu1(head);
    print(head);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值