Skip to content

Commit f1c886c

Browse files
committed
Initial Commit
0 parents commit f1c886c

File tree

7 files changed

+298
-0
lines changed

7 files changed

+298
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# CThreads
2+
Resources for POSIX Threads of Unix :
3+
<br/>
4+
https://www.ibm.com/docs/en/i/7.1?topic=ssw_ibm_i_71/apis/concept8.htm
5+
<br/>
6+
https://www.geeksforgeeks.org/thread-functions-in-c-c/

build/compile.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#**
2+
#* Ccoffee Build tool, manual build, alpha-v1.
3+
#* Custom Includsions for GTKmm cpp wrapper
4+
#* dependencies '-I"/usr/include/glibmm-2.9.1/glib" -I"/usr/include/sigc++-2.0/sigc++" -I"/usr/include/giomm-2.4" -I"/usr/include/gtkmm-4.2.0/gtk"'
5+
#*
6+
#* @author pavl_g.
7+
#*#
8+
echo "Compiling the project"
9+
10+
#1) define work directory
11+
workDir='/home/twisted/GradleProjects/DoubleLinkedList'
12+
13+
#1) recreate a clean one to gather cpp files for compiling & linking
14+
mkdir ${workDir}'/build/.build'
15+
16+
##attrs : dir to compile & sharedLib name
17+
libs=(${workDir}'/src/libs/*')
18+
main=(${workDir}'/src/main/*')
19+
20+
merge[0]=${libs}
21+
merge[1]=${main}
22+
23+
#2) copy cpp files to a gather directory
24+
for ((idx=0; idx < ${#merge[@]}; idx++)); do
25+
##act on ${merge[$idx]}
26+
cp ${merge[$idx]} ${workDir}'/build/.build'
27+
done
28+
29+
# 3) get the final String
30+
final=${workDir}'/build/.build/*'
31+
32+
# 4) execute using g++ compiler against cpp code
33+
##prepare exec names
34+
sharedlib='DDLL'
35+
executable='DDLL.exec'
36+
# 5) compile files with inclusions
37+
g++ -x c++ -I${workDir}'/src/includes' -I'/usr/include' -o ${sharedlib} ${final}
38+
clang++ -x c++ -I${workDir}'/src/includes' -I'/usr/include' -o ${executable} ${final}
39+
# 6) move files to output dir
40+
##prepare dirs
41+
builddir=${workDir}'/build/'
42+
outputdir=${workDir}'/output/'
43+
##moving files
44+
mv ${builddir}${sharedlib} ${outputdir}${sharedlib}
45+
mv ${builddir}${executable} ${outputdir}${executable}
46+
47+
#7) delete old build
48+
oldbuild=(${workDir}'/build/.build/*')
49+
for ((idx=0; idx < ${#oldbuild[@]}; idx++)); do
50+
rm ${oldbuild[$idx]}
51+
done
52+
53+
##remove the dir
54+
rmdir ${workDir}'/build/.build/'
55+
56+
echo "Successfully Compiled"
57+
58+

output/DDLL

17.5 KB
Binary file not shown.

output/DDLL.exec

17.4 KB
Binary file not shown.

src/includes/DoubleLinkedList.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#ifndef DDDL
5+
#define DDDL 555
6+
typedef struct Node Node;
7+
struct Node
8+
{
9+
//mark the node with an index to keep track of it
10+
int index;
11+
int Data;
12+
Node* Next;
13+
Node* Prev;
14+
};
15+
16+
extern "C" void Add(int);
17+
extern "C" void Display();
18+
extern "C" Node* Search(int);
19+
extern "C" void updateIndices();
20+
extern "C" void Delete(int);
21+
extern "C" void InsertAfter(int, int);
22+
extern "C" int GetItemsCount();
23+
extern "C" Node* GetByIndex(int);
24+
extern "C" Node* copyVals(Node*);
25+
extern "C" void Reverse();
26+
27+
#endif

src/libs/DoubleLinkedList.cpp

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <DoubleLinkedList.h>
4+
5+
//Define the head, tail & length of the linkedlist
6+
int length = 0;
7+
Node *head = NULL, *tail = NULL;
8+
9+
void Add(int data)
10+
{
11+
Node *newNode = (Node *) malloc(sizeof(Node));
12+
newNode->Data = data;
13+
newNode->Prev = newNode->Next = NULL;
14+
15+
if(head == NULL) // linked list is empty
16+
{ //first time call...
17+
head = tail = newNode;
18+
}
19+
else
20+
{
21+
//add a new item after the old tail
22+
tail->Next = newNode;
23+
//assign the tail to the Prev pointer of the newNode Pointer
24+
newNode->Prev = tail;
25+
//overwrite the old tail with the new one
26+
tail = newNode;
27+
}
28+
//assign the index to length-1
29+
//length++ is a postfix incremental operation ==> increases the length after this line
30+
newNode->index = length++;
31+
}
32+
33+
void Display()
34+
{
35+
Node *current = head;
36+
37+
while(current != NULL)
38+
{
39+
printf("%d \t", current->Data);
40+
current = current->Next;
41+
}
42+
printf("\n");
43+
}
44+
45+
Node* Search(int data)
46+
{
47+
Node *current = head;
48+
49+
while(current != NULL)
50+
{
51+
if(current->Data == data)
52+
{
53+
return current;
54+
}
55+
56+
current = current->Next;
57+
}
58+
59+
return NULL;
60+
}
61+
/**
62+
* swap the Next with the current Node index
63+
* starting from the start index.
64+
* @param startIndex value reference to the start index where the iterator would apply the indices swapping operation.
65+
*/
66+
void updateIndices(){
67+
int index = 0;
68+
Node* iterator = head;
69+
while(iterator != NULL && index < length){
70+
iterator->index = index++;
71+
iterator = iterator->Next;
72+
}
73+
}
74+
void Delete(int data)
75+
{
76+
//deep data copy
77+
Node *pDelete = Search(data);
78+
79+
if(pDelete == NULL)
80+
return ;
81+
82+
83+
if(pDelete == head)
84+
{
85+
if(pDelete == tail)
86+
{
87+
head = tail = NULL;
88+
}
89+
else
90+
{
91+
head = pDelete->Next;
92+
head->Prev = NULL;
93+
}
94+
}
95+
else if(pDelete == tail)
96+
{
97+
tail = pDelete->Prev;
98+
tail->Next = NULL;
99+
}
100+
else
101+
{
102+
//assign the next of the previousNode to the next of the pDelete Node, dissolving the pDelete Node from the head
103+
Node* previousNode = pDelete->Prev;
104+
previousNode->Next = pDelete->Next;
105+
106+
//assign the preva of the nextNode to the preva of the pDelete node, dissolving the pDelete Node from the tail
107+
Node* nextNode = pDelete->Next;
108+
nextNode->Prev = pDelete->Prev;
109+
}
110+
//remove one item from the length
111+
--length;
112+
//update the indices in accordance to the new length
113+
updateIndices();
114+
free(pDelete);
115+
pDelete = NULL;
116+
117+
}
118+
119+
/**
120+
* inserts data in the node after this one.
121+
* @param afterData the data of the node that we want to add after it.
122+
* @param data the proposed data to fill inside the wanted node.
123+
*/
124+
void InsertAfter(int afterData, int data){
125+
//get the node to add after it
126+
Node* pNode = Search(afterData);
127+
//create a new node & fill its data
128+
Node* newNode = new Node();
129+
newNode->Data = data;
130+
newNode->Prev = pNode;
131+
newNode->Next = pNode->Next;
132+
133+
//assign the next Node of the pNode to the newNode values
134+
pNode->Next = newNode;
135+
//assign the previous Node of the next node of the newNode with the values of the newNode
136+
newNode->Next->Prev = newNode;
137+
//advance the length by 1 new Node
138+
++length;
139+
//update the indices in accordance to the new length
140+
updateIndices();
141+
}
142+
int GetItemsCount(){
143+
return length;
144+
}
145+
/**
146+
* gets the node by an index
147+
* @param index.
148+
*/
149+
Node* GetByIndex(int index){
150+
if(index >= length){
151+
perror("Specified Node with this index, doesn't exist !, ArrayIndexOutOfBoundsException");
152+
return new Node();
153+
}
154+
//mark the head node as the start --Shallow Copy of Data
155+
Node* node = head;
156+
//loop over the list as long as the proceeding node of this node is available
157+
while(node != NULL)
158+
{
159+
//check if indices match
160+
if(node->index == index)
161+
{
162+
return node;
163+
}
164+
//shallow copy the next node to this pointer
165+
node = node->Next;
166+
}
167+
168+
return NULL;
169+
}
170+
171+
172+

src/main/main.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Double Linked Lists.
3+
* 2021-2022
4+
* @author pavl_g.
5+
*/
6+
#include <iostream>
7+
#include <pthread.h>
8+
#include <unistd.h>
9+
#include <DoubleLinkedList.h>
10+
using namespace std;
11+
12+
/**
13+
* Run main Thread
14+
* @return the status of the run
15+
*/
16+
int main () {
17+
//test add
18+
Add(3);
19+
Add(4);
20+
Add(6);
21+
//test display
22+
Display();
23+
//test delete
24+
Delete(4);
25+
Display();
26+
//test insertAfter
27+
InsertAfter(3, 2);
28+
InsertAfter(2, 1);
29+
Display();
30+
//test getByIndex
31+
std::cout << GetByIndex(2)->Data << std::endl;
32+
//test reverse --WIP
33+
34+
return 0;
35+
}

0 commit comments

Comments
 (0)