#include<stdio.h>
#include<stdlib.h>
typedef struct part
{
int number;
struct part *next;
}node;
node *search(node*head,int n);
node*insert(node*head,int n);
node*dele(node*head,int n);
void print(node*head);
//
int main()
{
node *head;
head=(node *)malloc(sizeof(node));
head->next=NULL;
node *last;
char ch;
int n;
for(; ;)
{
printf("input ch\n");
scanf(" %c",&ch);
switch(ch)
{
case'i':
printf("input n");scanf("%d",&n);
last=insert(head,n);
break;
case'd':
printf("input n");
scanf("%d",&n);
last=dele(head,n);
break;
case's':
printf("input n");
scanf("%d",&n);
last=search(head,n);
break;
case'p':
print(head);break;
default:
printf("you make a mistake");
}
}
return 1;}
node* insert(node *head,int n)
{
node *new_node;
new_node=(node *)malloc(sizeof(node));
if(new_node==NULL)
{
printf("no space lefted");
exit(0);
}
else
{new_node->number=n;
node *middle;
middle=head;
while(middle->next!=NULL)
middle=middle->next;
middle->next=new_node;
new_node->next=NULL;
return new_node;
}
}
node * dele(node*head,int n)
{
node *ctr,*ptr,*del;
for(ctr=head->next,ptr=head;ctr->number !=n&&ctr!=NULL;ptr=ctr,ctr=ctr->next);
if(ctr==NULL)
return head;
else
{ptr->next=ctr->next;
del=ctr;
free(ctr);}
return del;
}
node *search(node*head,int n)
{
node *ptr;
for(ptr=head;ptr!=NULL;ptr=ptr->next)
if(ptr->number=n)
return ptr;
else
return NULL;
}
void print(node*head)
{
node *ptr;
for(ptr=head->next;ptr!=NULL;ptr=ptr->next)
printf("%d ",ptr->number);
}
程序代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct part
{
int number;
struct part *next;
}node;
node *search(node*head,int n);
node*insert(node*head,int n);
node*dele(node*head,int n);
void print(node*head);
//
int main()
{
node *head;
head=(node *)malloc(sizeof(node));
head->next=NULL;
node *last;
char ch;
int n;
for(; ;)
{
printf("input ch\n");
scanf(" %c",&ch);
switch(ch)
{
case'i':
printf("input n");scanf("%d",&n);
last=insert(head,n);
break;
case'd':
printf("input n");
scanf("%d",&n);
last=dele(head,n);
break;
case's':
printf("input n");
scanf("%d",&n);
last=search(head,n);
break;
case'p':
print(head);break;
default:
printf("you make a mistake");
}
}
return 1;}
node* insert(node *head,int n)
{
node *new_node;
new_node=(node *)malloc(sizeof(node));
if(new_node==NULL)
{
printf("no space lefted");
exit(0);
}
else
{new_node->number=n;
node *middle;
middle=head;
while(middle->next!=NULL)
middle=middle->next;
middle->next=new_node;
new_node->next=NULL;
return new_node;
}
}
node * dele(node*head,int n)
{
node *ctr,*ptr,*del;
for(ctr=head->next,ptr=head;ctr->number !=n&&ctr!=NULL;ptr=ctr,ctr=ctr->next);
if(ctr==NULL)
return head;
else
{ptr->next=ctr->next;
del=ctr;
free(ctr);}
return del;
}
node *search(node*head,int n)
{
node *ptr;
for(ptr=head;ptr!=NULL;ptr=ptr->next)
if(ptr->number=n)
return ptr;
else
return NULL;
}
void print(node*head)
{
node *ptr;
for(ptr=head->next;ptr!=NULL;ptr=ptr->next)
printf("%d ",ptr->number);
}