Exercise 2
Exercise 2
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
int i,n,item;
struct node *temp,*new,*head;
printf("enter the number of nodes");
scanf("%d",&n);
head=new;
temp=head;
for(i=1;i<n;i++)
{
printf("enter the value of the next node\n");
scanf("%d",&item);
2. Write a program in C to insert a new node at the beginning of a Singly Linked List.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num; //Data of the node
struct node *nextptr; //Address of the node
}*new;
void createNodeList(int n);
void NodeInsertatBegin(int num);
void displayList();
int main()
{
int n,num;
printf("\nInsert a new node at the beginning of a Singly Linked List:\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n);
printf("\n Data entered in the list are : \n");
displayList();
printf("\n Insert an element at the begining of the list: ");
scanf("%d", &num);
NodeInsertatBegin(num);
printf("\n Data after inserted in the list are : \n");
displayList();
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int num, i;
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &num);
fnNode->num = num;
fnNode->nextptr = NULL;
tmp->nextptr = fnNode; // links previous node i.e. tmp to the fnNode
tmp = tmp->nextptr;
}
}
}
}
void displayList()
{
struct node *tmp;
if(new== NULL)
{
printf(" No data found in the list.");
}
else
{
tmp = new;
while(tmp != NULL)
{
printf(" Data = %d\n", tmp->num); // prints the data of current node
tmp = tmp->nextptr; // advances the position of current node
}
}
}
#include <stdio.h>
#include <stdlib.h>
struct node
// Function prototypes
// Main function
int main()
printf("-----------------------------\n");
scanf("%d", &n);
createNodeList(n);
displayList();
FirstNodeDeletion();
displayList();
return 0;
void createNodeList(int n)
int num, i;
if(stnode == NULL)
{
else
scanf("%d", &num);
tmp = stnode;
break;
else
tmp = tmp->nextptr;
void FirstNodeDeletion()
if(stnode == NULL)
else
toDelptr = stnode;
stnode = stnode->nextptr;
}
// Function to display the linked list
void displayList()
if(stnode == NULL)
else
tmp = stnode;
while(tmp != NULL)
printf(" Data = %d\n", tmp->data); // Prints the data of the current node
}
4. Implement a singly linked list and perform insertion and deletion operations?
#include<stdio.h>
#include<stdlib.h>
void create();
void display();
void insert_begin();
void insert_end();
void insert_pos();
void delete_begin();
void delete_end();
void delete_pos();
struct node
int data;
};
int main()
int choice;
while(1)
printf("\n*****\n");
printf("0. Create\n");
printf("1. display\n");
scanf("%d",&choice);
switch(choice)
case 0: create();
break;
case 1: display();
break;
case 2: insert_begin();
break;
case 3: insert_pos();
break;
case 4: insert_end();
break;
case 5: delete_begin();
break;
case 6: delete_end();
break;
case 7: delete_pos();
break;
case 8: exit(0);
default:printf("\n Wrong Choice");
break;
//creates a node
void create()
scanf("%d",&temp->data);
temp->next = NULL;
if(head==NULL) {
head = temp;
else{
while(ptr->next!=NULL)
ptr = ptr->next;
if(head==NULL)
return;
printf("LinkedList: ");
printf("%d ",ptr->data);
ptr = ptr->next;
printf("\n");
void insert_begin()
scanf("%d",&temp->data);
temp->next = NULL;
if(head==NULL)
{
head = temp;
return;
else
void insert_pos()
scanf("%d",&temp->data);
temp->next = NULL;
head = temp;
return;
else
scanf("%d",&pos);
for(int i=0;i<pos;i++)
prev_ptr = ptr;
ptr = ptr->next;
temp->next = ptr;
prev_ptr->next = temp;
void insert_end()
scanf("%d",&temp->data);
temp->next = NULL;
if(head==NULL)
return;
}
else{
while(ptr->next!=NULL)
ptr = ptr->next;
ptr->next = temp;
void delete_begin()
return;
else
}
// to delete last node of LinkedList
void delete_end()
return;
else if(head->next==NULL)
head = ptr->next;
free(ptr);
else
prev_ptr = ptr;
ptr = ptr->next;
prev_ptr->next = NULL; // next field of last but one field is made as NULL
}
// to delete node at given position
void delete_pos()
int pos;
scanf("%d",&pos);
return;
else if(pos == 0)
ptr = head;
else
for(int i=0;i<pos;i++)
prev_ptr = ptr;
ptr = ptr->next;
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
//function prototyping
struct node* create_node(int);
void insert_begin(int);
void reverse_list();
void print();
int main()
{
/* Create some nodes and insert data into them */
insert_begin(10);
insert_begin(90);
insert_begin(31);
insert_begin(78);
insert_begin(99);
printf("Linked List before reversed: \n");
print();
reverse_list();
printf("\nLinked List after reversed: \n");
print();
return 0;
}
/*
* Creates a new node using the malloc function
*/
struct node* create_node(int data)
{
struct node* new_node = (struct node*) malloc (sizeof(struct node));
if (new_node == NULL)
{
printf("Memory can't be allocated for new node");
return NULL;
}
else
{
new_node -> data = data;
new_node -> next = NULL;
return new_node;
}
}
/*
* prints the linked list
*/
void print()
{
struct node* temp = head;
while (temp != NULL)
{
printf("%d --> ", temp->data);
temp = temp->next;
}
printf("NULL \n");
}
6. Recursive C program to reverse the nodes of a linked list and display the elements
in reverse order?
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
//Driver Function
int main ()
{
struct node *head = NULL;
insert_new_node (&head, 1);
insert_new_node (&head, 2);
insert_new_node (&head, 3);
insert_new_node (&head, 4);
printf ("LinkedList : ");
print (head);
printf ("\nLinkedList in reverse order : ");
print_reverse_recursive (head);
printf ("\n");
return 0;
}
//Recursive Reverse
void print_reverse_recursive (struct node *head)
{
if (head == NULL)
{
return;
}
7. Write a program in C to create a singly linked list of n nodes and count the number
of nodes.
#include <stdio.h>
#include <stdlib.h>
struct node
// Function prototypes
// Main function
int main()
int n, totalNode;
// Displaying the purpose of the program
printf("\n\n Linked List : Create a singly linked list and count the number of nodes :\n");
printf("--------------------------------------\n");
scanf("%d", &n);
createNodeList(n);
displayList();
totalNode = NodeCount();
return 0;
}
void createNodeList(int n) {
int num, i;
if(stnode == NULL)
else
scanf("%d", &num);
stnode->num = num;
stnode->nextptr = NULL; // Setting the next pointer to NULL
tmp = stnode;
if(fnNode == NULL)
break;
else
int NodeCount()
int ctr = 0;
tmp = stnode;
while(tmp != NULL)
ctr++;
tmp = tmp->nextptr;
return ctr;
}
// Function to display the linked list
void displayList()
if(stnode == NULL)
else
tmp = stnode;
while(tmp != NULL)