Below is the program for Endogenous (Normal) linked list.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
};
void push(struct node **, int);
void append(struct node **, int);
void printList(struct node *);
int countNodes(struct node *);
void freeList(struct node **);
int main()
{
struct node *list = NULL;
append(&list, 1);
append(&list, 2);
append(&list, 3);
append(&list, 4);
append(&list, 5);
printf("No of nodes %d\n", countNodes(list));
printList(list);
freeList(&list);
return 0;
}
//Adds a node at beginning of list
void push(struct node **head_ref, int new_data)
{
struct node *new_node = (struct node *)malloc(sizeof(struct node));
if(new_node == NULL)
{
printf(“Memory allocation failed”);
exit(EXIT_FAILURE);
}
else
{
new_node->data = new_data;
if(*head_ref == NULL) //if list is empty
{
new_node->link = NULL;
*head_ref = new_node;
}
else //if list is not empty
{
new_node->link = *head_ref;
*head_ref = new_node;
}
}
}
//Adds a node at end of list
void append(struct node **head_ref, int new_data)
{
struct node *new_node = (struct node *)malloc(sizeof(struct node));
if(new_node == NULL)
{
printf(“Memory allocation failed”);
exit(EXIT_FAILURE);
}
else
{
new_node->data = new_data;
new_node->link = NULL;
if(*head_ref == NULL) //If list is empty
*head_ref = new_node;
else //If list is not empty
{
struct node *temp_node = *head_ref;
while(temp_node->link != NULL)
temp_node = temp_node->link;
temp_node->link = new_node;
}
}
}
void printList(struct node *n)
{
if(n != NULL)
{
while (n != NULL)
{
printf(" %d “, n->data);
n = n->link;
}
printf(”\n");
}
else
printf(“You called with empty list\n”);
}
int countNodes(struct node *node_ref)
{
int nodeCount = 0;
while(node_ref != NULL)
{
node_ref = node_ref->link;
nodeCount++;
}
return nodeCount;
}
void freeList(struct node **head_ref)
{
if(*head_ref == NULL)
printf(“List is empty\n”);
else
{
struct node *current, *next;
current = *head_ref;
while(current != NULL)
{
next = current->link;
//printf("Now %d deleted\n", current->data);
free(current);
current = next;
}
printf("List is free now\n");
*head_ref = NULL;
}
}
The below is node representation of Exogenous linked list
struct node
{
int *dataPonter;
struct node *link;
};
Please anyone let me know how to write push(), append() and other functions implementation.
Thnaks