*****单链表的实现*****
我们在这里只实现单链表里面最常见的几个操作,如果有新功能会进行更新。
《数据结构》一书中讲了线性表的有关算法,实现的数据类型有:静态数组、动态数组、单链表、循环链表和双向链表,这里,我实现了在实际开发中应用比较广阔的两种的数据结构:单链表和双向链表。
(1)单链表的结构定义:
typedef struct list
{
int data;
struct list *next;
}List;
(2)创建一个含有十个结点的单链表并初始化:
<1>前插法:(即每插入一个新元素就把它放到最靠近root结点的地方)List *ForwardCreat_List()
{
List *root;//a root (List *),data is null.
List *cur; //a pointer to current List
List *pre; //a pointer to present List
int i = 0; //a count to creat the number of List
root = (List *)malloc(sizeof(List));
if(NULL == root)
{
printf("ERROR: Can't malloc the root\n");
exit(ERROR);
}
root->next = NULL; //Init the root->next
pre = root->next;
while(++i <= 10)
{
cur = (List *)malloc(sizeof(List));
if(NULL == cur)
{
printf("ERROR: Can't malloc the cur\n");
exit(ERROR);
}
//printf("Creat the current List\n");
//assign the member of current List
cur->data = 2*i - 1;
cur->next = NULL;
//connect the current and present List
root->next = cur;
cur->next=pre;
pre=cur;
}
return root;
}
<2>后插法:(即每插入一个新元素就直接接到链表的最后)
List *BackCreat_List()
{
List *root;
List *cur;
List *pre;
int i = 0;
root = (List *)malloc(sizeof(List));
if(NULL == root)
{
printf("ERROR: Can't malloc the root\n");
exit(ERROR);
}
root->next = NULL;
pre = root;
while(++i <= 10)
{
cur = (List *)malloc(sizeof(List));
if(NULL == cur)
{
printf("ERROR: Can't malloc the cur\n");
exit(ERROR);
}
cur->data = 2*i - 1;
cur->next = NULL;
pre->next = cur;
pre = cur;
}
return root;
}
(3)遍历单链表:(既然创建好了就看看这个链表是否成功创建了,不会调试的话就可以用这个函数看看链表的创建与否)
void Show_List(List *root)
{
List *pre = root->next;
printf("This is the list:\n");
while(NULL != pre)
{
printf("%d\n",pre->data);
pre = pre->next;
}
}
(4)查找结点,返回该结点:(《数据结构》中还介绍了一个LocateElem()函数,该函数返回的是结点在链表的位置,实现方法和接下来的函数差不多)
List *Search_List(List *root)
{
List *cur = root->next;
int data;
printf("Enter the data you want to find:\n");
scanf("%d",&data);
while(NULL != cur)
{
if(data == cur->data)
return cur;
cur = cur->next;
}
return NULL;
}
(5)插入新结点:
List *Insert_List(List *root)
{
int data = 0;
List *cur = root->next;
List *pre = root;
List *newList = NULL;
printf("Enter a data you want to add:\n");
scanf("%d",&data);
while(NULL != cur->next)
{
if(data > cur->data)
{
pre = cur;
cur = cur->next;
}
else
{
newList = (List *)malloc(sizeof(List));
newList->data = data;
newList->next = NULL;
//Insert the new List
pre->next = newList;
newList->next = cur;
return newList;
}
}
newList = (List *)malloc(sizeof(List));
newList->data = data;
newList->next = NULL;
//This statement is wrong...I don't know why.
cur->next = newList;
return newList;
}
(6)删除一个结点:
List *Delete_List(List *root)
{
List *cur = root->next;
List *pre = root;
List *deleteList = NULL;
int data = 0;
printf("Enter a data you want to delete:\n");
scanf("%d",&data);
while(NULL != cur->next)
{
if(data == cur->data)
{
pre->next = cur->next;
deleteList = cur;
free(cur);
//with freeing the cur,the deleteList will become shakable
//So init the deleteList to the return's use
deleteList->next = NULL;
deleteList->data = data;
return deleteList;
}
else
{
pre = cur;
cur = cur->next;
}
}
if(cur->data == data)
{
pre->next = cur->next;
deleteList = cur;
free(cur);
deleteList->next = NULL;
deleteList->data = data;
return deleteList;
}
else
{
printf("Can't find the data\n");
return NULL;
}
}
(7)单链表各个函数的测试:
int main()
{
List *root = NULL;
List *find = NULL;
List *insert = NULL;
List *deleteL = NULL;
//Forward Creat List method and show the List
root = ForwardCreat_List();
Show_List(root);
//Back Creat List method and show the List
root = BackCreat_List();
Show_List(root);
//Find a data in the List
find = Search_List(root);
if(NULL == find)
{
printf("No data\n");
}
else
{
printf("This is the data:\n");
printf("%d\n",find->data);
}
//Insert a new data in the List
insert = Insert_List(root);
if(NULL != insert)
{
printf("Insert this data successfully\n");
Show_List(root);
}
else
{
printf("Sorry to insert failed\n");
}
//Delete a data in the List
deleteL = Delete_List(root);
if(NULL != deleteL)
{
printf("Delete this data successfully\n");
Show_List(root);
}
else
{
printf("Sorry to delete failed\n");
}
system("pause");
return 0;
}
(8)小总结:
有几个月没沾到单链表的实现,只记得思想,这次实现这个东西花了有几个小时,主要还是指针惹的祸啊,我也就只好利用仅有的一丁点的调试方法进行修改代码,不过实现这个单链表的过程让我对调试有了更深的理解,
至少对它有点儿感觉了,调试真是好东西啊。