#include <stdio.h>
#include <stdlib.h>
typedef struct manage
{
int num;
int name;
struct manage *next;
}STU;
STU *creat_link(int num)
{
STU *head,*pf,*pb;
int i;
for(i=0;i<num;i++)
{
pb=(STU *)malloc(sizeof(STU));
printf("please in intermation\n");
scanf("%d %d",&pb->num,&pb->name);
if(i==0)
{
head=pf=pb;
}
else
{
pf->next=pb;
pf=pb;
}
}
pb->next=NULL;
return (head);
}
STU *delete_link(STU *head,int num)
{
int flag=0;
STU *pf;
STU *temp=head;
while(temp!=NULL)
{
if(num==temp->num)
{
flag=1;
if(temp==head)
{
head=head->next;
}
else if(temp->next==NULL)
{
pf->next=NULL;
}
else
{
pf->next=temp->next;
}
free(temp);
break;
}
pf=temp;
temp=temp->next;
}
return head;
}
void print_all(STU *head)
{
while(head!=NULL)
{
printf("%d%d\n",head->num,head->name);
head=head->next;
}
}
STU *add_link(STU *head,STU *p_new)
{
STU *temp=head;
if(temp!=NULL)
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p_new;
}
else
{
temp=p_new;
}
p_new->next=NULL;
return (head);
}
STU* insert_body(STU *head,STU *p_new)
{
STU *temp=head,*pf;
int flag=0;
if(temp!=NULL)
{
while(temp!=NULL)
{
if(p_new->num<temp->num)
{
if(temp==head)
{
p_new->next=head;
head=p_new;
}
else
{
pf->next=p_new;
p_new->next=temp;
}
flag=1;
break;
}
pf=temp;
temp=temp->next;
}
if(flag==0)
{
pf->next=p_new;
p_new->next=NULL;
}
}
else
{
head=p_new;
p_new->next=NULL;
}
return head;
}
void bublle_link(STU *head)
{
STU *temp=head,*temp_n,*current,change;
while(temp->next!=NULL)
{
temp_n=temp->next;
while(temp_n->next!=NULL)
{
if(temp->num>temp_n->num)
{
change=*temp;
*temp=*temp_n;
*temp_n=change;
current=temp->next;
temp->next=temp_n->next;
temp_n->next=current;
}
temp_n=temp_n->next;
}
temp=temp->next;
}
}
int main(void)
{
STU *head=NULL,*p,*p_new;
int n,num;
printf("please in link count\n");
scanf("%d",&n);
head=creat_link(n);
print_all(head);
printf("please add information\n");
p_new=(STU *)malloc(sizeof(STU));
scanf("%d%d",&p_new->num,&p_new->name);
add_link(head,p_new);
print_all(head);
printf("please input delete num\n");
scanf("%d",&num);
delete_link(head,num);
print_all(head);
printf("please input insert information");
p_new=(STU *)malloc(sizeof(STU));
scanf("%d%d",&p_new->num,&p_new->name);
insert_body(head,p_new);
print_all(head);
bublle_link(head);
print_all(head);
}
#include <stdlib.h>
typedef struct manage
{
int num;
int name;
struct manage *next;
}STU;
STU *creat_link(int num)
{
STU *head,*pf,*pb;
int i;
for(i=0;i<num;i++)
{
pb=(STU *)malloc(sizeof(STU));
printf("please in intermation\n");
scanf("%d %d",&pb->num,&pb->name);
if(i==0)
{
head=pf=pb;
}
else
{
pf->next=pb;
pf=pb;
}
}
pb->next=NULL;
return (head);
}
STU *delete_link(STU *head,int num)
{
int flag=0;
STU *pf;
STU *temp=head;
while(temp!=NULL)
{
if(num==temp->num)
{
flag=1;
if(temp==head)
{
head=head->next;
}
else if(temp->next==NULL)
{
pf->next=NULL;
}
else
{
pf->next=temp->next;
}
free(temp);
break;
}
pf=temp;
temp=temp->next;
}
return head;
}
void print_all(STU *head)
{
while(head!=NULL)
{
printf("%d%d\n",head->num,head->name);
head=head->next;
}
}
STU *add_link(STU *head,STU *p_new)
{
STU *temp=head;
if(temp!=NULL)
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p_new;
}
else
{
temp=p_new;
}
p_new->next=NULL;
return (head);
}
STU* insert_body(STU *head,STU *p_new)
{
STU *temp=head,*pf;
int flag=0;
if(temp!=NULL)
{
while(temp!=NULL)
{
if(p_new->num<temp->num)
{
if(temp==head)
{
p_new->next=head;
head=p_new;
}
else
{
pf->next=p_new;
p_new->next=temp;
}
flag=1;
break;
}
pf=temp;
temp=temp->next;
}
if(flag==0)
{
pf->next=p_new;
p_new->next=NULL;
}
}
else
{
head=p_new;
p_new->next=NULL;
}
return head;
}
void bublle_link(STU *head)
{
STU *temp=head,*temp_n,*current,change;
while(temp->next!=NULL)
{
temp_n=temp->next;
while(temp_n->next!=NULL)
{
if(temp->num>temp_n->num)
{
change=*temp;
*temp=*temp_n;
*temp_n=change;
current=temp->next;
temp->next=temp_n->next;
temp_n->next=current;
}
temp_n=temp_n->next;
}
temp=temp->next;
}
}
int main(void)
{
STU *head=NULL,*p,*p_new;
int n,num;
printf("please in link count\n");
scanf("%d",&n);
head=creat_link(n);
print_all(head);
printf("please add information\n");
p_new=(STU *)malloc(sizeof(STU));
scanf("%d%d",&p_new->num,&p_new->name);
add_link(head,p_new);
print_all(head);
printf("please input delete num\n");
scanf("%d",&num);
delete_link(head,num);
print_all(head);
printf("please input insert information");
p_new=(STU *)malloc(sizeof(STU));
scanf("%d%d",&p_new->num,&p_new->name);
insert_body(head,p_new);
print_all(head);
bublle_link(head);
print_all(head);
}