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

📄 datas_002.cpp

📁 线形表链式存储结构算法 VC++6.0下开发~!
💻 CPP
字号:
#include<stdio.h>
#include<iostream.h>
#include<stdlib.h>

#define  LIST_INIT_SIZE     100 

int m,n;

typedef struct   LNode
					{
						int      data;							 // 数据域
						struct LNode   *next;					 // 指针域
					}LNode,*LinkList;  

void CreateList_L(LinkList &L) 
					{
							int i;
							LinkList p,np;												// 逆序输入 n 个数据元素,建立带头结点的单链表
							printf("Please input the length of list:");
							scanf("%d",&n);
							L = (LinkList)malloc(sizeof(LNode));
							L->next = NULL;										//先建立一个带头结点的单链表
							for (i=n; i>0; --i) 
								{


									p=(LinkList)malloc(sizeof(LNode));
                            //输入元素值
									printf("Please input a list:");
									scanf("%d",&p->data);
									np = L;
									if(L->next)
									while( np->next->data < p->data )
									{ np = np->next;
										if(np->next == NULL)
										break;	
									}
									p->next = np->next; np->next = p;				//插入
								}
							
					}														// CreateList_L


void  MergeList_L(LinkList &La,LinkList &Lb,LinkList &Lc)
				{
                                                                 
																			// 已知单链线性表La和Lb的元素按值非递减排列。
																			 // 归并La和Lb得到新的单链线性表Lc,Lc的元素也按值非递减排列。
					LinkList pa,pb,pc;
					pa=La->next; pb=Lb->next;
					Lc=pc=La;												// 用La的头结点作为Lc的头结点
					while (pa&&pb)
						 { 
							    if  (pa->data <= pb->data)
									{
										pc->next=pa;pc=pa;pa=pa->next;  
									}
								else 
									{
										pc->next=pb;pc=pb;pb=pb->next;
									}
						}
					pc->next=pa?pa:pb;											// 插入 剩余段
					free(Lb);													// 释放Lb的头结点
				}
																// MergeList_L    

void Output_list(LinkList L,int m)			//输出一个线性表;
				{
					LinkList p=L->next;
					int i=0;
					printf("\n");
					while(p)
						{
							printf("%d\n",p->data);
							p=p->next;
						}
} 

void main()
{
	LinkList La,Lb,Lc;
	CreateList_L(La);
//	Output_list(La,m);

	m=n;
	CreateList_L(Lb);
//	Output_list(Lb,m);

	m=m+n;
	MergeList_L(La,Lb,Lc);
	Output_list(Lc,m);
}
		

⌨️ 快捷键说明

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