zoukankan      html  css  js  c++  java
  • 数据结构趣题——在原表空间进行链表的归并

       1: #include <stdio.h>
       2: #include <stdlib.h>
       3:  
       4: /*定义int为ElemType类型*/
       5: typedef int ElemType;
       6:  
       7: /*定义链表的结点类型*/
       8: typedef struct node {
       9:     ElemType data;   /*数据域*/
      10:     struct node *next;  /*指针域*/
      11: } LNode, *LinkList;
      12:  
      13: /*创建一个长度为n的链表,并输入数据*/
      14: LinkList GreatLinkList(int n) {
      15:     LinkList p, r, list = NULL;
      16:     ElemType e;
      17:     int i;
      18:  
      19:     for(i = 1; i <= n; i++) {
      20:         scanf("%d", &e);
      21:         p = (LinkList)malloc(sizeof(LNode));
      22:         p->data = e;
      23:         p->next = NULL;
      24:  
      25:         if(!list)
      26:             list = p;
      27:         else
      28:             r->next = p;
      29:  
      30:         r = p;
      31:     }
      32:  
      33:     return list;
      34: }
      35:  
      36: /*向链表中插入结点,并向该结点的数据域中存放数据e*/
      37: void insertList(LinkList *list, LinkList q, ElemType e) {
      38:     LinkList p;
      39:     p = ( LinkList)malloc(sizeof(LNode));
      40:     p->data = e;
      41:  
      42:     if(!*list) {
      43:         *list = p;
      44:         p->next = NULL;
      45:     }
      46:     else {
      47:         p->next = q->next;
      48:         q->next = p;
      49:     }
      50: }
      51:  
      52: void insertNode(LinkList *q1, LinkList *q2, LinkList *p, LinkList *l2) {
      53:     if(*q1 == *q2)
      54:     {
      55:         (*p)->next = *q2;
      56:         *l2 = *q2 = *q1 = *p;
      57:  
      58:     }
      59:     else
      60:     {
      61:         (*q2)->next = *p;
      62:         (*p)->next = *q1;
      63:         (*q2) = (*q2)->next;
      64:     }
      65: }
      66:  
      67: void MergeLink(LinkList l1, LinkList l2 , LinkList *l3)
      68: {
      69:     /*将链表l1,l2有序归并,l3指向归并后的新链表*/
      70:     LinkList p, q1, q2;
      71:     q1 = q2  = l2; /*l3指向l2*/
      72:     p = l1;
      73:  
      74:     while(p != NULL && q1 != NULL) {
      75:         if(p->data >= q1->data) {
      76:             q2 = q1;
      77:             q1 = q1->next;
      78:         }
      79:         else {
      80:             l1 = l1->next ;
      81:             insertNode(&q1, &q2, &p, &l2);  /*将p指向的结点茶道q2之前q1之后*/
      82:             // q2 = q2->next;
      83:             p = l1;
      84:         }
      85:     }
      86:  
      87:     if(q1 == NULL) q2->next = p;
      88:  
      89:     *l3 = l2;
      90:  
      91:  
      92: }
      93:  
      94: int main()
      95: {
      96:     ElemType e;
      97:     LinkList  l1, l2, l3, q;
      98:  
      99:     printf("Please input the contents of Link1\n");
     100:     q = l1 = GreatLinkList(1); /*创建一个链表结点,q和l指向该结点*/
     101:  
     102:     scanf("%d", &e);
     103:  
     104:     while(e)             /*循环地输入数据,同时插入新生成的结点*/
     105:     {
     106:         insertList(&l1, q, e) ;
     107:         q = q->next;
     108:         scanf("%d", &e);
     109:     }
     110:  
     111:     printf("Please input the contents of Link2\n");
     112:     q = l2 = GreatLinkList(1); /*创建一个链表结点,q和l指向该结点*/
     113:  
     114:     scanf("%d", &e);
     115:  
     116:     while(e)             /*循环地输入数据,同时插入新生成的结点*/
     117:     {
     118:         insertList(&l2, q, e) ;
     119:         q = q->next;
     120:         scanf("%d", &e);
     121:     }
     122:  
     123:     MergeLink(l1, l2, &l3); /*合并l2,l2并用l3指向合并后的链表*/
     124:  
     125:     q = l3;
     126:  
     127:     printf("The merge of link1 and link 2 is\n");
     128:  
     129:     while(q)            /*打印出合并后的链表*/
     130:     {
     131:         printf("%d ", q->data) ;
     132:         q = q->next;
     133:     }
     134: }
  • 相关阅读:
    防火墙基础知识(持续补充更新)
    GNS3配置问题(持续更新)
    vc程序设计--对键盘与鼠标的响应(1)
    VC程序设计--文字输出方法与字体示例
    Excel vlookup筛选两列的重复项
    centos 软件安装包下载网站
    CentOS7 SSH免密码登录
    nmap 端口扫描工具
    win7 能ping通dns, 但无法解析域名
    转 Windws Server 2012 Server Backup(备份与还原)
  • 原文地址:https://www.cnblogs.com/steven_oyj/p/1745956.html
Copyright © 2011-2022 走看看