0% found this document useful (0 votes)
24 views12 pages

Exp.1.a - Singly Linked List Implementation

Uploaded by

kannangr8523
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)
24 views12 pages

Exp.1.a - Singly Linked List Implementation

Uploaded by

kannangr8523
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/ 12

Exp 1.a.

SINGLY LINKED LIST IMPLEMENTATION

SINGLY LINKED LIST IMPLEMENTATION

AIM
To implement singly linked list operations such as create, display, insertion, deletion

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void create();
void display();
void insert_beg();
void insert_pos();
void insert_end();
void delete_beg();
void delete_end();
void delete_pos();
1
Exp 1.a. SINGLY LINKED LIST IMPLEMENTATION
struct node *head= NULL;
struct node
{
int data;
struct node *next;
};
int main()
{
int choice;
clrscr();
while(1)
{
printf(" 1.Create ");
printf("\n 2.Display");
printf("\n 3.Insert");
printf("\n 4.Delete");
printf("\n 5. Exit");
printf("\nEnter your choice : ");
scanf("%d",&choice);
2
Exp 1.a. SINGLY LINKED LIST IMPLEMENTATION
switch(choice)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert_beg();
break;
case 4: insert_pos();
break;
case 5: insert_end();
break;
case 6: delete_beg();
break;
case 7: delete_end();
break;
case 8: delete_pos();
break;
case 9: exit(0);
3
Exp 1.a. SINGLY LINKED LIST IMPLEMENTATION
default:printf("wrong choice");
break;
}
}
}

void create( ) //Creation of a node in a linkedlist


{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter Data : ");
scanf("%d",&temp->data);
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
4
Exp 1.a. SINGLY LINKED LIST IMPLEMENTATION
struct node *ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
}
}
void display( ) // Displaying the data in the linked list
{
struct node *ptr=head;
if(head==NULL)
{
printf("Linked list is empty");
return;
}
printf("Linked List: \n");
while(ptr!=NULL)
{
5
Exp 1.a. SINGLY LINKED LIST IMPLEMENTATION
printf("%d\t",ptr->data);
ptr=ptr->next;
}
printf("\n");
getch();
}
void insert_beg( ) // Insertion of node at the beginning of the linked list
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter data : ");
scanf("%d",&temp->data);
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
6
Exp 1.a. SINGLY LINKED LIST IMPLEMENTATION
temp->next=head;
head=temp;
}
}

void insert_pos() // Insertion of node at the given position


{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter the data :");
scanf("%d",&temp->data);
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
7
Exp 1.a. SINGLY LINKED LIST IMPLEMENTATION
struct node *prev_ptr;
struct node *ptr=head;
int i,pos;
printf("Enter position : ");
scanf("%d",&pos);
for(i=0;i<pos;i++)
{
prev_ptr=ptr;
ptr=ptr->next;
}
temp->next=ptr;
prev_ptr->next=temp;
}
}
void insert_end() // Insertion of node at the end of the linked list
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the data : ");
8
Exp 1.a. SINGLY LINKED LIST IMPLEMENTATION
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;
}
}

void delete_beg() // Deletion of node at the beginning of the linked list


if(head==NULL)
9
Exp 1.a. SINGLY LINKED LIST IMPLEMENTATION
{
printf("Linked list is empty");
}
else
{
struct node *ptr=head;
head=ptr->next;
free(ptr);
printf("Node Deleted");
}
}
void delete_end() // Deletion of node at the end of the linked list
{
if(head==NULL)
{
printf("Linked list is empty");
}
else
{
10
Exp 1.a. SINGLY LINKED LIST IMPLEMENTATION
struct node *prev_ptr;
struct node *ptr=head;
while(ptr->next!=NULL)
{
prev_ptr=ptr;
ptr=prev_ptr->next;
}
prev_ptr->next=NULL;
free(ptr);
printf("Node deleted");
getch();
}
}
void delete_pos() // deletion of node at the given position
{
if(head==NULL)
{
printf("Linked list is empty");
}
11
Exp 1.a. SINGLY LINKED LIST IMPLEMENTATION
else
{
struct node *prev_ptr;
struct node *ptr=head;
int i,pos;
printf("Enter the pos: ");
scanf("%d",&pos);
for(i=0;i<pos;i++)
{
prev_ptr=ptr;
ptr=prev_ptr->next;
}
prev_ptr->next=ptr->next;
free(ptr);
printf("Node Deleted");
getch();
}
}

12

You might also like