#include #include #include #include struct node { int data; struct node* next; }; int DeleteNth(struct node** headRef, int index){ struct node* current = *headRef; struct node* next; struct node* nextnext; int i; for(i=1; inext; } next = current->next; nextnext = next->next; if (nextnext == NULL){ current->next = NULL; assert(next!=NULL); free(next); return 0; } current->next = nextnext; assert(next!=NULL); free(next); return 0; } int Pop(struct node** headRef){ struct node* current = *headRef; struct node* next; if(current != NULL){ int value = current->data; next = current->next; free(current); *headRef = next; return value; } printf("You can't pop anymore!"); return 0; } void PrintList(struct node* head){ struct node* current = head; int count = 0; printf("The following values are in the list:\n"); while (current != NULL){ count++; printf("%d. %d\n", count ,current->data); current = current->next; } } void Push(struct node** headRef, int data){ struct node* newNode = malloc(sizeof(struct node)); newNode->data = data; newNode->next = *headRef; *headRef = newNode; } void insertNth(struct node** headRef, int index, int data ){ if(index==0) Push(headRef,data); else{ struct node* current = *headRef; int i; for (i=1;inext; } assert(current!=NULL); Push(&(current->next),data); } } void deleteList(struct node** headRef){ struct node* current = *headRef; struct node* next; while (current != NULL){ next = current->next; free(current); current = next; } *headRef = NULL; } void UserInput(struct node** headRef){ int ans; int index; printf("What do you want to do?\n (1) Add value, (2) Add value to certain index, (3) Delete first value\n (4) Delete specific index (5) Delete list: "); scanf(" %d",&ans); switch (ans) { case 1: printf("What value?: "); scanf(" %d",&ans); insertNth(&*headRef,0, ans); break; case 2: printf("What value?: "); scanf(" %d",&ans); printf("What index?: "); scanf(" %d",&index); insertNth(&*headRef, index, ans); break; case 3: Pop(&*headRef); break; case 4: printf("What index?: "); scanf(" %d",&index); if (index == 1) Pop(&*headRef); else{ DeleteNth(&*headRef, index);} break; case 5: deleteList(&*headRef); break; } } void main(){ struct node* head = malloc(sizeof(struct node)); Pop(&head); while(1){ int a; int b; UserInput(&head); PrintList(head); } }