Skip to content

Instantly share code, notes, and snippets.

@aki017
Created December 3, 2012 09:58
Show Gist options
  • Save aki017/4193991 to your computer and use it in GitHub Desktop.
Save aki017/4193991 to your computer and use it in GitHub Desktop.
list
#include <stdio.h>
#include <stdlib.h>
typedef struct LIST{
struct LIST *next;
struct LIST *prev;
char *str;
}List;
void help();
char* get(List *list, int index);
List* push(List *list, char *str);
List* new(char *str);
char* get(List *list, int index)
{
int i;
for(i=0;i<index;i++)
{
if((list = list->next) == NULL)
{
fprintf(stderr, "Index Out of Range Exception\n\tindex:%d is out of range(%d).\n", index, i);
exit(EXIT_FAILURE);
}
}
return list->str;
}
List* new(char *str)
{
List *list = (List *)malloc(sizeof(List));
list->str = str;
return list;
}
List* push(List *list, char *str)
{
List *l = list;
if(list == NULL) return new(str);
while(list->next != NULL)
{
list = list->next;
}
list->next = (List *)malloc(sizeof(List));
list->next->str = str;
list->next->prev = list;
return l;
}
void p(List *list)
{
int i = 0;
printf("-----\n");
while(list != NULL)
{
printf("%d : %s \n", i++, list->str);
list = list->next;
}
printf("-----\n");
}
int main(int argc, char *argv[])
{
int i;
List *list;
list = push(list, "test1");
p(list);
list = push(list, "test2");
p(list);
list = push(list, "test3");
p(list);
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment