I am new at this, so probably a lot of mistakes, I tried to make a header file and pasted all functions, Here:
Code:
/*linkedlist.h contains methods to delete, push, display Linked List*/struct Node{
int data;
struct Node *next;
}*head;
struct Node* push(struct Node *current_head, int new_data);
void displayList(struct Node* current_head);
struct Node* deleteList(struct Node* current_head);
struct Node* dynamicList(struct Node* current_head)
{ int num;
puts("Enter the elements:(-1 to end)");
while(num != -1){
scanf("%d", &num);
if(num != -1)
current_head = push(current_head, num);
else
break;
}
return current_head;
}
struct Node* deleteList(struct Node* current_head){
struct Node *next;
while(current_head != NULL){
next = current_head->next;
free(current_head);
current_head = next;
}
printf("The list is deleted.");
return current_head;
}
void displayList(struct Node* current_head)
{
while(current_head != NULL){
printf("%d\n", current_head->data);
current_head = current_head->next;
}
}
struct Node* push(struct Node *current_head, int new_data)
{
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = current_head;
return new_node;
}
Then I created another file and imported those functions:
Code:
#include <stdio.h>#include <stdlib.h>
#include "linkedlist.h"
int main(){
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
head = dynamicList(head);
displayList(head);
head = deleteList(head);
}
When I am running this code after the input, the list is not displayed but a series of numbers(addresses?) are displayed like in integer overflows.