zoukankan      html  css  js  c++  java
  • 有序表的合并-用链表操作

    /*
    	有序表的合并---用链表实现 
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    typedef int ElemType;
    typedef struct Lnode{
    	ElemType data;
    	struct Lnode * next;
    }LNode, * LinkList;
    
    
    void CreateList_R(LinkList * L){
    	int len;
    	printf("请输入要创建的链表节点个数:");
    	scanf("%d", &len);
    	*L = (LinkList)malloc(sizeof(LNode) * len);
    	int i;
    	int val;
    	LinkList r;
    	r = *L;
    	for(i = 0; i < len; i++){
    		printf("请输入第%d个节点存入的值:
    ", i + 1);
    		scanf("%d", &val);
    		LinkList s = (LinkList)malloc(sizeof(LNode)); 
    		s->data = val;
    		s->next = NULL;
    		r->next = s;
    		r = s;
    	}
    }
    
    void traverse_list(LinkList L){
    	LinkList p = L->next;
    	while(p){
    		printf("%d ", p->data);
    		p = p->next;
    	}
    }
    
    /*
    	有序表合并---用链表实现 
    */
    LinkList MergeList_L(LinkList  La, LinkList  Lb){
    	LinkList  Lc;
    	LinkList pa = La->next;
    	LinkList pb = Lb->next;
    	Lc = La;
    	LinkList pc = La;
    	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);	
    	return Lc;
    }
    int main(){
    	LinkList La;
    	LinkList Lb;
    	LinkList Lc;
    	CreateList_R(&La);
    	CreateList_R(&Lb); 
    	
    	Lc = MergeList_L(La, Lb);
    	traverse_list(Lc);
    	return 0;
    } 
    
    我亦无他,惟手熟尔
  • 相关阅读:
    安装 macbook 双系统( OS X 和 Ubuntu )
    微信小程序初探
    [Javascript] Promise
    [AS/400] 基本概念
    [AS/400] Control Language(CL) 基本概念
    [AS/400] Control Language
    [github] 创建个人网页
    [tools] sublime 使用记录
    [Java] TreeMap
    exe转msi
  • 原文地址:https://www.cnblogs.com/AsuraPlus/p/15413094.html
Copyright © 2011-2022 走看看