#include <stdio.h> #include <stdlib.h> struct node{ int value; struct node*next; }; typedef struct node Node; int main() { int input=0; Node*list, *tmp; list=tmp=NULL; while(input != -1) { fprintf(stdout, "Enter a value (-1 to end): "); fscanf(stdin, "%d", &input); if(input != -1) { if(list == NULL) { list=tmp=(Node*)malloc(sizeof(Node)); tmp->next=NULL; list->value=input; } else { tmp->next=(Node*)malloc(sizeof(Node)); tmp->next->next=NULL; tmp->next->value=input; tmp=tmp->next; } } } tmp=list; input=0; while(tmp != NULL) { fprintf(stdout, "%d ->",tmp->value); tmp=tmp->next; input=input++; } fprintf(stdout, "NULL\n"); //insert into list fprintf(stdout, "Which node would you like to insert before? "); fscanf(stdin, "%d", &input); int seeker; tmp=list; Node*tmp2=NULL; for(seeker=0; seeker < (input-1); seeker++) { tmp=tmp->next; } fprintf(stdout, "Eneter a value to insert: "); fscanf(stdin, "%d", &input); tmp2=(Node*)malloc(sizeof(Node)); tmp2->value=input; tmp2->next=tmp->next; tmp->next=tmp2; return(0); }