【申明:本文仅限于自我归纳总结和相互交流,有纰漏还望各位指出。 联系邮箱:Mr_chenping@163.com】
单向链表(单链表),是链表中最简单的一种。顾名思义单向链表必须从头部开始读取顺序访问每个节点
单向链表特点如下:
一、每个节点包含一个指向链表下一节点的指针。
二、链表最后一个节点指针字段为NULL。
三、为了记住链表的起始位置,我们定义了一个头指针,它不包含任何数据。
创建数据结构:
typedef struct _Sample_List
{
int Data;
struct _Sample_List *next;
}Sample_List;
初始化一个头节点:
Sample_List *Create_Link()
{
Sample_List *head;
head = (Sample_List *)malloc(sizeof(Sample_List));
if(head == NULL)
{
perror("malloc:");
}
head->Data = 0;
head->next = NULL;
return head;
}
顺序插入一个节点(升序插入):
int Insert_Link(Sample_List *head, int valude)
{
Sample_List *current = head;
Sample_List *New; //申请新节点
New = (Sample_List *)malloc(sizeof(Sample_List));
if(New == NULL)
{
perror("malloc new");
}
New->Data = valude;
while((current->next != NULL) && (valude > current->next->Data))
{
current = current->next;
}
if(current->next == NULL) //第一个结点或者查找到最后
{
current->next = New;
New->next = NULL;
}
else //中间插入(注意:crrent和current->next是分别指向两个不同节点)
{
New->next = current->next;
current->next = New;
}
return 0;
}
删除一个已有的节点:
int Delete_Note(Sample_List *head, int valude)
{
Sample_List *history = head; //历史节点
Sample_List *current = head->next; //游动节点
while((current != NULL) && (current->Data != valude))
{
history = current;
current = current->next;
}
if(current == NULL) //等于NULL有两种情况:(1)查找到最后、(2)节点为空
{
if(head->next == NULL)
{
printf("List is empty!\n");
}
else
{
printf("Not find the Node!\n");
}
return -1;
}
else
{
history->next = current->next;
free(current);
printf("Delete Node success!\n");
}
return 0;
}
任意查找一个节点:
int Search_Link(Sample_List *head, int key)
{
Sample_List *current = head->next;
while((current != NULL) && (current->Data != key))
{
current = current->next;
}
if(current == NULL) //链表为空或者没有查找到
{
if(head->next == NULL)
{
printf("Link is empty!\n");
}
else
{
printf("Not find!\n");
}
return -1;
}
else
{
printf("find it!\n");
}
return 0;
}
链表逆序(逆置):
//方法一:插入法,依次把节点插入到head后面
Sample_List *Reverse_Link(Sample_List *head)
{
//----单链表的就地逆置----//
Sample_List *current,*record;
if(head->next == NULL || head->next->next == NULL) //没有节点或节点就只有一个
{
return head;
}
current = head->next; //指向第一个节点
head->next = NULL; //断开头结点和第一个节点
while(current != NULL)
{
record = current->next; //record保存current下一个节点
current->next = head->next; //当前节点后驱前指
head->next = current; //head一直指向刚调整的节点
current = record; //current指针后移
}
return head;
}
Sample_List *Reverse_Link(Sample_List **head)
{
Sample_List* PrevNode;
Sample_List* NextNode;
if(NULL == (*head)->next || NULL == (*head)->next->next)
return *head;
NextNode = (*head)->next;
(*head)->next = NULL;
while(NextNode != NULL)
{
PrevNode = NextNode;
NextNode = NextNode->next;
PrevNode->next = (*head)->next;
(*head)->next = PrevNode;
}
return *head;
}
遍历输出链表节点:
/*遍历链表*/
void Print_Link(Sample_List *head)
{
Sample_List *current = head->next;
printf("\nLink as fallows:\n");
while(current != NULL)
{
printf("%d-->", current->Data);
current = current->next;
}
printf("\n");
}
测定链表长度:
/*测定链表的长度,即有多少个结点*/
int Length_Link(Sample_List *head)
{
Sample_List *current = head->next;
int count = 0;
while(current != NULL)
{
count++;
current = current->next;
}
return count;
}
释放链表(包括头节点):
/*释放链表*/
void Free_Link(Sample_List *head)
{
Sample_List *current = head->next;
Sample_List *record = head;
while(current != NULL)
{
record = current;
current = current->next;
free(record);
}
free(head);
printf("Free Link success!");
exit(0);
}
函数接口调用:
int main()
{
int value=0;
char comd;
Sample_List *head = Create_Link();
while((comd = getchar()) != -1)
{
switch(comd)
{
case 'I':
{
printf("please intput int number:");
scanf("%d", &value);
Insert_Link(head, value);
}
break;
case 'D':
{
int a = -1;
printf("Please input value you want to delete:");
scanf("%d", &a);
Delete_Note(head, a);
}
break;
case 'S':
{
int b = -1, ret;
printf("Please input value you want to search:");
scanf("%d", &b);
ret = Search_Link(head, b);
}
break;
case 'F':
{
Free_Link(head);
}
break;
case 'L':
{
int l;
l = Length_Link(head);
printf("The Node length: %d\n", l);
}
break;
case 'R':
{
Sample_List *p;
p = Reverse_Link(&head);
Print_Link(p);
}
break;
case 'P':
{
Print_Link(head);
}
break;
}
}
return 0;
}