Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <xlocale.h>
- #include "linkedlist.h"
- sList* create_sList(void) {
- sList *lisy = malloc(sizeof (sList));
- lisy->count = 0;
- lisy->first = NULL;
- lisy->last = NULL;
- return lisy;
- }
- int insert_element_s(sList *L, snodeType *p, int value) {
- snodeType *head = malloc(sizeof (snodeType));
- if (p == NULL) {
- snodeType *tep = L->first;
- L->first = head;
- head->next = tep;
- head->value = value;
- L->count++;
- if (L->count == 1) {
- L->last = head;
- L->last->next = NULL;
- }
- } else {
- snodeType *t = malloc(sizeof (snodeType));
- t = L->first;
- while (t != p) {
- t = t->next;
- }
- head->value = value;
- head->next = t->next;
- t->next = head;
- L->count++;
- if (head->next == NULL) {
- L->last = head;
- L->last->next = NULL;
- }
- }
- return 0;
- }
- int delete_element_s(sList *L, snodeType *p) {
- int i = 1;
- snodeType *t = malloc(sizeof (snodeType));
- if (L != NULL) {
- if (p == NULL) {
- t = L->first;
- if (t != NULL) {
- L->first = t->next;
- L->count--;
- if (t->next == NULL) {
- L->last = NULL;
- }
- i = 0;
- }
- } else {
- t = p;
- if (t->next != NULL) {
- t = t->next;
- p->next = t->next;
- L->count--;
- if (t->next == NULL) {
- L->last = p;
- }
- i = 0;
- }
- }
- }
- return i;
- }
- sList* merge_lists(sList *L1, sList *L2) {
- sList *head = create_sList();
- if (L1->count == 0 && L2->count == 0) {
- return head;
- }
- snodeType *t1 = malloc(sizeof (snodeType));
- snodeType *t2 = malloc(sizeof (snodeType));
- snodeType *last = malloc(sizeof (snodeType));
- if (L1->count == 0) {
- insert_element_s(head, NULL, L2->first->value);
- t1 = NULL;
- if (L2->count == 1) {
- return head;
- } else {
- t2 = L2->first->next;
- }
- last = L2->first;
- } else if (L2->count == 0) {
- insert_element_s(head, NULL, L1->first->value);
- if (L1->count == 1) {
- return head;
- } else {
- t1 = L1->first->next;
- }
- t2 = NULL;
- last = L1->first;
- } else if (L1->first->value < L2->first->value) {
- insert_element_s(head, NULL, L1->first->value);
- t1 = L1->first->next;
- t2 = L2->first;
- last = L1->first;
- } else if (L1->first->value > L2->first->value) {
- insert_element_s(head, NULL, L2->first->value);
- t1 = L1->first;
- t2 = L2->first->next;
- last = L2->first;
- } else if (L1->first->value == L2->first->value) {
- insert_element_s(head, NULL, L2->first->value);
- t1 = L1->first->next;
- t2 = L2->first->next;
- last = L2->first;
- }
- while (t1 != NULL || t2 != NULL) {
- if (t2 == NULL) {
- insert_element_s(head, last, t1->value);
- last = t1;
- t1 = t1->next;
- } else if (t1 == NULL) {
- insert_element_s(head, last, t2->value);
- last = t2;
- t2 = t2->next;
- } else if (t1->value < t2->value) {
- insert_element_s(head, last, t1->value);
- last = t1;
- t1 = t1->next;
- } else if (t1->value > t2->value) {
- insert_element_s(head, last, t2->value);
- last = t2;
- t2 = t2->next;
- } else if (t1->value == t2->value) {
- insert_element_s(head, last, t2->value);
- last = t2;
- t2 = t2->next;
- t1 = t1->next;
- }
- }
- return head;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement