Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct _node {
- int data;
- struct _node *next, *prev;
- } node;
- node *addNode(node *head, node **tail, int num)
- {
- node * n = (node*) malloc(sizeof(node));
- n->next = NULL;
- n->prev = *tail;
- n->data = num;
- if (*tail)
- {
- (*tail)->next = n;
- }
- *tail = n;
- if (!head) head = *tail;
- return head;
- }
- node *dsort(node *head, node *tail)
- {
- node*el1 = head, *el2 = head;
- while (el1){
- el2 = head;
- while (el2)
- {
- if (el1->data < el2->data){
- int tmp = el1->data;
- el1->data = el2->data;
- el2->data = tmp;
- }
- el2 = el2->next;
- }
- el1 = el1->next;
- }
- return head;
- }
- node *put(node *headSource, node *head)
- {
- node *elStart = headSource;
- while (elStart)
- {
- node *elf = head;
- if (elStart->data == elf->data)
- {
- while (elf && elStart->data != elf->data)
- {
- elStart = elStart->next;
- }
- node *el = head, *tmp = NULL;
- tmp = elStart->next;
- elStart->next = head;
- while (el->next){
- el = el->next;
- }
- tmp->prev = el;
- el->next = tmp;
- }
- elStart = elStart->next;
- }
- return headSource;
- }
- void printList(node *head)
- {
- node *el = head;
- while (el)
- {
- printf("%d ", el->data);
- el = el->next;
- }
- }
- int main()
- {
- int i, t;
- node *headS = NULL, *headTR = NULL, *headR = NULL, *tailS = NULL, *tailTR = NULL, *tailR = NULL;
- printf("input L1: ");
- while (1)
- {
- scanf("%d", &t);
- if (t == 0) break;
- headS = addNode(headS, &tailS, t);
- }
- printf("input L2: ");
- while (1)
- {
- scanf("%d", &t);
- if (t == 0) break;
- headTR = addNode(headTR, &tailTR, t);
- }
- headS = dsort(headS, tailS);
- headS = put(headS, headTR);
- printList(headS);
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment