连接两个链表

| January 3, 2008 18:49 | timmy | Via Original

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct worker_struct{
  char name[20];
  int salary;
  struct worker_struct *next;

};
void main()
{
  void print_work(struct worker_struct *head);
  struct worker_struct *creat();
  struct worker_struct *link(struct worker_struct *list1,struct worker_struct *list2);
    struct worker_struct *list1,*list2;              
  list1=creat();
  list2=creat();
    link(list1,list2);
    print_work(list1);    
}
struct worker_struct *creat()
{
  struct worker_struct *head,*tail,*p;
  int salary;
  char name[20];
  int size=sizeof(struct worker_struct);
  head=tail=NULL;
  printf("Input name salary:\n");
  scanf("%s%d",name,&salary);
  while(salary>=0){
       p=(struct worker_struct *)malloc(size);
       strcpy(p->name,name);    
       p->salary=salary;
       p->next=NULL;
       if(head==NULL)
         head=p;
       else
         tail->next=p;
       tail=p;
       scanf("%20s%6d",name,&salary);
  }
  return head;
}  
void print_work(struct worker_struct *head)
{
      struct worker_struct *ptr;
    if(head==NULL){
      printf("\nNO Records\n");
      return;
    }
    printf("\nThe Workers' Records Are: \n");
    printf("   Name      salary\n");
    for(ptr=head;ptr;ptr=ptr->next)
    printf("    %s      %6d\n",ptr->name,ptr->salary);
}
struct worker_struct *link(struct worker_struct *list1,struct worker_struct *list2)
{

   struct worker_struct *ptr,*ptr1,*ptr2;
   ptr1=list1;
   ptr2=list1->next;
   ptr=list2;
   while(ptr2!=NULL)
   {
     ptr1=ptr2;
     ptr2=ptr1->next;
   }
   ptr1->next=ptr;
   return list1;
}
Tags:
Program/Code » C/C++ | Comments(0) | Trackbacks(0) | Reads(260)
Add a comment
 Site URI
 Email
  Password Optional
 Nickname  *  [Register]
               

 
Emots
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
Enable HTML
Enable UBB
Enable Emots
Hidden
Remember