You are on page 1of 2

#include <stdio.

h>
#include <stdlib.h>

struct node
{
int data;
struct node *next;
} *head;

void initialize()
{
head = NULL;
}

void Front(int num)


{
struct node* new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = num;
new_node->next = head;
head = new_node;
printf("Inserted Element : %d\n", num);
}

void End(struct node* head, int num)


{
if (head == NULL)
{
printf("Error : Invalid node pointer !!!\n");
return;
}
struct node* new_node =(struct node*) malloc(sizeof(struct node));
new_node->data = num;
new_node->next = NULL;
while(head->next != NULL)
head = head->next;
head->next = new_node;
}

void printLinkedList(struct node *Ptr) {


printf("\n Linked List :-\n");
while (Ptr != NULL) {
printf(" %d", Ptr->data);
Ptr = Ptr->next;
if(Ptr != NULL)
printf(" ");
}
}

int main() {
initialize();
printf("\n Name : Ayushmaan Kapahi ");
printf("\nRoll Number : 1/20/FET/BCS/031");
printf("\n----------------------------------\n\n");
Front(4); // Inserting node at front
Front(3); // Inserting node at front
Front(2); // Inserting node at front
Front(1); // Inserting node at front
printf("\n\n Before insertion at the end of list :-");
printf("\n -------------------------------------\n");
printLinkedList(head);
End(head,5); // Inserting node at the end
printf("\n\n After insertion at the end of list :-");
printf("\n ------------------------------------\n");
printLinkedList(head);
return 0;
}

You might also like