c语言 单链表的增删改查、排序以及摧毁(代码)

以下为本人对单链表的学习总结笔记,若有不对之处,望大家指出!!!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>

typedef int Type;

typedef struct List
{
        Type num;

        struct List *next;
}List_t;

List_t *Head = NULL;

int Head_Init()
{
        Head = (List_t *)malloc(sizeof(List_t));
        if(Head == NULL)
        {
                perror("malloc fail\n");
                return -1;
        }

        Head->next = NULL;
        return 0;
}


void List_Add(Type n)
{
        List_t *new = (List_t *)malloc(sizeof(List_t));
        if(new == NULL)
        {
                perror("malloc fail\n");
                return ;
        }
        new->num = n;

        List_t *p = Head;
        while(p->next != NULL)
        {
                p = p->next; 
        }

        p->next = new;
        new->next = NULL;
}

void List_Display()
{
        int i = 0;
        List_t *p = Head->next;

        if(p == NULL)
        {
                printf("list is NULL\n");
                return ;
        }

        printf("*******************\n");
        while(p != NULL)
        {
                printf("[%d] : %d\n", i++, p->num);
                p = p->next;
        }
        printf("*******************\n");
}

void List_Change(Type old, Type new)
{
        int flag = 0;

        List_t *p = Head->next;
        while(p != NULL)
        {
                if(p->num == old)
                {
                        p->num = new;
                        flag = 1;
                }
                p = p->next;
        }

        if(flag == 1)
        {
                printf("change success\n");
        }
        else
        {
                printf("change fail:the num does not exit\n");
        }
}

void List_Delete(Type del_num)
{
        int flag = 0;

        List_t *pre = Head;
        List_t *q   = pre->next;
        if(q == NULL)
        {
                printf("List is NULL");
                return ;
        }
        else if(q->next == NULL)
        {
                if(q->num == del_num)
                {
                        flag = 1;

                        free(q);
                        q = NULL;
                        pre->next = NULL;
                }
        }
        else
        {
                while(q != NULL)
                {
				   if(q->num == del_num)
									{
											flag = 1;

											List_t *tmp = q;
											q = q->next;
											pre->next = q;
											free(tmp);
											tmp = NULL;
									}
									else
									{
											pre = q;
											q = q->next;
									}
							}
					}

					if(flag == 1)
					{
							printf("delete success\n");
					}
					else
					{
							printf("the num does not exit\n");
					}
}

void List_Destroy()
{
        List_t *q   = Head->next;
        List_t *tmp = NULL;
        Head->next  = NULL;

        while(q != NULL)
        {
                tmp = q;
                q = q->next;
                free(tmp);
                tmp = NULL;
        }
        printf("\nDestroy success\n");
}

int List_Length()
{
        int length = 0;

        List_t *p = Head->next;
        while(p != NULL)
        {
                p = p->next;
                length++;
        }
        return length;
}

void List_Sort()
{
    int i, j, length;
	if(Head->next == NULL || Head->next->next == NULL)
	{
			return ;
	}

	List_t *pre = NULL;
	List_t *q = NULL;
	length = List_Length();
	for(j=1; j<length-1; j++)
	{
			for(i=0, pre=Head,q=pre->next; i<length-j; i++)
			{
					if(q->num > q->next->num && q->next != NULL)
					{
							pre->next = q->next;
							q->next = pre->next->next;
							pre->next->next = q;
					}
					else
					{
							q = q->next;
					}
					pre = pre->next;
			}
	}
}

int main(void)
{
        if(Head_Init() != 0)
        {
                printf("head init fail\n");
                return -1;
        }

        int choice = 0;
        while(1)
        {
                printf("\n1.Add 2.Display 3.Exit 4.Change 5.Delete 6.Destroy 7.Sort\    n");
                scanf("%d", &choice);
                switch(choice)
                {
                        case 1:
                        {
                                Type num = 0;
                                printf("input : ");
                                scanf("%d", &num);
                                List_Add(num);
                                break;
                        }
                        case 2:
                        {
                                List_Display();
								                               break;
                        }
                        case 3:
                        {
                                printf("********exit**********\n");
                                return -1;
                        }
                        case 4:
                        {
                                Type old_num, new_num;

                                printf("which num will be changed to which num\n");
                                scanf("%d %d", &old_num, &new_num);
                                List_Change(old_num, new_num);
                                break;
                        }
                        case 5:
                        {
                                Type delete_num;

                                printf("Delete which num\n");
                                scanf("%d", &delete_num);
                                List_Delete(delete_num);
                                break;
                        }
                        case 6:
                        {
								List_Destroy();
                                break;
                        }
                        case 7:
                        {
                                List_Sort();
                                break;
                        }
                        default:
                        {
                                printf("Do not have the chose, choice again,please\n    ");
                                break;
                        }
                }
        }
        return -1;
}

感觉有用的话,帮忙点个赞吧!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值