单向链表
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct phone
{
int num;
int price;
char brand[16];
struct phone *next;
}phones;
int num = 0;
struct phones *cteate_list(phones **old_output,phones *input)
{
phones *temp;
temp = *old_output;
if(*old_output == NULL)
{
printf("struct phones is empty...\n");
temp = input;
input->next = NULL;
printf("the phone has been added :%s\n its price is :%d\n\n",temp->brand,temp->price);
}
else
{
printf("struct is not empty...\n");
while(temp->next != NULL)
{
printf("current site is not last...\n");
temp = temp->next;
}
temp->next = input;
input->next = NULL;
if(temp->next == NULL)
{
printf("temp->next == NULL\n");
}
else
{
printf("temp->next == somethings\n");
}
printf("the phone has been added :%s\n its price is :%d\n\n",temp->brand,temp->price);
}
return temp;
}
void print_list(phones *in_output)
{
phones *output = in_output;
if(output == NULL)
{
printf("struct is empty...\n");
}
else
{
printf("start to print phones...\n");
while(output != NULL)
{
printf("phones message is:num:%d, brand:%s, price:%d\n\n",output->num,output->brand,output->price);
output = output->next;
}
}
}
void delete_member(phones **output,int del_num)
{
phones *former,*later;
former = later = *output;
if(*output == NULL)
{
printf("nothing to delete...\n");
}
else
{
while(later->num != del_num && later->next != NULL)
{
former = later;
later = later->next;
}
if(later->num == num)
{
if(later == *output)
{
*output = later->next;
}
else
{
former->next = later->next;
}
}
else
{
printf("no such num to delete...\n");
}
}
}
void seek_member(phones **output,int s_num)
{
phones *temp = *output;
if(*output == NULL)
{
printf("nothing to seek...\n");
}
else
{
while(temp != NULL)
{
if(temp->num == s_num)
{
printf("seek successfully,num:%d, price:%d, brand:%s\n",temp->num,temp->price,temp->brand);
break;
}
else
{
printf("no such num...\n");
break;
}
}
}
}
int main()
{
phones *input = NULL,*output = NULL;
input = (struct phone *)malloc(sizeof(struct phone));
input->price = 3;
strcpy(input->brand,"vivo");
output = cteate_list(&output,input);
for(int i = 0;i<3;i++)
{
input = (struct phone *)malloc(sizeof(struct phone));
num++;
input->num = num;
scanf("%d %s",&input->price,input->brand);
cteate_list(&output,input);
}
print_list(output);
while(1)
{
int s_num;
printf("input seek s_num:");
scanf("%d",&s_num);
seek_member(&output,s_num);
if(s_num == 9)
{
break;
}
}
while(1)
{
int d_num;
printf("input delete d_num:");
scanf("%d",&d_num);
delete_member(&output,d_num);
print_list(output);
if(output == NULL)
{
break;
}
}
return 0;
}