C語言 鏈表的簡單操作

/*一、實驗題目
1.設有兩個無頭節點的單鏈表,頭指針分別爲ha和hb,鏈中有數據域data,鏈域next,
兩鏈表的數據都按遞增序存放,現要求將hb表歸到ha表中,且歸併後ha仍遞增序,
歸併中ha表中已有的數據若hb中也有,則hb中的數據不歸併到ha中,hb的鏈表在算法中不允許破壞。*/

#include<stdio.h>
#include<stdlib.h>

//判斷數據n是否在頭指針爲head的鏈表中出現,出現則返回true,否則返回false
bool isExist(struct intNode *head,int n);
//建立一個先進先出的鏈表
void createStack(struct intNode **headp);
//給頭指針爲head的鏈表冒泡排序
void sortchain(struct intNode *head);
//輸出頭指針爲head的鏈表
void output(struct intNode *head);

struct intNode
{
 int data;
 struct intNode *next;
};

bool isExist(struct intNode *head,int n)
{
 struct intNode *p=NULL;
 p=head->next;
 while(p!=NULL)
 {
  if(p->data==n)
   return true;
  p=p->next;
 }
 return false;
}

void createStack(struct intNode **headp)
{
 int n;
 struct intNode *p=NULL,*p1=NULL,*tail=NULL; 

 printf("input some integers end of \"0\"\n");
 scanf("%d",&n);
 p=(struct intNode *)malloc(sizeof(struct intNode));
 p->data=n;
 p->next=NULL;
 tail=p;

 p1=(struct intNode *)malloc(sizeof(struct intNode));  
 p1->data=0;
 p1->next=p;
 *headp=p1;
 
 scanf("%d",&n);
 while(n)
 {
  if(!isExist(*headp,n))
  {
   p=(struct intNode *)malloc(sizeof(struct intNode));  
   p->data=n;
   p->next=NULL;
   tail->next=p;
   tail=p;
  }
  scanf("%d",&n);
 }
}

void sortchain(struct intNode *head)
{
 int temp=0;
 struct intNode *p1=NULL,*p2=NULL;
 
 for(p1=head->next;p1!=NULL;p1=p1->next)
  for(p2=p1->next;p2!=NULL;p2=p2->next)
   if(p1->data>p2->data)
   {
    temp=p2->data;
    p2->data=p1->data;
    p1->data=temp;
   }
}

void unionNode(struct intNode *heada,struct intNode *headb)
{
 struct intNode *p=NULL;
 struct intNode *pa=heada->next,*pb=headb->next;
 struct intNode *taila=heada;

 while(taila->next!=NULL)
 {
  taila=taila->next;
 }
 while(pb!=NULL)
 {
  if(!isExist(heada,pb->data))
  {
   if(pb->data<heada->next->data)
   {
    p=(struct intNode *)malloc(sizeof(struct intNode));
    p->data=pb->data;
    p->next=heada->next;
    heada->next=p;
   }
   else if(pb->data>taila->data)
   {
    p=(struct intNode *)malloc(sizeof(struct intNode));
    p->data=pb->data;
    p->next=NULL;
    taila->next=p;
    taila=p;
   }
   else
    while(pa->next!=NULL)
    {
     if((pb->data)>(pa->data)&&(pb->data)<(pa->next->data))
     {
      p=(struct intNode *)malloc(sizeof(struct intNode));
      p->data=pb->data;
      p->next=pa->next;
      pa->next=p;
      break;
     }
     pa=pa->next;
    }
  }
  pb=pb->next;
 }
}

void output(struct intNode *head)
{
 struct intNode *p=NULL;
 p=head->next;
 while(p!=NULL)
 {
  printf("%d ",p->data);
  p=p->next;
 }
 printf("\n");
}

void main()
{
 struct intNode *ha,*hb;

 printf("Create a:");
 createStack(&ha);//建立鏈表a
 sortchain(ha);//給a排序

 printf("Create b:");
 createStack(&hb);//建立鏈表b 
 sortchain(hb);//給b排序

 printf("--------------------------\n");
 printf("a:"); output(ha);//輸出a
 printf("b:"); output(hb);//輸出b

 unionNode(ha,hb);
 printf("--------------------------\n");
 printf("a+b:");
 output(ha);

 printf("--------------------------\n");
 printf("b:");
 output(hb);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章