aboutsummaryrefslogtreecommitdiffstats
path: root/Algorithms/LinkedLists.cpp
blob: 360714e5754a43d6de4a1022f7967a1c43dd17cd (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* 
* @Author: yannherklotz
* @Date:   2016-02-08 22:20:52
* @Last Modified by:   yannherklotz
* @Last Modified time: 2016-02-09 12:43:15
*/

#include <iostream>

using namespace std;

typedef int list_t;

struct linkNode {
	list_t val;
	linkNode* next;
};

void addel(list_t, linkNode*&);
void deallocate(linkNode*&);
void printListOpposite(linkNode*);
void printListCorrect(linkNode*);
int listLength(linkNode*);
int deleteItem(linkNode*&, list_t);
linkNode* concatenateList(linkNode*, linkNode*);

int main(){
	linkNode* listA = NULL;
	linkNode* listB = NULL;
	linkNode* listC = NULL;

	int n;
	list_t el;

	cout << "How many elements: ";
	cin >> n;

	for(int i = 0; i < n; ++i) {
		cout << "Enter element: ";
		cin >> el;

		addel(el, listA);
	}
	printListOpposite(listA);
	cout << endl;
	printListCorrect(listA);
	cout << "List Count: " << listLength(listA) << endl;
	cout << "which element to remove? ";
	cin >> n;
	deleteItem(listA, n);
	printListCorrect(listA);

	cout << "How many elements in list B: ";
	cin >> n;

	for(int i = 0; i < n; ++i) {
		cout << "Enter element: ";
		cin >> el;

		addel(el, listB);
	}

	printListCorrect(listB);
	listC = concatenateList(listA, listB);
	cout << "Concatenated list:" << endl;
	printListCorrect(listC);
	cout << "delete from a: ";
	cin >> n;
	deleteItem(listA, n);
	cout << "delete from b: ";
	cin >> n;
	deleteItem(listB, n);
	cout << endl << "A" << endl;
	printListCorrect(listA);
	cout << "B" << endl;
	printListCorrect(listB);
	cout << "A+B" << endl;
	printListCorrect(listC);

	deallocate(listA);
	deallocate(listB);
	deallocate(listC);
    return 0;
}

void addel(list_t el, linkNode* &ln) {
	linkNode* tmp = new linkNode;
	tmp->val = el;
	tmp->next = ln;
	ln = tmp;
}

void deallocate(linkNode* &ln) {
	while(ln != NULL) {
		linkNode* nextln = ln->next;
		delete ln;
		ln = nextln;
	}
}

void printListOpposite(linkNode* ln) {
	while(ln != NULL) {
		cout << ln->val << endl;
		ln = ln->next;
	}
}

void printListCorrect(linkNode* ln) {
	if(ln != NULL) {
		printListCorrect(ln->next);
		cout << ln->val << endl;
	}
}

int listLength(linkNode* ln) {
	int count = 0;
	while(ln != NULL) {
		++count;
		ln = ln->next;
	}
	return count;
}

int findItem(linkNode* ln, list_t el) {
	int count = 0;
	while(ln != NULL) {
		if(ln->val == el) {
			return count;
		}
		++count;
	}
	return(-1);
}

int deleteItem(linkNode* &ln, list_t el) {
	linkNode* sp = ln;
	linkNode* fp = ln->next;

	if(sp->val == el) {
		ln = ln->next;
		delete sp;
	} else {
		while(fp != NULL) {
			if(fp->val == el) {
				sp->next = fp->next;
				delete fp;
				return 1;
			}
			fp = fp->next;
			sp = sp->next;
		}
	}
	return 0;
}

linkNode* concatenateList(linkNode* fl, linkNode* sl) {
	linkNode* tmpsl = NULL;
	linkNode* tmpfl = NULL;

	while(fl != NULL) {
		addel(fl->val, tmpfl);
		fl = fl->next;
	}
	while(sl != NULL) {
		addel(sl->val, tmpsl);
		sl = sl->next;
	}

	linkNode* tmp = tmpsl;
	while(tmp->next != NULL) {
		tmp = tmp->next;
	}
	tmp->next = tmpfl;
	return tmpsl;
}

void reverseOrder(listNode* &ln) {
	linkNode* sp = ln;
	linkNode* tmp = ln;
	ln = ln->next;
	tmp->next = NULL;
	while(ln->next != NULL) {
		tmp = ln->next;
		ln->next = sp;
		sp = ln;
		ln = tmp;
	}
}