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

📄 lianbiao.cpp

📁 用链表实现下面功能:输入两个非降序列
💻 CPP
字号:

#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct link)
struct link
{int n;
 struct link * next;
};

struct link * input(int len)
{struct link *head,*p;
int i,num;
 p=NULL;
 for(i=1;i<=len;i++)
 {scanf("%d",&num);
 head=(struct link *)malloc(LEN);
  head->n=num;
  head->next=p;
  p=head;
 }
 return(head);
}


void insert(struct link * p,struct link *q)
{q->next=p->next;
 p->next=q;
}

struct link * rep(struct link *p,struct link * q)
{struct link *head,*p1,*q1,*p2,*q2;
int gotonext;
 head=p;
 p1=p;
 p2=p1->next;
 q1=q;
 while(q1!=NULL)
 {  gotonext=1;
    q2=q1->next;
	 if(q1->n<=p1->n && ( p2==NULL ||q1->n>p2->n ))
	 {insert(p1,q1);
      p1=p1->next;
	 }
	 else
		 if(q1->n>p1->n)
		 {head=q1;
		 q1->next=p1;
		 p1=head;
		 }
		 else
		 { p1=p2;gotonext=0;}
 p2=p1->next;
if(gotonext) q1=q2;
 }
 return(head);
}

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


void main()
{
 int lena,lenb;
 struct link * alink,* blink,* together;
 printf("please input the lengh of the first link:");
 scanf("%d",&lena);
 printf("请输入非降系列:\n");
 alink=input(lena);
 printf("please input the lengh of the second link:");
 scanf("%d",&lenb);
 printf("请输入非降系列:\n");
 blink=input(lenb);
 output(alink);
 output(blink);
 together=rep(alink,blink);
 printf("非降系列1和2合并后而成的系列为:");
 output(together);
}

⌨️ 快捷键说明

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