Skip to content

Instantly share code, notes, and snippets.

@simplexityx
Created January 16, 2017 22:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simplexityx/9d01e2cf984ac8ced7b16664ddeed386 to your computer and use it in GitHub Desktop.
Save simplexityx/9d01e2cf984ac8ced7b16664ddeed386 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list;
/*
* The type of comparison functions.
*/
typedef int (*cmpfunc_t)(void *, void *);
/*
* Prints an error message and terminates the program.
* Use this to report fatal errors that prevent your program from proceeding.
*/
void fatal_error(char *msg);
/*
* Reads the given file, and parses it into words (tokens).
* Adds the words to the given list, in the same order that they
* occur in the file.
*
* This tokenizer ignores punctuation and whitespace, so if the file
* contains the text "Hello! This is an example...." the recognized
* words will be "Hello", "This", "is", "an", and "example".
*/
void tokenize_file(FILE *file, struct list *list);
#endif
#include "list.h"
#include "common.h"
#include <stdio.h>
#include <stdlib.h>
typedef struct list list_t;
typedef struct node node_t;
struct node{
void *elements;
node_t *next;
node_t *prev;
};
struct list{
int numitems;
node_t *listhead;
};
/*
* Creates a new, empty list that uses the given comparison function
* to compare elements. The comparison function accepts two elements,
* and returns -1 if the first is smaller than the second, 1 if the
* first is greater than the second, and 0 if the elements are equal.
*
* Returns the new list.
*/
list_t *list_create(cmpfunc_t cmpfunc){
list_t *nyliste;
nyliste=malloc(sizeof(list_t));
nyliste->listhead=NULL;
nyliste->numitems=0;
return nyliste;
}
/*
* Destroys the given list. Subsequently accessing the list
* will lead to undefined behavior.
*/
void list_destroy(list_t *list){
node_t *a, *temp;
a=list->listhead;
while(a!=NULL){
temp=a;
a=list->listhead->next;
free(temp);
}
free(list);
}
/*
* Returns the current size of the given list.
*/
int list_size(list_t *list){
return list->numitems;
}
/*
* Adds the given element to the start of the given list.
*/
void list_addfirst(list_t *list, void *elem){
node_t *nynode;
nynode = malloc(sizeof(node_t));
nynode->next=list->listhead;
nynode->elements=elem;
list->numitems++;
list->listhead=nynode;
}
/*
* Adds the given element to the end of the given list.
*/
void list_addlast(list_t *list, void *elem){
node_t *temp, *nynode;
nynode=malloc(sizeof(node_t));
temp=list->listhead;
while(temp->next!=NULL){
temp->next=temp->next->next;
}
nynode->next=temp->next;
temp->next=nynode;
nynode->elements=elem;
list->numitems++;
}
/*
* Removes and returns the first element of the given list.
*/
void *list_popfirst(list_t *list){
void *element;
node_t* tmp;
if(list->listhead==NULL){
return NULL;
}
element=list->listhead->elements;
tmp=list->listhead;
list->listhead=list->listhead->next;
list->numitems-=1;
return element;
}
/*
* Removes and returns the last element of the given list.
*/
void *list_poplast(list_t *list){
void *element;
node_t *tmp;
tmp=list->listhead;
while( tmp->next!= NULL){
tmp->next=tmp->next->next;
}
element=tmp->elements;
list->numitems--;
return element;
}
/*
* Returns 1 if the given list contains the given element, 0 otherwise.
*
* The comparison function of the list is used to check elements for equality.
*/
int list_contains(list_t *list, void *elem){
node_t *tmp;
int a;
tmp=malloc(sizeof(node_t));
tmp=list->listhead;
while(tmp != NULL){
printf("søker");
if(tmp->elements==elem){
free(tmp);
return 1;
}
tmp=tmp->next;
}
free(tmp);
return 0;
}
/*
* Sorts the elements of the given list, using the comparison function
* of the list to determine the ordering of the elements.
*/
void list_sort(list_t *list){
}
/*
* The type of list iterators.
*/
typedef struct list_iter list_iter_t;
struct list_iter{
node_t *next;
list_t *list;
};
/*
* Creates a new list iterator for iterating over the given list.
*/
list_iter_t *list_createiter(list_t *list){
list_iter_t *iter;
iter=malloc(sizeof(list_iter_t));
iter->list=list;
iter->next=list->listhead;
return iter;
}
/*
* Destroys the given list iterator.
*/
void list_destroyiter(list_iter_t *iter){
free(iter);
}
/*
* Returns 0 if the given list iterator has reached the end of the
* list, or 1 otherwise.
*/
int list_hasnext(list_iter_t *iter){
if(iter->next==NULL){
return 0;
}
return 1;
}
/*
* Returns the next element in the sequence represented by the given
* list iterator.
*/
void *list_next(list_iter_t *iter){
void *element = NULL;
if (iter->next != NULL) {
element = iter->next->elements;
iter->next = iter->next->next;
}
return element;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment