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;
- } node;
- node *addNode(node *head, int num)
- {
- node * el = head;
- node * n = (node*) malloc(sizeof(node));
- n->next = NULL;
- n->data = num;
- if (!head)
- {
- return n;
- }
- while (el->next)
- el = el->next;
- el->next = n;
- return head;
- }
- node *replace(node *headSource, node *headToRep, node *headRep)
- {
- node *elStart = headSource, *elEnd = headSource, *elStartPrev = headSource;
- while (elStart)
- {
- node *elf = headToRep;
- if (elStart->data == elf->data)
- {
- elEnd = elStart;
- while (elf && elEnd && elEnd->data == elf->data)
- {
- elEnd = elEnd->next;
- elf = elf->next;
- }
- if (!elf)
- {
- node *el = headRep;
- elStartPrev->next = headRep;
- while (el->next)
- el = el->next;
- el->next = elEnd;
- }
- }
- elStartPrev = elStart;
- 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, *in = NULL;
- printf("input L1: ");
- while (1)
- {
- scanf("%d", &t);
- if (t == 0) break;
- headS = addNode(headS, t);
- }
- printf("input L2: ");
- while (1)
- {
- scanf("%d", &t);
- if (t == 0) break;
- headTR = addNode(headTR, t);
- }
- printf("input L3: ");
- while (1)
- {
- scanf("%d", &t);
- if (t == 0) break;
- headR = addNode(headR, t);
- }
- replace(headS, headTR, headR);
- printList(headS);
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment