online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
#include<stdio.h> #include<stdlib.h> typedef struct { int data; struct Node * next; } Node; void list_init(Node ** pHead, int data); int list_length(Node ** pHead); int list_get(Node ** pHead, int index); void list_delete(Node ** pHead, int index); int list_index_errno(Node ** pHead, int index); void list_add(Node ** pHead, int index, int data); void list_show(Node ** pHead); void list_push(Node ** pHead, int data); void list_unshift(Node ** pHead); void list_pop(Node ** pHead); void list_shift(Node ** pHead); void list_clear(Node ** pHead); void list_init(Node ** pHead, int data){ Node * tmp = *pHead; if(!tmp) { Node * tmp2 = malloc(sizeof(Node)); tmp2->data = data; tmp2->next = NULL; *pHead = tmp2; } } int list_length(Node ** pHead){ Node * tmp = *pHead; if(!tmp) return 0; int count = 0; Node * current = tmp; while (current){ count++; current = (Node *)current->next; } return count; } int list_get(Node ** pHead, int index){ int err_no = list_index_errno(pHead, index); if( !err_no ){ Node * tmp = *pHead; Node * current = tmp; for (int i = 0; i<index; i++){ current=(Node *)current->next; } return current->data; } return 0; } void list_delete(Node ** pHead, int index){ Node * tmp = *pHead; int err_no = list_index_errno(pHead, index); if(!err_no) { Node * current; Node * previous; current = tmp; for (int i = 0; i<index; i++){ previous=current; current=(Node *)current->next; } if(index==0){ *pHead = (Node *)current->next; free(tmp); } else{ previous->next=current->next; free(current); } } } int list_index_errno(Node ** pHead, int index){ Node * tmp = *pHead; if( !tmp ){ return 1; } else { int len = list_length(pHead); if(index < 0){ return 2; } else if(index>=len){ return 3; } } return 0; } void list_add(Node ** pHead, int index, int data){ Node * tmp = *pHead; if(!tmp) { list_init(pHead, data); } else { int err_no = list_index_errno(pHead, index-1); if(! err_no ){ Node * current = tmp; Node * previous = NULL; for (int i = 0; i<index; i++){ previous=current; current=(Node *)current->next; } Node * new_node = malloc(sizeof (Node)); new_node->data = data; new_node->next = (struct Node *)current; if(index==0){ *pHead = new_node; } else{ previous->next = (struct Node *)new_node; } } } } void list_show(Node ** pHead){ Node * tmp = *pHead; if(!tmp) { printf("no data!\n"); } else { int len = list_length(pHead); for(int i = 0; i<len; i++){ int data = list_get(pHead, i); printf("data %d => %d \n", i, data); } } } void list_push(Node ** pHead, int data){ int index = list_length(pHead); list_add(pHead,index,data); } void list_unshift(Node ** pHead){ Node * tmp = *pHead; int data; printf("Enter data: "); scanf("%d", &data); if(!tmp) { Node * tmp2 = malloc(sizeof(Node)); tmp2->data = data; tmp2->next = NULL; *pHead = tmp2; } else { Node * tmp2 = malloc(sizeof(Node)); tmp2->data = data; tmp2->next = (struct Node *)pHead; *pHead = tmp2; } } void list_pop(Node ** pHead){ int index = list_length(pHead); list_delete(pHead, index-1); } void list_shift(Node ** pHead){ list_delete(pHead, 0); } void list_clear(Node ** pHead){ Node * tmp = *pHead; if(!tmp) { printf("no data!\n"); } else { int len = list_length(pHead); for(int i = 0; i<len; i++){ list_pop(pHead);//just use pop } } } int main() { Node * head = NULL; int menu=0; do { printf("1. Show \t 6. Shift \n"); printf("2. Get \t\t 7. Delete \n"); printf("3. Push \t 8. Clear \n"); printf("4. Unshift \t 9. exit \n"); printf("5. Pop \t \n"); printf("Select Menu: "); scanf("%d", &menu); switch (menu) { case 1: { list_show(&head); break; } case 2: { int index; printf("index: "); scanf("%d", &index); printf("data : %d \n", list_get(&head, index)); break; } case 3: { int data; printf("enter data: "); scanf("%d", &data); list_push(&head, data); break; } case 4: { list_unshift(&head); break; } case 5: { list_pop(&head); break; } case 6: { list_shift(&head); break; } case 7: { int index; printf("index: "); scanf("%d", &index); list_delete(&head, index); break; } case 8: { list_clear(&head); break; } default: { if(menu!=9){ printf("Invalid menu code!\n"); } break; } } } while (menu!=9); return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue