Skip to content

Commit 8b04823

Browse files
Update README.md
Initial README.md file update.
1 parent 40259a4 commit 8b04823

File tree

1 file changed

+281
-2
lines changed

1 file changed

+281
-2
lines changed

README.md

Lines changed: 281 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,281 @@
1-
# singly_linked_list
2-
Definition of a singly linked list in the C programming language, along with some of the more common operations.
1+
2+
3+
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/TiagoRodrigues1111/singly_linked_list?color=orange)](https://github.com/TiagoRodrigues1111/singly_linked_list/releases)
4+
[![Unlicense License](https://img.shields.io/badge/license-Unlicense-blue.svg)](LICENSE)
5+
6+
7+
# Singly Linked List Implementation in C
8+
9+
This repository contains a simple implementation of a singly linked list data structure in the C programming language. A singly linked list is a linear data structure where each element is a separate object, known as a node. Each node contains two items: the data and a reference (or link) to the next node in the sequence. This implementation supports common singly linked list operations such as add to head/tail, remove from head/tail,and get value of a node. It also has two functions for getting the next node. Additionally, it also allows for nodes to be added/removed at a specific position n, and to get the nth value from a node.
10+
11+
## Features
12+
13+
- Singly Linked List implementation.
14+
- Basic operations:
15+
- `Create`: Creates a node of the singly linked list datastructure. - `create_node`
16+
- `Add value`: Gives a value to a node. - `give_node_value`
17+
- `Add at head`: Adds a node at the head of a linked list. - `add_node_to_head`
18+
- `Add at tail`: Adds a node at the tail of a linked list. - `add_node_to_tail`
19+
- `Add at n`: Adds a node at index n of a linked list. - `add_node_in_index_n`
20+
- `remove head`: Removes head node of a linked list. - `remove_head_node`
21+
- `remove tail`: Removes tail node of a linked list. - `remove_tail_node `
22+
- `remove n`: Removes the nth node of a linked list. - `remove_node_in_index_n`
23+
- `next node`: gives the pointer the value of its next node. - `next_node`
24+
- `get next node`: returns the next node. - `get_next_node`
25+
- `get value`: returns value of a node. - `get_value`
26+
- `get value at n`: returns value of the nth node. - `get_value_in_index_n`
27+
- `Free`: frees a linked. - `free_linked_list`
28+
29+
30+
## Prerequisites
31+
32+
To compile and run the program, you need:
33+
- A C compiler (e.g., GCC, Clang, or MSVC).
34+
- Basic knowledge of working with C files.
35+
36+
37+
## How to Use
38+
39+
1. Clone the repository:
40+
```bash
41+
git clone https://github.com/TiagoRodrigues1111/singly_linked_list
42+
cd singly_linked_list
43+
```
44+
45+
46+
47+
2. Compile the code using one of the following options:
48+
49+
#### 2.1 Using `singly_linked_list.c` (Array-based implementation):
50+
```bash
51+
gcc singly_linked_list.c simplemain.c -o single_linked_list_test
52+
```
53+
#### 2.2 Using `MakeFile`:
54+
```bash
55+
make all/single_linked_list_test
56+
```
57+
58+
3. Run the executable:
59+
#### 3.1 Without make:
60+
```bash
61+
./single_linked_list_test
62+
```
63+
#### 3.2 With make:
64+
```bash
65+
make run
66+
```
67+
68+
69+
## Repo Structure
70+
71+
### File Structure
72+
73+
- [singlelinkedlist.c](singlelinkedlist.c): Contains the implementation of the singly linked list functions
74+
- [singlelinkedlist.h](singlelinkedlist.h): Header file with function declarations and definitions
75+
- [simplemain.c](simplemain.c): Example usage of the singly linked list
76+
- [README.md](README.md): Documentation for the project
77+
- [Makefile](Makefile): Simple Makefile
78+
79+
80+
### Folder Structure
81+
82+
```
83+
.
84+
├── singlelinkedlist.c
85+
├── singlelinkedlist.h
86+
├── simplemain.c
87+
├── README.md
88+
└── Makefile
89+
```
90+
91+
92+
- `Free`: frees a linked. - `free_linked_list`
93+
94+
## Operations Breakdown
95+
96+
### `Create`
97+
- **Description**: Creates a node
98+
- **Input**: The pointer to the memory position of the node
99+
- **Example**:
100+
```c
101+
void *head1= NULL;
102+
create_node(&head1);
103+
```
104+
105+
### `Add value`
106+
- **Description**: Gives a value to a node.
107+
- **Input**: The pointer to the node, as well as a void* to the input data, and the size of the datatype.
108+
- **Example**:
109+
```c
110+
uint8_t data=10;
111+
give_node_value(head1,(void*)&data,sizeof(uint8_t));
112+
```
113+
114+
### `Add at head`
115+
- **Description**: Adds a node at the head of a linked list
116+
- **Input**: The pointer to the head, And the node to add.
117+
- **Example**:
118+
```c
119+
120+
add_node_to_head(&head1,node1);
121+
```
122+
### `Add at tail`
123+
- **Description**: Adds a node at the tail of a linked list
124+
- **Input**: The pointer to the head, And the node to add.
125+
- **Example**:
126+
```c
127+
128+
add_node_to_tail(&head1,node1);
129+
```
130+
131+
### `Add at n`
132+
- **Description**: Adds a node at index n of a linked list
133+
- **Input**: The pointer to the head, the node to add, and the position to add.
134+
- **Example**:
135+
```c
136+
uint64_t n = 100;
137+
add_node_in_index_n(&head1,node1,n);
138+
```
139+
140+
### `Remove head`
141+
- **Description**: Remove node at the head of a linked list
142+
- **Input**: The pointer to the head
143+
- **Example**:
144+
```c
145+
146+
remove_head_node(&head1);
147+
```
148+
### `Remove tail`
149+
- **Description**: Remove node at the tail of a linked list
150+
- **Input**: The pointer to the head.
151+
- **Example**:
152+
```c
153+
154+
remove_tail_node(&head1,node1);
155+
```
156+
157+
### `Remove at n`
158+
- **Description**: Remove node at index n of a linked list
159+
- **Input**: The pointer to the head, and the position to remove.
160+
- **Example**:
161+
```c
162+
uint64_t n = 100;
163+
remove_node_in_index_n(&head1,n);
164+
```
165+
166+
### `next node`
167+
- **Description**: gives the pointer the value of its next node
168+
- **Input**: The pointer to the node.
169+
- **Example**:
170+
```c
171+
next_node(&node);
172+
173+
### `get next node`
174+
- **Description**: returns the next node
175+
- **Input**: The pointer to the node.
176+
- **Example**:
177+
```c
178+
void *nextnode = NULL;
179+
nextnode = get_next_node(node);
180+
181+
### `get value`
182+
- **Description**: returns value of a node
183+
- **Input**: The node to get value.
184+
- **Example**:
185+
```c
186+
void *value = NULL;
187+
value = get_value(node);
188+
189+
### `get value at n`
190+
- **Description**: returns value of the nth node
191+
- **Input**: The head node of the list, and the position of the node.
192+
- **Example**:
193+
```c
194+
void *value = NULL;
195+
uint64_t n = 100;
196+
value = get_value_in_index_n(node,n);
197+
198+
199+
### `Free`
200+
- **Description**: Frees the linked list.
201+
- **Output**: None.
202+
- **Example**:
203+
```c
204+
free_linked_list(&head1);
205+
```
206+
207+
208+
209+
## Example Usage
210+
211+
Here is a simple example of how the linked list can be used (tutorial() in [simplemain.c](simplemain.c)):
212+
213+
```c
214+
#include <stdio.h>
215+
#include "singlelinkedlist.h"
216+
217+
int main()
218+
{
219+
void *head1 = NULL;
220+
void *node1 = NULL;
221+
uint16_t data1 = 0;
222+
void *value_aux = NULL;
223+
224+
225+
create_node(&head1); // create a node
226+
227+
data1 = 3;
228+
give_node_value(head1,(void*) &data1,sizeof(uint16_t)); // give a value to a node
229+
230+
231+
create_node(&node1);
232+
data1 = 20;
233+
give_node_value(node1,(void*) &data1,sizeof(uint16_t));
234+
add_node_to_head(&head1,node1); // add new node to the head
235+
236+
create_node(&node1);
237+
data1 = 30;
238+
give_node_value(node1,(void*) &data1,sizeof(uint16_t));
239+
add_node_to_tail(&head1,node1); // add node to tail of head
240+
241+
create_node(&node1);
242+
data1 = 40;
243+
give_node_value(node1,(void*) &data1,sizeof(uint16_t));
244+
add_node_in_index_n(&head1,node1, 1); // add node at index 1 of list
245+
246+
247+
value_aux = get_value(head1); // get value at the head of list
248+
if(NULL != value_aux)
249+
{
250+
printf("%u\n",*((uint16_t*)value_aux));
251+
}
252+
253+
value_aux = get_value_in_index_n(head1, 1); // get value at index 1 of list
254+
if(NULL != value_aux)
255+
{
256+
printf("%u\n",*((uint16_t*)value_aux));
257+
}
258+
259+
260+
remove_head_node(&head1); // remove head node
261+
262+
remove_tail_node(&head1); // remove tail node
263+
264+
remove_node_in_index_n(&head1,1); // remove node at index 1 of list
265+
266+
267+
free_linked_list(&head1); // free linked list
268+
269+
return 0;
270+
}
271+
```
272+
## Notes
273+
274+
275+
## Contributions
276+
277+
Contributions are welcome! If you find any issues or have ideas for improvement, feel free to open an issue or submit a pull request.
278+
279+
## License
280+
281+
This project is licensed under the Unlicense license. See the `LICENSE` file for more details.

0 commit comments

Comments
 (0)