Code:
#include "conio.h"
#include "stdio.h"
#include "alloc.h"
typedef unsigned int Position;
typedef int ElementType;
typedef struct Node {
ElementType Element;
Node *Next;
};
typedef Node *List;
//khoi tao danh sach rong
List MakeNull_List(){
List Header = (List)malloc(sizeof(List));
(Header)->Next = NULL;
return Header;
}
//kiem tra danh sach rong
char Empty_List(List L) {
return L->Next == NULL;
}
//Khoi tao 1 node
Node *Make_Node(ElementType X) {
Node *temp = (Node*)malloc(sizeof(Node));
temp->Element = X;
temp->Next = NULL;
return temp;
}
//xen mot phan tu vao danh sach
void Insert_List(ElementType X, List P) {
Node *Temp = Make_Node(X);
Temp->Next = P->Next;
P->Next = Temp;
}
//nhap dach sach co n phan tu
List Input_List(int n) {
List L = MakeNull_List();
ElementType x;
for(int i = 1; i<=n; i++) {
printf("\tElement %d = ",i);
scanf("%d",&x);
Insert_List(x,L);
}
return L;
}
//xuat danh sach cac so nguyen
void Out_List(List L) {
List temp = L;
while(!Empty_List(temp)) {
printf("%d\t",temp->Next->Element);
temp = temp->Next;
}
}
//noi danh sach L2 vao L1 va luu trong L1
void Connect(List L1, List L2)
{
List temp = L1;
while(temp->Next!=NULL)
temp = temp->Next;
temp->Next = L2->Next;
}
//the main programming
void main() {
clrscr();
ElementType x;
int n,i;
printf("Nhap so phan tu n = ");
scanf("%d",&n);
printf("Nhap danh sach 1:\n");
List L1 = Input_List(n);
printf("Nhap danh sach 2:\n");
List L2 = Input_List(n);
printf("Danh sach 1:\n");
Out_List(L1);
printf("\nDanh sach 2:\n");
Out_List(L2);
Connect(L1,L2);
printf("\nDanh sach 1:\n");
Out_List(L1);
printf("\nDanh sach 2:\n");
Out_List(L2);
getch();
}