0% found this document useful (0 votes)
44 views

Assignment 5

The document describes a C program to perform operations on a singly linked list including creation, insertion of nodes at different positions (start, end, after a node), deletion of nodes from different positions (start, end, after a node), and traversal. The operations are implemented through functions like insert_begin(), insert_last(), insert_locc(), delete_begin(), delete_last(), delete_locc(), and print(). A main() function calls these functions based on user input to perform the requested operation.

Uploaded by

SHIVANG PANDEY
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Assignment 5

The document describes a C program to perform operations on a singly linked list including creation, insertion of nodes at different positions (start, end, after a node), deletion of nodes from different positions (start, end, after a node), and traversal. The operations are implemented through functions like insert_begin(), insert_last(), insert_locc(), delete_begin(), delete_last(), delete_locc(), and print(). A main() function calls these functions based on user input to perform the requested operation.

Uploaded by

SHIVANG PANDEY
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Assignment -5

Write a program that performs following operations in one way linear linked list. a)
Creation
b) Insertion node at
(start, end, and after a node positions)
c) Deletion node from (start, end, and particular node positions) d)
Traversal

Ans:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *start;
/*fuction declaration of all the operations*/
void insert_begin();
void insert_last(); void
insert_locc(); void
delete_begin(); void
delete_last(); void
delete_locc(); void
print();
void main ()
{
int ch=0;
while(ch!=8)
{
printf("\nEnter the operation to be performed\n");
printf("\n1.Insert in the begining\n2.Insert at last\n3.Insert at any specified position\
n4.Delete from Beginning\n5.Delete from last\n6.Delete node after specified location\
n7.Show\n8.Exit\n");
scanf("\n%d",&ch);
switch(ch)
{ /*function calls of all the operations */
case 1:
insert_begin();
break;
case 2:
insert_last();
break;
case 3:
insert_locc();
break;
case 4:
delete_begin();
break;
case 5:
delete_last();
break;
case 6:
delete_locc();
break;
case 7:
print();
break;
case 8:
exit(0);
break;
default:
printf("Enter valid option");
}
}
} /*function definition*/
void insert_begin() //to insert the node at the beginnning of linked list
{
struct node *p;
int value;
p=(struct node *) malloc(sizeof(struct node *));
if(p==NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&value);
p->data=value;
p->next=start;
start=p;
}
}
void insert_last() //to insert the node at the last of linked list
{
struct node *p,*temp;
int value;
p=(struct node*)malloc(sizeof(struct node));
if(p==NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&value);
p->data=value;
if(start==NULL)
{
p->next=NULL;
start=p;
}
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
p->next=NULL;
}
}
}
void insert_locc() //to insert the node at the specified location of linked list
{
int i,loc,value;
struct node *p, *temp;
p=(struct node *)malloc(sizeof(struct node));
if(p==NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter element value");
scanf("%d",&value);
p->data=value;
printf("\nEnter the location after which you want to insert ");
scanf("\n%d",&loc);
temp=start;
for(i=0;i<loc;i++)
{
temp=temp->next;
if(temp==NULL)
{
printf("\ncan't insert\n");
return;
}
}
p->next=temp->next;
temp->next=p;
}
}
void delete_begin() //to delete the node present in the beginning of the linked list
{
struct node *p;
if(start==NULL)
{
printf("\nList is empty\n");
}
else
{
p=start;
start=p->next;
free(p);
}
}
void delete_last() //to delete the node present in the last of the linked list
{
struct node *p,*p1;
if(start==NULL)
{
printf("\nlist is empty");
}
else if(start->next==NULL)
{
start=NULL;
free(start);
printf("\nOnly node of the list deleted ...\n");
}
else
{
p=start;
while(p->next!=NULL)
{
p1=p;
p=p->next;
}
p1->next=NULL;
free(p);
}
}
void delete_locc() //to delete the node present at the specified of the linked list
{
struct node *p,*p1;
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
p=start;
for(i=0;i<loc;i++)
{
p1=p;
p=p->next;

if(p==NULL)
{
printf("\nCan't delete"); return;
}
}
p1->next=p->next;
free(p);
printf("\nDeleted node %d ",loc+1);
}
void print() //to print the values in the linked list
{
struct node *p;
p=start;
if(p==NULL)
{
printf("Nothing to print");
}
else
{
printf("\nprinting values\n");
while (p!=NULL)
{
printf("\n%d",p->data);
p=p->next;
}
}
}

Output:

Write a program that performs following operations in one way circular linked list. a)
Creation
b) Insertion node at (start,
end, and after a node positions)
c) Deletion node from (start, end, and particular node positions)
d) Traversal

Answer:
Insertion:
// C program for the above operation #include
<stdio.h>
#include <stdlib.h>

// Structure of a linked list node struct


node {
int info;
struct node* next;
};

// Pointer to last node in the list struct


node* last = NULL;

// Function to insert a node in the


// starting of the list
void insertAtFront(int data)
{
// Initialize a new node
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));

// If the new node is the only


// node in the list if
(last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}

// Else last node contains the


// reference of the new node and
// new node contains the reference
// of the previous first node
else {
temp->info = data;
temp->next = last->next;

// last node now has reference


// of the new node temp
last->next = temp;
}
}

// Function to print the list


void viewList()
{
// If list is empty
if (last == NULL)
printf("\nList is empty\n");

// Else print the list else


{
struct node* temp;
temp = last->next;

// While first node is not


// reached again, print,
// since the list is circular do
{
printf("\nData = %d", temp->info);
temp = temp->next;
} while (temp != last->next);
}
}

// Driver Code int


main()
{
// Function Call
insertAtFront(59);
insertAtFront(28);
insertAtFront(12);

// Print list
viewList();

return 0;
}

Output:

Deletion:

// C program for the above operation #include


<stdio.h>
#include <stdlib.h>

// Structure of a linked list node struct


node {
int info;
struct node* next;
};
// Pointer to last node in list
struct node* last = NULL;

// Function to add a new node


// at the end of the list void
addatlast(int data)
{
// Initialize a new node
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));

// If the new node is the only


// node in the list if
(last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}

// Else the new node will be the


// last node and will contain
// the reference of head node else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}
}

// Function to delete the first


// element of the list
void deletefirst()
{
struct node* temp;

// If list is empty
if (last == NULL)
printf("\nList is empty.\n");

// Else last node now contains


// reference of the second node
// in the list because the
// list is circular else
{
temp = last->next;
last->next = temp->next;
free(temp);
}
}

// Function to print the list


void viewList()
{
// If list is empty
if (last == NULL)
printf("\nList is empty\n");

// Else print the list


else {
struct node* temp;
temp = last->next;
do {
printf("\nData = %d", temp->info);
temp = temp->next;
} while (temp != last->next);
}
}

// Driver Code int


main()
{
// Initialize the list
addatlast(10);
addatlast(20);
addatlast(30);

printf("Before deletion:\n");
viewList();

// Function Call
deletefirst();

// Print list
printf("\n\nAfter deletion:\n");
viewList();

return 0;
}

You might also like