//It is known that two single linked lists a and B represent two sets respectively, and their elements are arranged incrementally. Please write an assembly

From , 3 Years ago, written in C, viewed 88 times.
URL https://pastebin.vip/view/0d73a250
  1. //已知两个单链表 A 和 B 分别表示两个集合,其元素递增排列。请编写程序求集合
  2. //A 和 B 的交集 C = A∩B,要求单链表C 按其元素递增排列,并利用原表(即表A
  3. //和表B)的结点空间存放表C。
  4.  
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. #include<test2.h>
  8. #include<string.h>
  9. #define Max 100
  10.  
  11. int main()
  12. {
  13.         int i,len1,len2;
  14.         char str[Max];
  15.         LinkList *A,*B;
  16.         A=CreateLinkList(); //为栈A创建一个链表指针
  17.         B=CreateLinkList(); //为栈B创建一个链表指针
  18.         InitLinkList(A);//初始化栈A
  19.         InitLinkList(B);//初始化栈B
  20.  
  21.         printf("输入单链表A的数据元素:\n");
  22.         gets(str);
  23.         len1=strlen(str);
  24.         for(i=0;i<len1;i++)
  25.                 InsertLinkList(A,str[i]);//将数据元素插入栈A
  26.  
  27.         printf("将单链表A的数据元素排序:\n");
  28.         Order(A,len1);//为栈A的数据元素排序
  29.         Display(A);//输出栈A的数据元素
  30.  
  31.         printf("输入单链表B中的数据元素:\n");
  32.         gets(str);
  33.         len2=strlen(str);
  34.         for(i=0;i<len2;i++)
  35.                 InsertLinkList(B,str[i]);//将数据元素插入栈B
  36.  
  37.         printf("将单链表B的数据元素排序:\n");
  38.         Order(B,len2);//为栈B排序
  39.         Display(B);//输出栈B的数据元素
  40.  
  41.         printf("求集合单链表C:\n");
  42.     ConnetLinkList(A,B,len1,len2);//联接栈A和栈B
  43.         Display(A);//输出联接后的栈的数据元素
  44.  
  45.         return 0;
  46. }
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. ///////////////////////////////////////////
  54. /*        test2.cpp                      */
  55. /*    filename:test2.cpp                 */
  56. /*    description:test2的实现文件        */
  57. /*    designer:zhu jian                  */
  58. /*    data:12-10-26                      */
  59. ////////////////////////////////////////////
  60.  
  61.  
  62. #include"stdio.h"
  63. #include"stdlib.h"
  64. #include"test2.h"
  65. #include"string.h"
  66.  
  67.  
  68. LinkList *CreateLinkList(void)//创建一个结点
  69. {
  70.         LinkList *L;
  71.         L=(LinkList *)malloc(sizeof(LinkList));
  72.         if(!L)
  73.                 return NULL;
  74.  
  75.         L->next=NULL;
  76.         return L;
  77. }
  78.  
  79. void InitLinkList(LinkList *L)//初始化链表
  80. {
  81.         LinkList *p;
  82.         p=L;
  83.         if(!L)
  84.                 return ;
  85.  
  86.         p->data=0;
  87. }
  88.  
  89. unsigned char InsertLinkList(LinkList *L,elemtype e)//插入结点元素
  90. {
  91.         LinkList *p,*q;
  92.         p=L;
  93.         if(!L)
  94.                 return ERROR;
  95.  
  96.         while(p->next!=NULL)
  97.                 p=p->next;
  98.         q=(LinkList *)malloc(sizeof(LinkList));
  99.         q->data=e;
  100.         p->next=q;
  101.         q->next=NULL;
  102.         return OK;
  103. }
  104.  
  105. unsigned char Order(LinkList *L,int len)//排序
  106. {
  107.         int i=0;
  108.         elemtype k;
  109.         LinkList *p,*q;
  110.         p=L;
  111.         if(!L)
  112.                 return ERROR;
  113.  
  114.     q=p->next;
  115.         while(i<len)
  116.         {
  117.                 p=L;
  118.                 q=p->next;
  119.                 while(p->next != NULL)
  120.                         if(p->data > q->data)
  121.                         {
  122.                             k=p->data;
  123.                             p->data=q->data;
  124.                             q->data=k;
  125.                             p=p->next;
  126.                             q=q->next;
  127.                         }
  128.                         else
  129.                         {
  130.                                 p=p->next;
  131.                                 q=q->next;
  132.                         }
  133.                 i++;
  134.         }
  135.  
  136.         return OK;
  137. }
  138.  
  139. unsigned char ConnetLinkList(LinkList *L,LinkList *S,int len1,int len2)//联接单链表A和单链表B
  140. {
  141.         int len;
  142.         LinkList *p,*q;
  143.         p=L;
  144.         q=S;
  145.         if(!L || !S)
  146.                 return ERROR;
  147.  
  148.         len=len1+len2;
  149.         while(p->next != NULL)
  150.                 p=p->next;
  151.         p->next=q->next;
  152.         Order(L,len);
  153.         return OK;
  154. }
  155.  
  156. unsigned char Display(LinkList *L)//输出单链表的数据元素
  157. {
  158.         LinkList *p;
  159.         p=L->next;
  160.         if(!L)
  161.                 return ERROR;
  162.  
  163.         while(p->next != NULL)
  164.         {
  165.                 printf("%c",p->data);
  166.                 p=p->next;
  167.         }
  168.         printf("%c\n",p->data);
  169.     return OK;
  170. }
  171.  
  172.  
  173. ///////////////////////////////////////////
  174. /*        test2.h                        */
  175. /*    filename:test2.h                   */
  176. /*    description:test2的头文件          */
  177. /*    designer:zhu jian                  */
  178. /*    data:12-10-26                      */
  179. ////////////////////////////////////////////
  180.  
  181.  
  182. #ifndef _DATA_STRUCTURE_TEST2_H_
  183. #define _DATA_STRUCTURE_TEST2_H_
  184.  
  185.  
  186. #ifndef OK
  187. #define OK 1
  188. #endif
  189.  
  190. #ifndef ERROR
  191. #define ERROR -1
  192. #endif
  193.  
  194. #ifndef NULL
  195. #define NULL 0
  196. #endif
  197.  
  198.  
  199. typedef char elemtype; //定义用elemtype取代char
  200.  
  201.  
  202. typedef struct node{
  203.         elemtype data;
  204.         struct node *next;
  205. }LinkList;
  206.  
  207.  
  208. LinkList *CreateLinkList(void);//创建一个结点
  209.  
  210. void InitLinkList(LinkList *L);//初始化链表
  211.  
  212. unsigned char InsertLinkList(LinkList *L,elemtype e);//插入结点元素
  213.  
  214. unsigned char Order(LinkList *L,int len);//排序
  215.  
  216. unsigned char ConnetLinkList(LinkList *L,LinkList *S,int len1,int len2);//联接单链表A和单链表B
  217.  
  218. unsigned char Display(LinkList *L);//输出单链表的数据元素
  219.  
  220.  
  221. #endif
  222.  
  223.  
  224.  
  225.  
  226.  

Reply to "//It is known that two single linked lists a and B represent two sets respectively, and their elements are arranged incrementally. Please write an assembly"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website