Skip to content

Commit

Permalink
Added List with Linked List Dummy Node implementation (TAD)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcross23 committed Oct 23, 2020
1 parent 18f0ff1 commit deb20c5
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 0 deletions.
172 changes: 172 additions & 0 deletions TADS/Lists/LinkedList Imp/Dummy Node Imp/LLDList.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "LLDList.h"

int createEmptyList(LLDList *list){
if (list == NULL) return -1;

//Create a dummy node
if (NULL == (list->first = list->last = (listNode *)malloc(sizeof(listNode)))) return -2;

list->first->next = NULL;
return 0;

}

int isEmpty(LLDList *list){
if (list == NULL || list->first == NULL) return -1;

return (list->first->next == NULL);
}

listPosition first(LLDList *list){
if(list==NULL || list->first==NULL) return NULL;

return list->first;
}

listPosition next(listPosition p, LLDList *list){
if(list==NULL || list->first==NULL || p==NULL) return NULL;

return p->next;
}


listPosition prev(listPosition p, LLDList *list){
if(list==NULL || list->first==NULL || p==NULL) return NULL;

if(p == list->first)
return list->first;
else{
listPosition previous = list->first;
while(previous->next != p){
previous = previous->next;
}
return previous;
}
}

listPosition end(LLDList *list){
if(list==NULL || list->first==NULL) return NULL;

return list->last;
}

int listAdd(listElement x, listPosition p, LLDList *list){
if(p==NULL || list==NULL || list->first==NULL || list->last==NULL) return -1;

listNode *new;;
if(NULL == (new = malloc(sizeof(listNode)))) return -2;

new->element = x;
new->next = p->next;
p->next = new;

if(p == end(list)) list->last = new;
return 0;
}


int listRemove(listPosition p, LLDList *list){
if(p==NULL || list==NULL || list->first==NULL || list->last==NULL) return -1;

listPosition toRemove;

if(p == end(list)){
list->last = prev(p,list);
list->last->next = NULL;
toRemove = p;
}else{
toRemove = p->next;
p->next = toRemove->next;
}

free(toRemove);
return 0;

}


listPosition find(listElement x, LLDList *list, int *position){
if(list == NULL || list->first==NULL || list->last==NULL){
*position = -1;
return NULL;
}

listPosition aux = list->first;
*position = 0;
while(aux != end(list)){
if(aux->next->element == x)
return aux;
else{
(*position)++;
aux = aux->next;
}
}

*position = -2;
return NULL;
}

listElement get(listPosition p, LLDList *list){
if(list == NULL || list->first==NULL || list->last==NULL || p==end(list)) return -1;

return p->next->element;
}

int clear(LLDList *list){
if(list == NULL || list->first==NULL) return -1;

listPosition aux;

while(list->first->next != NULL){
aux = list->first->next;
list->first->next = list->first->next->next;
free(aux);
}

list->first->next = list->last = NULL;
return 0;
}

int destroy(LLDList *list){
if(list == NULL || list->first==NULL) return -1;

clear(list);
free(list->first);
list->first = list->last = NULL;
return 0;
}

void printList(LLDList *list){
if(list != NULL && list->first !=NULL){
printf("\nLIST\n");
if(!isEmpty(list)){
listPosition p = list->first->next;
while(p != NULL){
printf("%d ",p->element);
p = p->next;
}
}else{
printf("The list is empty");
}
puts("");
}

}














39 changes: 39 additions & 0 deletions TADS/Lists/LinkedList Imp/Dummy Node Imp/LLDList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef __LLD_LIST_H__
#define __LLD_LIST_H__

//List element
typedef int listElement;

//List structure with array implementation
typedef struct listNode{
listElement element;
struct listNode *next;
} listNode;

typedef struct LLDList {
listNode *first, *last;
} LLDList;

//List position
typedef listNode* listPosition;

//Stack methods
int createEmptyList(LLDList *list);
int isEmpty(LLDList *list);

listPosition first(LLDList *list);
listPosition next(listPosition p, LLDList *list);
listPosition prev(listPosition p, LLDList *list);
listPosition end(LLDList *list);

int listAdd(listElement x, listPosition p, LLDList *list);
int listRemove(listPosition p, LLDList *list);

listPosition find(listElement x, LLDList *list, int *position);
listElement get(listPosition p, LLDList *list);

int clear(LLDList *list);
int destroy(LLDList *list);
void printList(LLDList *list);

#endif
49 changes: 49 additions & 0 deletions TADS/Lists/LinkedList Imp/Dummy Node Imp/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>

#include "LLDList.h"


int main(int argc, char **argv){
LLDList *list;

if(NULL == (list = malloc(sizeof(LLDList)))) return -1;


createEmptyList(list);
printList(list);

listAdd(5,end(list),list);
listAdd(8,end(list),list);
listAdd(3,end(list),list);

listAdd(4,first(list),list);
printList(list);

listAdd(7, prev(prev(end(list),list),list), list);
printList(list);

listRemove(first(list),list);
printList(list);

listElement element = 7;
int position;
find(element, list, &position);
printf("\n\nElement %d finded at position %d\n",element, position);

listPosition posToGet = prev(end(list), list);
printf("\nElement %d goted\n",get(posToGet, list));


clear(list);
printList(list);

destroy(list);

puts("\n");
return 0;
}




21 changes: 21 additions & 0 deletions TADS/Lists/LinkedList Imp/Dummy Node Imp/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CC=gcc
#CFLAGS=-c
CFLAGS=-c -g
EXECUTABLE_NAME=LLDList

all: $(EXECUTABLE_NAME)

$(EXECUTABLE_NAME): main.o LLDList.o
$(CC) $^ -o $@ -lm

main.o: main.c
$(CC) $(CFLAGS) main.c

LLDList.o: LLDList.c LLDList.h
$(CC) $(CFLAGS) LLDList.c


clean:
rm *.o
rm $(EXECUTABLE_NAME)

0 comments on commit deb20c5

Please sign in to comment.