Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct Node {
- int x;
- struct Node *nxt;
- } Node;
- Node *mkNode(int x, Node *nxt) {
- Node *node = malloc(sizeof(Node));
- if (node == NULL) {
- perror("");
- exit(-1);
- }
- node->x = x;
- node->nxt = nxt;
- return node;
- }
- Node *addEnd(Node *node, int x) {
- if (!node) {
- return mkNode(x, NULL);
- }
- Node *p;
- for (p = node; p->nxt; p = p->nxt);
- p->nxt = mkNode(x, NULL);
- return node;
- }
- void printList(Node *node) {
- for (; node; node = node->nxt) {
- printf("%d ", node->x);
- }
- puts("");
- }//*/
- void freeList(Node *node) {
- Node *curr = node, *nxt;
- while (curr) {
- nxt = curr->nxt;
- free(curr);
- curr = nxt;
- }
- }
- Node *readList() {
- int n, x;
- scanf("%d", &n);
- Node *node = NULL;
- for (int i = 0; i < n; i++) {
- scanf("%d", &x);
- node = addEnd(node, x);
- }
- return node;
- }
- Node *intercl(Node *node1, Node *node2) {
- Node *res = NULL;
- while (node1 && node2) {
- if (node1->x < node2->x) {
- res = addEnd(res, node1->x);
- node1 = node1->nxt;
- } else {
- res = addEnd(res, node2->x);
- node2 = node2->nxt;
- }
- }
- while (node1) {
- res = addEnd(res, node1->x);
- node1 = node1->nxt;
- }
- while (node2) {
- res = addEnd(res, node2->x);
- node2 = node2->nxt;
- }
- return res;
- }
- int main(void) {
- Node *node1 = readList();
- Node *node2 = readList();
- Node *res = intercl(node1, node2);
- printList(res);
- freeList(res);
- freeList(node1);
- freeList(node2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement