⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dynamic link.c

📁 动态链表的设计与使用
💻 C
字号:
#define NULL 0
#include<stdio.h>
#include<malloc.h>
typedef int ElemType;
typedef struct LNode
{
  ElemType data;
  struct LNode *next;
}LNode;
LNode *creatlist()
{
   LNode *head,*r,*s;
   ElemType x;
   head=(LNode *)malloc(sizeof(LNode));
   r=head;
   printf("please putin the elems,end with '0'\n");
   scanf("%d",&x);
   while(x!=0)
   {
     s=(LNode *)malloc(sizeof(LNode));
     s->data=x;
     r->next=s;
     s->next=NULL;
     r=s;
     scanf("%d",&x);
    }
   r->next=NULL;
   return(head);
}
void print(LNode *head)
{
   LNode *p;
   p=head;
   while(p!=NULL)
   {
     printf("%d\n",p->data);
     p=p->next;
    }
}
void daozhi(LNode *head)
{
   LNode *p,*q,*r;
   p=head;
   q=p->next;
   while(q!=NULL)
   {
     r=q->next;
     q->next=p;
     p=q;
     q=r;
    }
   head->next=NULL;
   head=p;
   printf("the list is:\n");
   print(head);
}
void shanchu(LNode *head)
{LNode *p,*c,*s,*r;
   c=(LNode *)malloc(sizeof(LNode));
   p=head;
   r=c;
  while(p!=NULL)
  {if((p->data)%2)
   {s=(LNode *)malloc(sizeof(LNode));
    s->next=NULL;
    s->data=p->data;
	r->next=s;
	r=s;
	}
   p=p->next;
  }
  printf("shanchu's list is:\n");
print(c);
}

void youxu(LNode *headb,ElemType x)
{   LNode *p,*q,*s;
    s=(LNode *)malloc(sizeof(LNode));
   
    s->data=x;
    s->next=NULL;
    if(headb==NULL||x<headb->data)
     {s->next=headb;headb=s;}
    else 
	{   q=headb;p=q->next;
      while(p!=NULL&&x>=p->data)
      {
       if(s->data>=p->data)
	   {q=p;p=p->next;}
	  }
	 s->next=p;q->next=s;

     }


}
LNode *danxiang()
{  
 LNode *head;
	int a,i,b[100];
	head=(LNode *)malloc(sizeof(LNode));
	
    printf("how long:\n");
    scanf("%d",&a);
	printf("please putin the first elem:\n");
    scanf("%d",&b[1]);head->data=b[1];head->next=NULL;
    for(i=2;i<=a;i++)
    {printf("please putin the elem which your list's number:\n");
     scanf("%d",&b[i]);
     youxu(head,b[i]);
    }
return(head);
}
void hebing(LNode *heade,LNode *headf)
{
   LNode *c,*p,*q,*s,*r;
   c=(LNode *)malloc(sizeof(LNode));
   heade=danxiang();
   headf=danxiang();
   r=c;p=heade;
   q=headf;
   while(p!=NULL&&q!=NULL)
    {
     if(p->data>=q->data)
      {
       s=(LNode *)malloc(sizeof(LNode));
       s->data=q->data;
       r->next=s;
       r=s;
       q=q->next;
       }
     else 
      {
      s=(LNode *)malloc(sizeof(LNode));
      s->data=p->data;
      r->next=s;
      r=s;
     p=p->next;
      }
     
    }
   if(p!=NULL)r->next=p;
   if(q!=NULL)r->next=q;
   daozhi(c);
 
}
main(){
LNode *head,*heada,*headb,*heade;
head=creatlist();print(head);
shanchu(head);
heade=creatlist();
daozhi(heade);
hebing(heada,headb);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -