0% found this document useful (0 votes)
6 views29 pages

Exercise 2

The document contains multiple C programs demonstrating various operations on singly linked lists, including creating a linked list, inserting nodes at different positions, deleting nodes, displaying the list, and reversing the list both iteratively and recursively. Each program includes functions for specific tasks, such as creating nodes, displaying the list, and handling memory allocation. The overall focus is on managing linked lists through user input and function calls.

Uploaded by

anusha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views29 pages

Exercise 2

The document contains multiple C programs demonstrating various operations on singly linked lists, including creating a linked list, inserting nodes at different positions, deleting nodes, displaying the list, and reversing the list both iteratively and recursively. Each program includes functions for specific tasks, such as creating nodes, displaying the list, and handling memory allocation. The overall focus is on managing linked lists through user input and function calls.

Uploaded by

anusha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

1. C Program to create the linked list and display the elements in the linked list?

#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);

printf("enter the value of the head node");


scanf("%d",&item);

new=(struct node *)malloc(sizeof(struct node));


new->data=item;
new->next=NULL;

head=new;
temp=head;
for(i=1;i<n;i++)
{
printf("enter the value of the next node\n");
scanf("%d",&item);

new=(struct node *)malloc(sizeof(struct node));


new->data=item;
new->next=NULL;
temp->next=new;
temp=temp->next;
}
printf("\n");
temp=head;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}
return 0;
}

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;

new = (struct node *)malloc(sizeof(struct node));


if(new == NULL)
{
printf(" Memory can not be allocated.");
}
else
{

printf(" Input data for node 1 : ");


scanf("%d", &num);
new-> num = num;
new-> nextptr = NULL;
tmp = new;

//Creates n nodes and adds to linked list


for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));

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 NodeInsertatBegin(int num)


{
struct node *fnNode;
fnNode = (struct node*)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
fnNode->num = num; //Links the data part
fnNode->nextptr = new; //Links the address part
new = fnNode; //Makes stnode as first node
}
}

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
}
}
}

3. Write a program in C to delete the first node of a Singly Linked List?

#include <stdio.h>

#include <stdlib.h>

struct node

int data; // Data of the node

struct node *nextptr; // Address of the node

}*stnode; // Pointer to the starting node

// Function prototypes

void createNodeList(int n); // Function to create the linked list

void FirstNodeDeletion(); // Function to delete the first node

void displayList(); // Function to display the linked list

// Main function
int main()

int n, data, pos;

printf("\n Delete first node of Singly Linked List :\n");

printf("-----------------------------\n");

printf(" Input the number of nodes : ");

scanf("%d", &n);

createNodeList(n);

printf("\n Data entered in the list are : \n");

displayList();

FirstNodeDeletion();

printf("\n Data, after deletion of first node : \n");

displayList();

return 0;

// Function to create a linked list with n nodes

void createNodeList(int n)

struct node *fnNode, *tmp;

int num, i;

stnode = (struct node *)malloc(sizeof(struct node));

if(stnode == NULL)
{

printf(" Memory can not be allocated.");

else

printf(" Input data for node 1 : ");

scanf("%d", &num);

stnode-> data = num;

stnode-> nextptr = NULL; // Links the address field to NULL

tmp = stnode;

for(i = 2; i <= n; i++)

fnNode = (struct node *)malloc(sizeof(struct node));

if(fnNode == NULL) // Check whether fnNode is NULL for memory allocation

printf(" Memory can not be allocated.");

break;

else

printf(" Input data for node %d : ", i);

scanf(" %d", &num);

fnNode->data= num; // Links the num field of fnNode with num

fnNode->nextptr = NULL; // Links the address field of fnNode with NULL


tmp->nextptr = fnNode; // Links previous node i.e. tmp to the fnNode

tmp = tmp->nextptr;

// Function to delete the first node of the list

void FirstNodeDeletion()

struct node *toDelptr;

if(stnode == NULL)

printf(" There are no nodes in the list.");

else

toDelptr = stnode;

stnode = stnode->nextptr;

printf("\n Data of node 1 which is being deleted is : %d\n", toDelptr->data);

free(toDelptr); // Clears the memory occupied by the first node

}
// Function to display the linked list

void displayList()

struct node *tmp;

if(stnode == NULL)

printf(" No data found in the list.");

else

tmp = stnode;

while(tmp != NULL)

printf(" Data = %d\n", tmp->data); // Prints the data of the current node

tmp = tmp->nextptr; // Advances the position 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* head = NULL;

struct node

int data;

struct node* next;

};

int main()

int choice;

while(1)

printf("\n*****\n");

printf("0. Create\n");

printf("1. display\n");

printf("2. Insert Node at beginning\n");

printf("3. Insert Node in specific position\n");


printf("4. Insert Node at end of LinkedList\n");

printf("5. Delete Node at beginning\n");

printf("6. Delete Node at end\n");

printf("7. Delete Node at position\n");

printf("8. ** To exit **");

printf("\n Enter your choice: ");

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()

struct node* temp;

//creating new node

temp = (struct node*)malloc(sizeof(struct node));

printf("Enter node data: ");

scanf("%d",&temp->data);

temp->next = NULL;

if(head==NULL) {

head = temp;

else{

struct node* ptr = head;

while(ptr->next!=NULL)

ptr = ptr->next;

ptr->next = temp; //inserting at end of List

// prints the entire LinkedList


void display()

if(head==NULL)

printf("Linked List is Empty\n");

return;

printf("LinkedList: ");

struct node* ptr = head;

while(ptr!=NULL) // start from first node

printf("%d ",ptr->data);

ptr = ptr->next;

printf("\n");

// to insert node at start of LinkedList

void insert_begin()

struct node* temp;

// creating a new node

temp = (struct node*)malloc(sizeof(struct node));

printf("Enter node data: ");

scanf("%d",&temp->data);

temp->next = NULL;

if(head==NULL)

{
head = temp;

return;

else

temp->next = head; //point it to old head node

head = temp; //point head to new first node

// to insert node at given position

void insert_pos()

struct node* temp;

// creating a new node

temp = (struct node*)malloc(sizeof(struct node));

printf("Enter node data: ");

scanf("%d",&temp->data);

temp->next = NULL;

if(head==NULL) // if list empty we return

head = temp;

return;

else

struct node* prev_ptr;

struct node* ptr = head;


int pos;

printf("Enter position: ");

scanf("%d",&pos);

for(int i=0;i<pos;i++)

prev_ptr = ptr;

ptr = ptr->next;

//new node pointing to node in that pos

temp->next = ptr;

//prevptr pointing to new node

prev_ptr->next = temp;

// to insert node at end of LinkedList

void insert_end()

struct node* temp;

//creating new node

temp = (struct node*)malloc(sizeof(struct node));

printf("Enter node data: ");

scanf("%d",&temp->data);

temp->next = NULL;

if(head==NULL)

head = temp; //if list is empty, we return

return;
}

else{

struct node* ptr = head;

while(ptr->next!=NULL)

ptr = ptr->next;

// tail node pointing to new node

ptr->next = temp;

// to delete first node of LinkedList

void delete_begin()

if(head==NULL) //if List is empty we return

printf("Linked List is empty | Nothing to delete \n");

return;

else

struct node* ptr = head;

head = head->next; // head node pointing to second node

free(ptr); // deleting prev head node

printf("Node Deleted \n");

}
// to delete last node of LinkedList

void delete_end()

if(head==NULL) //if List is empty we return

printf("Linked List is empty | Nothing to delete \n");

return;

else if(head->next==NULL)

struct node* ptr = head;

head = ptr->next;

free(ptr);

else

struct node* ptr = head;

struct node* prev_ptr = NULL;

while(ptr->next!=NULL)// traverse till last but one node

prev_ptr = ptr;

ptr = ptr->next;

prev_ptr->next = NULL; // next field of last but one field is made as NULL

free(ptr); // deleting last node

}
// to delete node at given position

void delete_pos()

int pos;

printf("Enter node position to delete: ");

scanf("%d",&pos);

struct node* ptr=head;

if(head==NULL) //we return if List is empty

printf("Linked List is empty \n");

return;

else if(pos == 0)

ptr = head;

head=ptr->next; // head pointing to second node

free(ptr); // deleting old first node

else

struct node* prev_ptr;

for(int i=0;i<pos;i++)

prev_ptr = ptr;

ptr = ptr->next;

prev_ptr->next = ptr->next; //prev node pointing to pos+1 node


free(ptr); //deleting node at pos

5. C Program to Reverse a Linked List using iterative approach?

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};

