-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.cpp
91 lines (85 loc) · 2.22 KB
/
linkedlist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
using namespace std;
//g++ -o ll -Wall -ansi -pedantic -O0 -g linkedlist.cpp
typedef struct _node { //NODE
char data;
struct _node *prox;
}node;
typedef struct _lista { //LIST
node *begin = NULL;
node *end = NULL;
}list;
//INSERT BACK
void insert(list *List,char data){
//INSERT FIRST ELEMENT
if(List->begin == NULL){
List->begin = new node;
List->end = List->begin;
List->begin->data = data;
List->begin->prox = NULL;
cout << "Inseriu o primeiro!"<<endl;
}
else{
//INSERT REMAINING ELEMENTS
List->end->prox = new node;
List->end = List->end->prox;
List->end->data = data;
List->end->prox = NULL;
cout << "Inseriu!"<<endl;
}
}
//REMOVE FRONT
void remove(list *List){
if(List->begin){
node *tmp = List->begin->prox;
delete List->begin;
List->begin = tmp;
cout << "Removeu!"<<endl;
if(!List->begin){List->begin = NULL;}
}
}
//SHOW ELEMENTS OF LIST
void showElements(list *List){
list *temp = List;
while(temp->begin != NULL){
cout << temp->begin->data <<endl;
temp->begin = temp->begin->prox;
}
}
//MERGE TWO LISTS BY THE BEGIN(HEADS)
_node* mergeLists(_node *headA, _node* headB)
{
_node *result = NULL;
_node **ptr = &result;
while( headA || headB ) {
_node *temp = new _node;
temp->prox = NULL;
*ptr = temp;
ptr = &temp->prox;
if( headB == NULL || ( headA && headA->data < headB->data ) ) {
temp->data = headA->data;
headA = headA->prox;
} else {
temp->data = headB->data;
headB = headB->prox;
}
}
return result;
}
int main (){
list *minhaLista = new list; //CREATE A NEW LIST
list *segundaLista = new list;
list *listaFinal = new list;
insert(minhaLista,'A');
insert(minhaLista,'B');
//remove(minhaLista);
//remove(minhaLista);
insert(segundaLista,'C');
insert(segundaLista,'D');
//insert(minhaLista,'E');
//remove(minhaLista);
//insert(minhaLista,'F');
listaFinal->begin = mergeLists(minhaLista->begin,segundaLista->begin);
showElements(listaFinal);
return 0;
}