标签:gif printf ext include null class opened one 有序
笔记和代码的思路来源:
好大学慕课浙江大学陈越、何钦铭的《数据结构》
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef int ElementType; 5 typedef struct Node *PtrToNode; 6 struct Node { 7 ElementType Data; 8 PtrToNode Next; 9 }; 10 typedef PtrToNode List; 11 12 List Read(); /* 细节在此不表 */ 13 void Print( List L ); /* 细节在此不表;空链表将输出NULL */ 14 15 List Merge( List L1, List L2 ); 16 17 18 19 int main() 20 { 21 List L1, L2, L; 22 L1 = Read(); 23 L2 = Read(); 24 25 L = Merge(L1, L2); 26 Print(L); 27 Print(L1); 28 Print(L2); 29 30 return 0; 31 } 32 33 34 35 36 /* 你的代码将被嵌在这里 */ 37 void Print(PtrToNode list){ 38 PtrToNode p = list->Next; 39 if(p==NULL){ 40 printf("NULL\n"); 41 return; 42 } 43 while(p!=NULL){ 44 printf("%d ",p->Data); 45 p = p->Next; 46 } 47 printf("\n"); 48 } 49 50 List Read(){ 51 int count,i,value; 52 scanf("%d",&count); 53 PtrToNode list = (PtrToNode)malloc(sizeof(struct Node)); 54 PtrToNode rear = list; 55 if(list){ 56 list->Next=NULL; 57 } 58 for(i=0;i<count;i++){ 59 PtrToNode newNode = (PtrToNode)malloc(sizeof(struct Node)); 60 scanf("%d",&value); 61 newNode->Data=value; 62 newNode->Next = rear->Next; 63 rear->Next = newNode; 64 rear=newNode; 65 } 66 return list; 67 } 68 69 70 List Merge( List L1, List L2 ){ 71 PtrToNode temp; 72 PtrToNode list = (PtrToNode)malloc(sizeof(struct Node)); 73 PtrToNode rear = list; 74 PtrToNode p1=L1->Next,p2=L2->Next; 75 if(list){ 76 list->Next=NULL; 77 } 78 while(p1 && p2){ 79 if(p1->Data<=p2->Data){ 80 temp = p1; 81 rear->Next = temp; 82 rear=temp; 83 p1 = p1->Next; 84 }else{ 85 temp = p2; 86 rear->Next = temp; 87 rear=temp; 88 p2 = p2->Next; 89 } 90 } 91 92 while(p1){ 93 temp = p1; 94 rear->Next = temp; 95 rear=temp; 96 //free(p1); 97 p1 = p1->Next; 98 } 99 while(p2){ 100 temp = p2; 101 rear->Next = temp; 102 rear=temp; 103 //free(p2); 104 p2 = p2->Next; 105 } 106 L1->Next=NULL; 107 L2->Next=NULL; 108 //Print(list); 109 return list; 110 111 }
标签:gif printf ext include null class opened one 有序
原文地址:http://www.cnblogs.com/yghjava/p/6653470.html