Singly Linked List Program
Linked List Program - Singly Linked List
Program -
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int item;
struct Node *next;
};
void insertAtLast(struct Node **s, int data)
{
struct Node *t;
struct Node *n = (struct Node*)malloc(sizeof(struct Node));
n->item = data;
n ->next = NULL;
if(*s == NULL)
*s = n;
else
{
t = *s;
while(t->next != NULL)
t= t->next;
t->next = n;
}
}
void insertAtFirst(struct Node **s, int data)
{
struct Node *t;
struct Node *n = (struct Node*)malloc(sizeof(struct Node));
n->item = data;
n->next = *s;
*s = n;
}
void deleteFirst(struct Node **s)
{
struct Node *t;
if(*s==NULL)
printf("\nDeletion Error:List is empty");
else
{
t=*s;
*s = t->next;
free(t);
}
}
void insertAfter(struct Node *temp, int data)
{
struct Node *n = (struct Node*)malloc(sizeof(struct Node));
n->item = data;
n->next = temp->next;
temp->next = n;
}
struct Node* findElement(struct Node *start,int data)
{
if(start==NULL)
printf("\n List is empty");
else
{
while(start->next !=NULL)
{
if(start->item == data)
return start;
start = start ->next;
}
return NULL;
}
}
void deleteLast(struct Node **s)
{
struct Node *t1,*t2;
if(*s==NULL)
printf("\nDeletion Error: List is empty");
else
{
t1=NULL;
t2=*s;
while(t2->next != NULL)
{
t1=t2;
t2=t2->next;
}
if(t1==NULL)
*s=NULL;
else
t1->next = NULL;
free(t2);
}
}
void viewList(struct Node *start)
{
while(start!=NULL)
{
printf("%d ",start->item);
start=start->next;
}
}
void deleteNode(struct Node **s,struct Node *temp)
{
struct Node *t;
if(*s==NULL)
printf("\nDeletion Error: List is empty");
else if(*s==temp)
deleteFirst(s); // call delete first if temp is starting node
else
{
t=*s;
while(t->next!=temp)
{
t = t->next;
}
if(t->next == temp)
deleteLast(s);
else
{
t->next = temp->next;
}
free(temp);
}
}
int menu()
{
int choice;
printf("\nSingly linked list demonstration\n");
printf("\n1.Insert Item as first node");
printf("\n2.Insert Item as last node");
printf("\n3.Insert Item after given node");
printf("\n4.Delete first node");
printf("\n5.Delete last node");
printf("\n6.Delete particular node");
printf("\n7.Exit");
printf("\nEnter your choice\n");
scanf("%d",&choice);
return choice;
}
int main()
{
struct Node *start = NULL;
int element,data;
struct Node *temp;
while(1)
{
system("clear");
printf("Data: ");
viewList(start);
printf("\n");
switch(menu())
{
case 1: printf("\nEnter the item to be inserted at first\n");
scanf("%d",&element);
insertAtFirst(&start,element);
break;
case 2:
printf("\nEnter the item to be inserted at last\n");
scanf("%d",&element);
insertAtLast(&start,element);
break;
case 3:
printf("\nFind the element first\n");
printf("\nEnter the element\n");
scanf("%d",&element);
printf("\nEnter the data to be inserted after");
scanf("%d",&data);
temp= findElement(start,element);
if(temp!=NULL)
insertAfter(temp,data);
break;
case 4:
printf("\nDeleting first list item");
deleteFirst(&start);
break;
case 5:
printf("\nDeleting last list item");
deleteLast(&start);
break;
case 6:
printf("\nEnter the element to be deleted\n");
scanf("%d",&element);
temp = findElement(start,element);
if(temp!=NULL)
deleteNode(&start,temp);
break;
case 7: exit(0);
break;
default:
printf("\nEnter valid choice\n");
}
getchar();
}
return 0;
}
#include<stdlib.h>
struct Node
{
int item;
struct Node *next;
};
void insertAtLast(struct Node **s, int data)
{
struct Node *t;
struct Node *n = (struct Node*)malloc(sizeof(struct Node));
n->item = data;
n ->next = NULL;
if(*s == NULL)
*s = n;
else
{
t = *s;
while(t->next != NULL)
t= t->next;
t->next = n;
}
}
void insertAtFirst(struct Node **s, int data)
{
struct Node *t;
struct Node *n = (struct Node*)malloc(sizeof(struct Node));
n->item = data;
n->next = *s;
*s = n;
}
void deleteFirst(struct Node **s)
{
struct Node *t;
if(*s==NULL)
printf("\nDeletion Error:List is empty");
else
{
t=*s;
*s = t->next;
free(t);
}
}
void insertAfter(struct Node *temp, int data)
{
struct Node *n = (struct Node*)malloc(sizeof(struct Node));
n->item = data;
n->next = temp->next;
temp->next = n;
}
struct Node* findElement(struct Node *start,int data)
{
if(start==NULL)
printf("\n List is empty");
else
{
while(start->next !=NULL)
{
if(start->item == data)
return start;
start = start ->next;
}
return NULL;
}
}
void deleteLast(struct Node **s)
{
struct Node *t1,*t2;
if(*s==NULL)
printf("\nDeletion Error: List is empty");
else
{
t1=NULL;
t2=*s;
while(t2->next != NULL)
{
t1=t2;
t2=t2->next;
}
if(t1==NULL)
*s=NULL;
else
t1->next = NULL;
free(t2);
}
}
void viewList(struct Node *start)
{
while(start!=NULL)
{
printf("%d ",start->item);
start=start->next;
}
}
void deleteNode(struct Node **s,struct Node *temp)
{
struct Node *t;
if(*s==NULL)
printf("\nDeletion Error: List is empty");
else if(*s==temp)
deleteFirst(s); // call delete first if temp is starting node
else
{
t=*s;
while(t->next!=temp)
{
t = t->next;
}
if(t->next == temp)
deleteLast(s);
else
{
t->next = temp->next;
}
free(temp);
}
}
int menu()
{
int choice;
printf("\nSingly linked list demonstration\n");
printf("\n1.Insert Item as first node");
printf("\n2.Insert Item as last node");
printf("\n3.Insert Item after given node");
printf("\n4.Delete first node");
printf("\n5.Delete last node");
printf("\n6.Delete particular node");
printf("\n7.Exit");
printf("\nEnter your choice\n");
scanf("%d",&choice);
return choice;
}
int main()
{
struct Node *start = NULL;
int element,data;
struct Node *temp;
while(1)
{
system("clear");
printf("Data: ");
viewList(start);
printf("\n");
switch(menu())
{
case 1: printf("\nEnter the item to be inserted at first\n");
scanf("%d",&element);
insertAtFirst(&start,element);
break;
case 2:
printf("\nEnter the item to be inserted at last\n");
scanf("%d",&element);
insertAtLast(&start,element);
break;
case 3:
printf("\nFind the element first\n");
printf("\nEnter the element\n");
scanf("%d",&element);
printf("\nEnter the data to be inserted after");
scanf("%d",&data);
temp= findElement(start,element);
if(temp!=NULL)
insertAfter(temp,data);
break;
case 4:
printf("\nDeleting first list item");
deleteFirst(&start);
break;
case 5:
printf("\nDeleting last list item");
deleteLast(&start);
break;
case 6:
printf("\nEnter the element to be deleted\n");
scanf("%d",&element);
temp = findElement(start,element);
if(temp!=NULL)
deleteNode(&start,temp);
break;
case 7: exit(0);
break;
default:
printf("\nEnter valid choice\n");
}
getchar();
}
return 0;
}
Comments
Post a Comment