Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- /* Struktura za spisuka */
- struct List {
- int key;
- struct List* next;
- };
- /*
- Vzema purviqt element ot purviqt spisuk i go slaga kato
- kato purvi element na vtoriqt spisuk
- */
- void move_element(struct List**, struct List**);
- /*
- Vzema dva spisuka, sortirani vuv vuzhodqsht red i obedinqva
- elementite im v edin nov spisuk
- */
- struct List* combine_lists(struct List* list1, struct List* list2)
- {
- /* Vremenen element da sudurja rezultata */
- struct List temp;
- /* sochi kum kraq na spisuka */
- struct List* end = &temp;
- /* taka che sledvashtiqt element na krainiqt da e
- mqstoto, kudeto da se dobavqt novi elementi */
- temp.next = nullptr;
- while (1)
- {
- if (list1 == nullptr)
- {
- /* Ako edin ot spisucite svurshi(toest nqma
- elementi veche), izpolzvame drugiqt spisuk
- */
- end->next = list2;
- break;
- }
- else if (list2 == nullptr)
- {
- end->next = list1;
- break;
- }
- if (list1->key <= list2->key)
- move_element(&(end->next), &list1);
- else
- move_element(&(end->next), &list1);
- end = end->next;
- }
- return(temp.next);
- }
- /* move_element()
- funkciqta vzema element ot nachaloto na ediniqt spisuk i
- go premestva v nachaloto na drugiqt spisuk. Greshka e da
- se izvika funkciqta pri prazen spisuk
- Predi izvikvaneto na move_element():
- list1 == {1, 2, 3}
- list2 == {1, 2, 3}
- Sled izvikvaneto na move_element():
- list1 == {2, 3}
- list2 == {1, 1, 2, 3} */
- void move_element(struct List** list1, struct List** list2)
- {
- /*
- ukazatel kum nachaloto na vtoriqt spisuk
- kum koito shte se dobavqt elementite v nachaloto
- */
- struct List* new_list = *list2;
- /* Advance the source pointer */
- *list2 = new_list->next;
- /* Link the old dest off the new node */
- new_list->next = *list1;
- /* Move dest to point to the new node */
- *list1 = new_list;
- }
- /* Funkciq za vuvejdane v nachaloto na spisuka */
- void push(struct List** begin, int number) {
- /* Zadelqne na pamet za nov vremenen spisuk*/
- struct List* elem = (struct List*) malloc(sizeof(struct List));
- /* Vuvejdame chisloto v kletkata */
- elem->key = number;
- /* Pravim sledvashtiqt ukazatel da sochi kum nachaloto na spisuka
- za da se dobavqt novite elementi tam
- */
- elem->next = (*begin);
- /* Zadavame na ukazatelqt, koito sochi nachaloto na
- spisuka kum noviqt element, za da moje da se
- dobavqt novi elementi
- */
- (*begin) = elem;
- }
- /* Function to print nodes in a given linked list */
- void print_list(struct List *list) {
- while (list != nullptr) {
- cout << list->key << " ";
- list = list->next;
- }
- }
- /* Glavna funkciq*/
- int main()
- {
- /* Zanulqvane na spisucite */
- struct List* a = nullptr;
- struct List* b = nullptr;
- struct List* c = nullptr;
- /* Suzdavame dva spisuka
- a: 1->10->7, b: 18->4->13 */
- push(&a, 1);
- push(&a, 10);
- push(&a, 7);
- push(&b, 18);
- push(&b, 4);
- push(&b, 13);
- /* Izvejdame vseki spisuk po otdelno */
- cout << "List A: \n";
- print_list(a);
- cout << "\n\nList B: \n";
- print_list(b);
- /* Kombinirame dvata spisuka v treti spisuk s ime c */
- c = combine_lists(a, b);
- /* Izvejdame kombiniraniqt spisuk */
- cout << "\n\nList C: \n";
- print_list(c);
- cout << "\n\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement