#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define NULL 0
#include<stdio.h>
#include<stdlib.h>
typedef int Status;
typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode *next;
}LNode,*LinkList;
typedef LNode *LinkList;
void CreateList_L(LinkList L, int n);
void MergeList_L(LinkList La, LinkList Lb, LinkList Lc);
void OutputList(LinkList L);
void CreateList_L(LinkList L, int n) {
int i;
LinkList p;
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
for (i = 0; i < n; ++i) {
p = (LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next = L->next; L->next = p;
}
}//CreateList_L
void MergeList_L(LinkList La, LinkList Lb, LinkList Lc) {
LinkList pa, pb, pc;
pa = La->next; pb = Lb->next;
Lc = 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);
}//MergeList_L
void OutputList(LinkList L) {
LinkList q;
q=L->next;
while(q){
printf("%d",q->data);
q=q->next;
}
}//OutputList_L
int main() {
LinkList L1,L2,L3;
printf("Please enter the element of L1:\n");
CreateList_L(L1, 4);
printf("Please enter the element of L2:\n");
CreateList_L(L2, 4);
printf("The LinkList L1 is:\n");
OutputList(L1);
printf("The LinkList L2 is:\n");
OutputList(L2);
MergeList_L(L1, L2, L3);
printf("The LinkList L3 is:\n");
OutputList(L3);
return 0;
}
uj5u.com熱心網友回復:
我的單鏈表可以輸入但無法輸出,語法沒有錯誤,請各位大佬幫忙看一看uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
//#define NULL ((void *)0)
typedef int Status;
typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode *next;
}LNode,*LinkList;
typedef LNode *LinkList;
void CreateList_L(LinkList *L, int n);
void MergeList_L(LinkList La, LinkList Lb, LinkList *Lc);
void OutputList(LinkList L);
void CreateList_L(LinkList *L, int n) {
int i;
LinkList p;
*L = (LinkList)malloc(sizeof(LNode));
(*L)->next = NULL;
for (i = 0; i < n; ++i) {
p = (LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next = (*L)->next;
(*L)->next = p;
}
}//CreateList_L
void MergeList_L(LinkList La, LinkList Lb, LinkList *Lc) {
LinkList pa, pb, pc;
pa = La->next; pb = Lb->next;
*Lc = 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);
}//MergeList_L
void OutputList(LinkList L) {
LinkList q;
q=L->next;
while(q){
printf("%d",q->data);
q=q->next;
}
}//OutputList_L
int main() {
LinkList L1,L2,L3;
printf("Please enter the element of L1:\n");
CreateList_L(&L1, 4);
printf("Please enter the element of L2:\n");
CreateList_L(&L2, 4);
printf("The LinkList L1 is:\n");
OutputList(L1);
printf("The LinkList L2 is:\n");
OutputList(L2);
MergeList_L(L1, L2, &L3);
printf("The LinkList L3 is:\n");
OutputList(L3);
return 0;
}
供參考~
試一下這個版本,然后對比一下這兩個代碼的區別,找出自己的問題;
另外,重點學習一下引數傳遞,傳入引數、傳出引數等等;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/277180.html
標籤:C語言
