linklist_no.h:
#ifndef _LINKLIST_NO_H
#define _LINKLIST_NO_H
#define OK 1
#define ERROR 0
typedef struct node_s
{
int data;
struct node_s *next;
}node_t;
node_t *LinklistInit();
int ListInsert_head(node_t **head);
int ListInsert_tail(node_t **head);
int Linklist_Delete(node_t **head);
int reverse(node_t **head);
void ListDisplay(node_t *head);
#endif
linklist_no.c:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"linklist-no.h"
node_t *LinklistInit()
{
node_t *head=NULL;
return head;
}
void ListDisplay(node_t *head)
{
if(head==NULL)
return ;
node_t *tmp=head;
while(tmp)
{
printf("%d\n",tmp->data);
tmp=tmp->next;
}
}
int reverse(node_t **head)
{
if(*head==NULL)
{
return ERROR;
}
if((*head)->next==NULL)
return OK;
/* node_t *p_cur=(*head)->next;
node_t *p_next=p_cur->next;
(*head)->next=NULL;
while(p_cur)
{
p_cur->next=*head;
*head=p_cur;
p_cur=p_next; 遍历到最后p_cur会到达NULL的位置,
p_next=p_next->next; p_next会到达NULL的后继,这样做
非法操作了内存,造成段错误
} */
node_t *p_cur=(*head)->next;
node_t *p_next=(*head)->next;
(*head)->next=NULL;
while(p_cur)
{
p_next=p_next->next; //当p_cur为真时,p_next 才后移,这样就不会越界
p_cur->next = *head;
*head = p_cur;
p_cur = p_next;
}
return OK;
}
int Linklist_Delete(node_t **head)
{
if(*head==NULL)
{
return ERROR;
}
node_t *p_per=*head;
node_t *p_cur=(*head)->next;
int data;
printf("请输入要删除结点的数据域:");
scanf("%d",&data);
if((*head)->data==data)
{
*head=p_cur;
free(p_per);
}
else
{
while(p_cur)
{
if(p_cur->data==data)
{
p_per->next=p_cur->next;
p_cur->next=NULL;
free(p_cur);
return OK; //正确删除之后,返回ok
}
else
{
p_per=p_cur;
p_cur=p_cur->next;
}
}
}
return ERROR;
}
int ListInsert_tail(node_t **head)
{
node_t *new_node=(node_t *)malloc(sizeof(node_t));
if(new_node==NULL)
{
return ERROR;
}
memset(new_node,0,sizeof(node_t));
if(*head==NULL)
{
printf("请输入数据域:");
scanf("%d",&new_node->data);
new_node->next=*head;
*head=new_node;
}
else
{
node_t *tmp=*head;
while(tmp->next)
tmp=tmp->next;
printf("请输入数据域:");
scanf("%d",&new_node->data);
new_node->next=NULL;
tmp->next=new_node;
}
return OK;
}
int ListInsert_head(node_t **head)
{
node_t *new_node=(node_t *)malloc(sizeof(node_t));
if(new_node==NULL)
{
return ERROR;
}
memset(new_node,0,sizeof(node_t));
printf("请输入数据域:");
scanf("%d",&new_node->data);
new_node->next=*head;
*head=new_node;
return OK;
}
linklist_no_main.c:
#include<stdio.h>
#include<stdlib.h>
#include"linklist-no.h" //起初没有包含.h头文件声明,引起 "node_t" 编译错误
int main()
{
printf("来啦老弟!\n");
node_t *head=LinklistInit();
int i;
for(i=0;i<3;i++)
ListInsert_head(&head);
printf("此时通讯录为:\n");
ListDisplay(head);
int cmd;
while(1)
{
printf("Please enter the following keys:\n");
printf("1,Tail interpolation adds users:\n");
printf("2,Deleting users through data fields:\n");
printf("3,Reverse:\n");
printf("4,Exit:\n");
scanf("%d",&cmd);
switch(cmd)
{
case 1:
ListInsert_tail(&head);
printf("尾插之后的通讯录为:\n");
ListDisplay(head);
break;
case 2:
Linklist_Delete(&head);
printf("删除之后的通讯录为:\n");
ListDisplay(head);
break;
case 3:
reverse(&head);
printf("逆序之后的通讯录为:\n");
ListDisplay(head);
break;
case 4:
exit(0);
break;
}
}
return OK;
}