//Globally initialized head pointer


struct node* head = NULL;

//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;
}
}

/* insert a new node at the beginning of the list


*/
void insert_begin(int data)
{
struct node* new_node = create_node(data);
if (new_node != NULL)
{
new_node -> next = head;
head = new_node;
}
}
/*
* reverse the linked list
*/
void reverse_list()
{
if (head == NULL)
{
return;
}
struct node* temp = head;
struct node* new_head = NULL;

// create new nodes and insert them beginning


while (temp != NULL)
{
struct node* new_node = create_node(temp->data);
new_node->next = new_head;
new_head = new_node;
temp = temp->next;
}

// update the head with the new head


head = new_head;
}

/*
* 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;
};

void print_reverse_recursive (struct node *);


void print (struct node *);
void create_new_node (struct node *, int );

//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;
}

//Recursive call first


print_reverse_recursive (head -> next);
//Print later
printf ("%d ", head -> data);
}

//Print the linked list normal


void print (struct node *head)
{
if (head == NULL)
{
return;
}
printf ("%d ", head -> data);
print (head -> next);
}

//New data added in the start


void insert_new_node (struct node ** head_ref, int new_data)
{
struct node * new_node = (struct node *) malloc (sizeof (struct node));
new_node -> data = new_data;
new_node -> next = (*head_ref);
(*head_ref) = new_node;
}

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>

// Structure for a node in a linked list

struct node

int num; // Data of the node

struct node *nextptr; // Address of the next node

} *stnode; // Pointer to the starting node

// Function prototypes

void createNodeList(int n); // Function to create the linked list

int NodeCount(); // Function to count the nodes

void displayList(); // Function to display the linked list

// 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");

// Inputting the number of nodes for the linked list

printf(" Input the number of nodes : ");

scanf("%d", &n);

// Creating the linked list with n nodes

createNodeList(n);

printf("\n Data entered in the list are : \n");

// Displaying the data entered in the linked list

displayList();

// Counting the number of nodes in the linked list

totalNode = NodeCount();

printf("\n Total number of nodes = %d\n", totalNode);

return 0;
}

// Function to create a linked list with n nodes

void createNodeList(int n) {

struct node *fnNode, *tmp;

int num, i;

// Allocating memory for the starting node

stnode = (struct node *)malloc(sizeof(struct node));

// Checking if memory allocation is successful

if(stnode == NULL)

printf(" Memory can not be allocated.");

else

// Reading data for the starting node from user input

printf(" Input data for node 1 : ");

scanf("%d", &num);

stnode->num = num;
stnode->nextptr = NULL; // Setting the next pointer to NULL

tmp = stnode;

// Creating n nodes and adding them to the linked list

for(i = 2; i <= n; i++)

fnNode = (struct node *)malloc(sizeof(struct node));

// Checking if memory allocation is successful

if(fnNode == NULL)

printf(" Memory can not be allocated.");

break;

else

// Reading data for each node from user input

printf(" Input data for node %d : ", i);

scanf(" %d", &num);

fnNode->num = num; // Setting the data for fnNode

fnNode->nextptr = NULL; // Setting the next pointer to NULL

tmp->nextptr = fnNode; // Linking the current node to fnNode


tmp = tmp->nextptr; // Moving tmp to the next node

// Function to count the number of nodes in the linked list

int NodeCount()

int ctr = 0;

struct node *tmp;

tmp = stnode;

// Counting the nodes by traversing the linked list

while(tmp != NULL)

ctr++;

tmp = tmp->nextptr;

return ctr;

}
// Function to display the linked list

void displayList()

struct node *tmp;

if(stnode == NULL)

printf(" No data found in the list.");

else

tmp = stnode;

while(tmp != NULL)

printf(" Data = %d\n", tmp->num); // Printing the data of current node

tmp = tmp->nextptr; // Advancing the position of current node

You might also like