Commit 130bc8f1 authored by William Sha's avatar William Sha
Browse files

Implemented initialize and insert_empty_list

Modified initialize function header. Insert_beginning_list is still in development.
parent 53a48daa
......@@ -6,7 +6,8 @@ int main()
{
if (DEBUG) printf("Initialization of ChainedLists...\n\n");
int action; // represents the choice of the user
int action = 0; // represents the choice of the user
char str_number[5];
printf("*****CHAINED LISTS*****\n");
printf("**UTC - November 2017**\n");
......@@ -14,7 +15,8 @@ int main()
if (DEBUG) printf("Initialization of an instance of List...\n\n");
List* default_list;
initialize(default_list);
initialize(&default_list);
if (default_list->head == NULL) printf("NULL\n");
while(action != 8){
......@@ -35,10 +37,13 @@ int main()
}
if (DEBUG) printf("{action = %d}\n", action);
fflush(stdin);
switch(action){
case TOKEN_ADD_NB_BEG:
insert_beginning_list(NULL, NULL);
printf("What do you want to add at the beginning ? ");
scanf("%s", str_number);
insert_beginning_list(default_list, str_number);
break;
case TOKEN_ADD_NB_END:
......
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tp3.h"
/**
......@@ -9,47 +10,77 @@
*/
void initialize(List* list){
printf("INITIALIZE\n");
list = malloc(sizeof(List));
list->head = NULL;
list->tail = NULL;
void initialize(List** list){
if (DEBUG) printf("INITIALIZE\n");
*list = malloc(sizeof(List));
(*list)->head = NULL;
(*list)->tail = NULL;
}
void insert_empty_list(List* list, char* str){
printf("INSERT_EMPTY_LIST\n");
if (DEBUG) printf("INSERT_EMPTY_LIST\n");
if (list->head || list->tail){
printf("List is not empty... use another insert function.\n");
return;
}
Element* e = malloc(sizeof(Element));
strncpy(e->data, str, N);
list->head = e;
list->tail = e;
if (DEBUG) printf("Data inserted : %s\n", list->head->data);
}
void insert_beginning_list(List* list, char* str){
printf("INSERT_BEGINNING_LIST\n");
if (DEBUG) printf("INSERT_BEGINNING_LIST(%p, %s)\n", list, str);
// empty list
if (list->head == NULL && list->tail == NULL){
if (DEBUG) printf("The list is empty...\n");
insert_empty_list(list, str);
return;
}
Element* e = malloc(sizeof(Element));
if (DEBUG) printf("Trying to put input into a new element...\n", list, str);
strncpy(e->data, str, N);
// changes the chain to plug the new element
if (DEBUG) printf("Reorganizing the list...\n", list, str);
e->next = list->head;
list->head = e;
}
void insert_end_list(List* list, char* str){
printf("INSERT_END_LIST\n");
if (DEBUG) printf("INSERT_END_LIST\n");
}
int insert_after_position(List* list, char* str, int p){
printf("INSERT_AFTER_POSITION\n");
if (DEBUG) printf("INSERT_AFTER_POSITION\n");
}
int remove_element(List* list, int p){
printf("REMOVE_ELEMENT\n");
if (DEBUG) printf("REMOVE_ELEMENT\n");
}
int compare(char* str1, char* str2){
printf("COMPARE\n");
if (DEBUG) printf("COMPARE\n");
}
int sort(List* list){
printf("SORT\n");
if (DEBUG) printf("SORT\n");
}
void display(List* list){
printf("DISPLAY\n");
if (DEBUG) printf("DISPLAY\n");
}
void destruct(List* list){
printf("DESTRUCT\n");
if (DEBUG) printf("DESTRUCT\n");
}
void display_menu(){
......
......@@ -19,7 +19,7 @@ typedef struct list{
Element* tail;
} List;
void initialize(List* list);
void initialize(List** list);
void insert_empty_list(List* list, char* str);
void insert_beginning_list(List* list, char* str);
void insert_end_list(List* list, char* str);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment