-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added List with Linked List Dummy Node implementation (TAD)
- Loading branch information
Showing
4 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(""); | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|