以下为本人对单链表的学习总结笔记,若有不对之处,望大家指出!!!
#include
#include
#include
#include
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; jnext; inum > 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;
}
感觉有用的话,帮忙点个赞吧!!