Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored the code into multiple files and added comments #6

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
# CC=gcc
# CFLAGS=-Wall
# EXECUTABLE=list
# list: linkedlist.o main.c
# $(CC) $(CFLAGS) linkedlist.o main.c -o $(EXECUTABLE)
# linkedlist.o: linkedlist.h
# clean:
# rm -rf *.o *.gch $(EXECUTABLE)
# leaks:
# valgrind --leak-check=yes ./$(EXECUTABLE)

CC=gcc
CFLAGS=-Wall
EXECUTABLE=list
list: linkedlist.o main.c
$(CC) $(CFLAGS) linkedlist.o main.c -o $(EXECUTABLE)
linkedlist.o: linkedlist.h

%.o: %.c
$(CC) $(CFLAGS) -c $<

list: linkedlist.o main.o
$(CC) $(CFLAGS) linkedlist.o main.o -o $@

# linkedlist.o: linkedlist.h

clean:
rm -rf *.o *.gch $(EXECUTABLE)
rm -rf *.o $(EXECUTABLE)
leaks:
valgrind --leak-check=yes ./$(EXECUTABLE)
valgrind --leak-check=yes ./$(EXECUTABLE)
123 changes: 85 additions & 38 deletions linkedlist.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h"

struct node {
int data;
struct node * next;
};
#ifndef LINKED_STRUCT_HEADER
#define LINKED_STRUCT_HEADER
#include "linkedlist_struct.h"
#endif

struct list {
Node * head;
};
#ifndef LINKED_HEADER
#define LINKED_HEADER
#include "linkedlist.h"
#endif

Node * createnode(int data);

Node * createnode(int data){
Node * newNode = malloc(sizeof(Node));
Expand All @@ -32,22 +31,32 @@ List * makelist(){
return list;
}

/**
* This function displays the elements of the linked list in order from head to tail.
* Example: If a linked list contains the elements 1,2,3 in this order with 1 at the head then
* the display function will display the linked list as: 1 -> 2 -> 3 ->
*
* The -> after 3 points to nothing to indicate that it is pointing to NULL
*
* The function does not print anything in case the linked list is empty.
* */
void display(List * list) {
Node * current = list->head;
if(list->head == NULL)
return;

for(; current != NULL; current = current->next) {
printf("%d\n", current->data);
for(Node * current = list->head; current != NULL; current = current->next) {
printf("%d -> ", current->data);
}
}


/**
* This functions adds an element at the end of the linked list.
* */
void add(int data, List * list){
Node * current = NULL;
// If the linked list is empty
if(list->head == NULL){
list->head = createnode(data);
}
else {
else { // If there are elements present in the linked list
Node * current = NULL;
current = list->head;
while (current->next!=NULL){
current = current->next;
Expand All @@ -56,42 +65,80 @@ void add(int data, List * list){
}
}


// void delete(int data, List * list){
// Node * current = list->head;
// Node * previous = current;
// while(current != NULL){
// if(current->data == data){
// previous->next = current->next;
// if(current == list->head)
// list->head = current->next;
// free(current);
// return;
// }
// previous = current;
// current = current->next;
// }
// }


/**
* This function deletes the first occurrence of the node containing the data value in it.
* NOTE: This function only uses a single pointer for the deletion.
* */
void delete(int data, List * list){
Node * current = list->head;
Node * previous = current;
while(current != NULL){
if(current->data == data){
previous->next = current->next;
if(current == list->head)
list->head = current->next;
free(current);
return;
}
previous = current;
current = current->next;
}
}
Node * finder = list->head; // Finder will be used to find the first occurrence of the node containing the value data

while(finder != NULL){
if(finder->data == data){
Node * temp = finder->next; // temp points to the next node

if(temp != NULL){ // If the node to delete is the first node or somewhere in-between in the linked list
finder->data = temp->data; // Copying the data from the next node to the current node
finder->next = temp->next; // Setting the next of the current node to the next value of the next node
free(temp);
temp = NULL; // To avoid dangling pointers
} else { // If the node to delete is the last node in the linked list
free(finder);
finder = NULL; // To avoid dangling pointers
}
}
finder = finder->next;
}
}


/**
* This function reverses the given linked list.
* The function does not do anything if the linked list passed in as the argument is empty.
* */
void reverse(List * list){
Node * reversed = NULL;
Node * previous = NULL;
Node * current = list->head;
Node * temp = NULL;
while(current != NULL){
temp = current;
current = current->next;
temp->next = reversed;
reversed = temp;
temp = current->next;
current->next = previous;
previous = current;
current = temp;
}
list->head = reversed;
list->head = previous;
}


/**
* This function destroys the linked list passed in as the argument.
* */
void destroy(List * list){
Node * current = list->head;
Node * next = current;
Node * next = NULL;

while(current != NULL){
next = current->next;
free(current);
current = next;
}
free(list);
list = NULL; // To avoid dangling pointers
}
8 changes: 5 additions & 3 deletions linkedlist.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#ifndef LINKEDLIST_HEADER
#define LINKEDLIST_HEADER

typedef struct node Node;

typedef struct list List;
#ifndef LINKED_STRUCT_HEADER
#define LINKED_STRUCT_HEADER
#include "linkedlist_struct.h"
#endif

List * makelist();
Node * createnode(int data);
void add(int data, List * list);
void delete(int data, List * list);
void display(List * list);
Expand Down
8 changes: 8 additions & 0 deletions linkedlist_struct.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
typedef struct node {
int data;
struct node * next;
} Node;

typedef struct list {
Node * head;
} List;
7 changes: 6 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h"
#include <stddef.h>

#ifndef LINKED_HEADER
#define LINKED_HEADER
#include "linkedlist.h"
#endif

int main(void){
List * list = makelist();
Expand Down