typedef struct Node{
int num;
struct Node* next;
}Node;
Node* SetList(void){
Node *head = (Node *)malloc(sizeof(Node));
Node *p = (Node *)malloc(sizeof(Node));
head -> num = 0;
head -> next = p;
int a = 0;
printf("输入一个非降序列,以0结尾:\n");
scanf("%d", &a);
while (a != 0) {
p -> num = a;
p -> next = (Node *)malloc(sizeof(Node));
p = p -> next;
scanf("%d", &a);
}
p -> num = 0;
return head;
}
Node* RevList(Node* head){
Node *pPrev = NULL;
Node *pCur = head -> next;
Node *pNExt = head -> next -> next;
while (pCur -> num != 0) {
pCur -> next = pPrev;
pPrev = pCur;
pCur = pNExt;
if (pCur -> num != 0) {
pNExt = pNExt -> next;
}
}
return pPrev;
}
Node *LinkLists(Node *List1, Node* List2){
Node* pHead = NULL;
if (List1->num >= List2->num) {
pHead = List1;
List1 = List1 -> next;
}
else {
pHead = List2;
List2 = List2 -> next;
}//Set new head
Node* pCur = pHead;
while (List1 != NULL && List2 != NULL) {
if (List1->num >= List2->num) {
pCur -> next = List1;
List1 = List1 -> next;
}
else {
pCur -> next= List2;
List2 = List2 -> next;
}
pCur = pCur -> next;
}
if (List1 != NULL){
pCur -> next = List1;
}
if (List2 != NULL) {
pCur -> next = List2;
}
return pHead;
}
void Print(Node *head){
Node *p = head;
while (p != NULL) {
printf("%d ", p->num);
p = p -> next;
}
putchar('\n');
}
int main(){
Node *List1, *List2, *Linked;
List1 = SetList();
List1 = RevList(List1);
List2 = SetList();
List2 = RevList(List2);
Linked = LinkLists(List1, List2);
Print(Linked);
return 0;
}
typedef struct _node{
int num;
struct _node *next;
struct _node *prev;
}Node;
Node* Create(void){
Node *head = (Node *)malloc(sizeof(Node));
Node *p = (Node *)malloc(sizeof(Node));
head -> next = p;
head -> num = 0;
p -> prev = head;
int number;
printf("Enter a series of number, end with 0:\n");
scanf("%d", &number);
while (number != 0) {
p -> num = number;
p -> next = (Node *)malloc(sizeof(Node));
p -> next -> prev = p;
p = p -> next;
scanf("%d", &number);
}
p = p -> prev;
free(p -> next);
p -> next = head;
return head;
}
Node* RevList_v1(Node* head){
Node *pPrev = head;
Node *pCur = head -> next;
Node *pNExt = head -> next -> next;
while (pCur -> num != 0) {
pCur -> next = pCur -> prev;
pPrev = pCur;
pCur = pNExt;
pNExt = pNExt -> next;
}
pCur -> next = pPrev;
return pPrev;
}
Node* RevList_v2(Node* head){
Node *pCur = head -> next;
Node *pNExt = pCur -> next;
while (pNExt -> num != 0) {
pCur -> next = pCur -> prev;
pCur = pNExt;
pNExt = pNExt -> next;
}
pCur -> next = pCur -> prev;
return pCur;
}
Node *LinkLists(Node *List1, Node* List2){
Node* pHead = NULL;
if (List1->num >= List2->num) {
pHead = List1;
List1 = List1 -> next;
}
else {
pHead = List2;
List2 = List2 -> next;
}//Set new head
Node* pCur = pHead;
while (List1->num != 0 && List2->num != 0) {
if (List1->num >= List2->num) {
pCur -> next = List1;
List1 = List1 -> next;
}
else {
pCur -> next= List2;
List2 = List2 -> next;
}
pCur = pCur -> next;
}
if (List1->num != 0){
pCur -> next = List1;
}
if (List2->num != 0) {
pCur -> next = List2;
}
return pHead;
}
void Print(Node *head){
while (head -> num != 0) {
printf("%d ", head -> num);
head = head -> next;
}
putchar('\n');
}
int main(){
Node *List1, *List2, *LinkedList;
List1 = Create();
List1 = RevList_v1(List1);
printf("Reversed List1:\n");
Print(List1);
List2 = Create();
List2 = RevList_v1(List2);
printf("Reversed List2:\n");
Print(List2);
printf("Linked List:\n");
LinkedList = LinkLists(List1, List2);
Print(LinkedList);
system("pause");
return 0;
}