📄 test24.c
字号:
/***Link operator***/
#include "myhdr.h"
struct data{
char name[20];
struct data *next;
};
typedef struct data PERSON;
typedef PERSON *LINK;
void name_print(LINK p);
LINK creat_list();
void delete_node(LINK p);
void add_node(LINK p);
int get_len(LINK p);
LINK head=NULL;
void menu();
int query_node(LINK p);
int delete_query_node(LINK p);
int
main()
{
head=creat_list();
name_print(head);
menu();
return 0;
}
void name_print(LINK p)
{
LINK q;
q=p;
while(q!=NULL)
{
printf("%s\n",q->name);
q=q->next;
}
}
LINK creat_list()
{
LINK head=NULL;
LINK new =NULL;
LINK current=NULL;
LINK precurr=NULL;
while(strcmp(new->name,"exit")!=0)
{
new=(LINK)malloc(sizeof(PERSON));
if(NULL==new)
{
printf("%s\n","Memmory allocted error");
exit(-1);
}
if(head==NULL)
{
new->next=NULL;
head=new;
current=head;
}
else
{
precurr=current;
current->next=new;
current=new;
current->next=NULL;
new->next=NULL;
}
printf("%s\n","input the name please");
scanf("%s",new->name);
strcpy(current->name,new->name);
}
/***若只有节点"exit",释放为空链***/
if(head->next==NULL)
{
head=NULL;
free(current);
}
/***释放节点"exit"****/
else
{
precurr->next=NULL;
free(current);
}
return head;
}
void delete_node(LINK p)
{
int count;
int length;
int i;
LINK q;
LINK de_node;
q=p;
/****若链为空,退出****/
if(p==NULL)
exit(0);
printf("%s\n","input the delete postion of the node");
scanf("%d",&count);
length=get_len(head);
/***删除位置不合法****/
if(count<1||count>length)
{
printf("%s\n","the postion is invalid");
exit(-1);
}
/***删除链首元素***/
if(count==1)
{
head=q->next;
free(q);
}
else
{
for(i=1;i<count-1;i++)
{
q=q->next;
}
/***删除链尾元素***/
if(count==length)
{
free(q->next);
q->next=NULL;
}
/****删除其它元素****/
else
{
de_node=q->next;
q->next=de_node->next;
free(de_node);
}
}
}
void add_node(LINK p)
{
int count;
int length;
int i;
LINK q;
LINK add_node;
q=p;
printf("%s\n","input the add postion of the node");
scanf("%d",&count);
length=get_len(head);
/***增加节点位置不合法,退出****/
if(count<=0||count>length+1)
{
printf("%s\n","the postion is invalid");
exit(-1);
}
/**为所增加节点分配空间***/
add_node=(LINK)malloc(sizeof(PERSON));
if(NULL==add_node)
{
printf("%s\n","Memmory allocted error");
exit(-1);
}
printf("%s\n","input the name please");
scanf("%s",add_node->name);
/**若增加节点位置在链首***/
if(count==1)
{
/***若为空链***/
if(p==NULL)
{
head=add_node;
head->next=NULL;
}
else
{
add_node->next=head;
head=add_node;
}
}
else
{
for(i=1;i<count-1;i++)
{
q=q->next;
}
/*增加节点位置在链中***/
if(count<length+1)
{
add_node->next=q->next;
q->next=add_node;
}
/**若增加节点位置在链尾***/
else
{
add_node->next=NULL;
q->next=add_node;
}
}
}
int query_node(LINK p)
{
int count;
int i=0;
int j=0;
LINK q;
char name[20];
q=p;
printf("%s\n","input the query node please");
scanf("%s",name);
fflush(stdin);
while(q!=NULL)
{
count=strcmp(name,q->name);
i++;
if(count==0)
{
printf("the node exsist,the postion is: %d\n",i);
j++;
return i;
}
q=q->next;
}
if(j==0)
{
printf("%s\n","the node is not exsist");
return -1;
}
}
int delete_query_node(LINK p)
{
int count=0;
count=query_node(p);
if(count>0)
{
printf("input the count: %d\n",count);
delete_node(head);
return 0;
}
else
{
return -1;
}
}
int get_len(LINK p)
{
LINK q;
int count=1;
q=p;
while(q->next!=NULL)
{
q=q->next;
count++;
}
return count;
}
void menu()
{
int count;
while(1)
{
puts("input you choice");
puts("0 to query");
puts("1 to delete");
puts("2 to add");
puts("3 to delete_query");
puts("4 to exit");
scanf("%d",&count);
fflush(stdin);
if(count==0)
{
query_node(head);
}
else
if(count==1)
{
delete_node(head);
printf("%s\n","The deleted list is:");
name_print(head);
}
else
if(count==2)
{
add_node(head);
printf("%s\n","The added list is:");
name_print(head);
}
else
if(count==3)
{
int return_value;
return_value=delete_query_node(head);
if(return_value==0)
{
printf("%s\n","The deleted list is:");
name_print(head);
}
else
{
}
}
else
if(count==4)
{
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -