//COMPLETE LINEAR LINKED LIST---
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node*next;
};
struct node*head=NULL;
//1. Traversal
void traversal(){
if(head==NULL)
{
printf("Linked list is empty!\n");
}
else
{
struct node*p=head;
while(p!=NULL)
{
printf("%d->",p->data);
p=p->next;
}
printf("NULL\n");
}
}
//2. Add in first
void addinfirst(){
int val;
printf("Enter the value: ");
scanf("%d", &val);
struct node*ptr = (struct node*)malloc(sizeof(struct node));
ptr -> data =val;
ptr -> next =NULL;
if(head == NULL){
head = ptr;
}
else{
ptr -> next =head;
head = ptr;
}
}
//3. Add in last
void addinlast(){
int val;
printf("Enter the value: ");
scanf("%d", &val);
struct node*ptr = (struct node*)malloc(sizeof(struct node));
ptr -> data =val;
ptr -> next =NULL;
if(head == NULL){
head = ptr;
}
else{
struct node*p = head;
while(p->next!=NULL){
p = p-> next;
}
p -> next =ptr;
}
}
//4. Add in position
void addinposition(){
int pos, val;
printf("Enter the position : ");
scanf("%d",&pos);
struct node* ptr = (struct node*) malloc (sizeof(struct node));
printf("Enter the value : ");
scanf("%d",&val);
ptr->data = val;
ptr->next = NULL;
if(head==NULL)
{
if(pos==1)
head=ptr;
else
printf("Linked list is empty, not possible\n");
}
else if(pos==1)
{
ptr->next=head;
head=ptr;
}
else
{
struct node*p = head,*q;
int count=0;
while(count<pos-2 && p!=NULL)
{
p=p->next;
count++;
}
if(p==NULL){
printf("Position is out of bound!\n");
}
else
{
q=p->next;
p->next=ptr;
ptr->next=q;
}
}
}
//5. Add before a value
void addbeforevalue(){
if(head==NULL){
printf("The linked list is empty\n");
}
else{
int v, val;
printf("Enter the value : ");
scanf("%d",&v);
struct node* ptr = (struct node*) malloc (sizeof(struct node));
printf("Enter the data of the node: ");
scanf("%d",&val);
ptr->data = val;
ptr->next = NULL;
if(head->data == v)
{
ptr->next = head;
head = ptr;
}
else{
struct node*p = head,*q;
while( p->next!=NULL && p->next->data!=v)
{
p=p->next;
}
if(p->next==NULL)
printf("The value is not there in the list\n");
else
{
q=p->next;
p->next=ptr;
ptr->next=q;
}
}
}
}
//6. Add after value
void addaftervalue(){
if(head==NULL){
printf("The linked list is empty\n");
}
else{
int v, val;
printf("Enter the value in the linked list: ");
scanf("%d",&v);
struct node* ptr = (struct node*) malloc (sizeof(struct node));
printf("Enter the value: ");
scanf("%d",&val);
ptr->data = val;
ptr->next = NULL;
struct node*p = head;
while(p!=NULL && p->data!=v )
{
p=p->next;
}
if(p==NULL)
printf("The value is not in the list\n");
else
{
ptr->next=p->next;
p->next=ptr;
}
}
}
//7. Delete from first
void deletefromfirst() {
if (head == NULL) {
printf("Linked list is empty, deletion is not possible!\n");
} else {
struct node* p = head;
head = head->next;
printf("%d is deleted from the list\n", p->data);
free(p);
}
}
//8. Delete from last
void deletefromlast() {
if (head == NULL) {
printf("Linked list is empty, deletion is not possible!\n");
}
else if (head->next == NULL) {
struct node* p = head;
head = NULL;
printf("%d is deleted from the list\n", p->data);
free(p);
}
else {
struct node* p = head,*q;
while (p->next->next != NULL) {
p = p->next;
}
q = p->next;
p->next = NULL;
printf("%d is deleted from the list\n", q->data);
free(q);
}
}
//9. Delete value
void deletevalue() {
if (head == NULL) {
printf("Linked list is empty, deletion is not possible\n");
}
else{
int val;
printf("Enter the value of the node to delete: ");
scanf("%d", &val);
if (head->data == val) {
struct node* p = head;
head = head->next;
printf("%d is deleted from the list\n", p->data);
free(p);
return;
}
else{
struct node* p = head,*q;
while (p->next != NULL && p->next->data != val) {
p = p->next;
}
if (p-> next== NULL) {
printf("The value %d is not in the list\n", val);
}
else {
q = p->next;
p->next = q->next;
printf("%d is deleted from the list\n", q->data);
free(q);
}
}
}
}
//10. Delete before value
void deletebeforevalue(){
if(head==NULL){
printf("The linked list is empty\n");
}
else{
int val;
printf("Enter the value: ");
scanf("%d", &val);
struct node*p = head,*q;
if(head->next->data==val){
head = head->next;
free(p);
}
else{
while(p->next->next!=NULL && p->next->next->data!=val){
p= p->next;
}
if(p->next->next==NULL){
printf("The value is not in the list\n");
}
else{
q=p->next;
p->next=q->next;
free(q);
}
}
}
}
//11. Delete after value
void deleteaftervalue(){
if(head==NULL){
printf("The linked list is empty\n");
}
else{
int val;
printf("Enter the value: ");
scanf("%d", &val);
struct node*p = head,*q,*r;
if(head->next==NULL){
printf("There is only one value in the linked list\n");
}
else{
while(p->next->data!=val){
p= p->next;
}
if(p->next==NULL){
printf("The value is not in the list\n");
}
else{
q=p->next;
p->next=q->next;
free(q);
}
}
}
}
//12. Delete from position
void deletefromposition(){
int pos;
printf("Enter the position : ");
scanf("%d",&pos);
struct node*p = head,*q;
if(head==NULL)
{
if(pos==1){
head=p;
free(p);
}
else{
printf("Linked list is empty, not possible\n");
}
}
else
{
int count=0;
while(count<pos-2 && p!=NULL)
{
p=p->next;
count++;
}
if(p==NULL)
printf("Position is out of bound!\n");
else
{
q=p->next;
p->next=q->next;
free(q);
}
}
}
//13. Count the number of nodes present in a linked list
void coutnodes(){
int count=0;
struct node*p = head;
if(head==NULL){
printf("The number of nodes available in the linked list is 0\n");
}
else{
while(p!=NULL){
count++;
p = p->next;
}
printf("The number of nodes available in the linked list is: %d\n",count);
}
}
//14. Sum of all values in the linked list
void sumofallvalues(){
int sum=0;
struct node*p = head;
if(head==NULL){
printf("There is no number available in the linked list, the sum is 0\n");
}
else if(head->next==NULL){
printf("The sum is: %d\n",head->data);
}
else{
while(p!=NULL){
sum+=p->data;
p = p->next;
}
printf("The sum is: %d\n",sum);
}
}
//15. Count the number of even and add numbers present in a list
void even_odd_cout(){
int even_count=0, odd_count=0;
struct node*p = head;
if(head==NULL){
printf("The linked list is empty\n");
}
else{
while(p!=NULL){
if(p->data%2==0){
even_count++;
}
else{
odd_count++;
}
p = p->next;
}
printf("The number of odd value in the list is: %d\n",odd_count);
printf("The number of even value in the list is: %d\n",even_count);
}
}
//16. Search for an element
void search(){
if(head==NULL){
printf("The linked list is empty\n");
}
else{
int val;
printf("Enter the value: ");
scanf("%d",&val);
struct node*p=head;
while(p!=NULL){
if(p->data==val){
printf("Present\n");
return;
}
p=p->next;
}
printf("Not Present\n");
}
}
//17. Sort the list
void sort(){
struct node*start=head;
while(start->next!=NULL)
{
struct node* p=start->next;
while(p!=NULL)
{
if(start->data > p->data)
{
int temp=start->data;
start->data=p->data;
p->data=temp;
}
p=p->next;
}
start=start->next;
}
}
//17. Reverse the list
void reverse()
{
if(head==NULL)
{
printf("The linked list is empty\n");
}
else
{
struct node*prev_node=NULL,*curr_node=head,*next_node=NULL;
while(curr_node!=NULL)
{
next_node=curr_node->next; // Store next node
curr_node->next=prev_node; // Reverse the link
prev_node=curr_node; // Move prev_node to current node
curr_node=next_node; // Move to next node
}
head=prev_node; // Update head to the new first node
printf("The linked list has been reversed successfully\n");
}
}
int main()
{
int op;
printf("Enter 0 for Exit\nEnter 1 to Traverse\nEnter 2 to Add in first\nEnter 3 to Add in
last\nEnter 4 to Add in position\nEnter 5 to Add before value\nEnter 6 to Add after value\nEnter 7 to
Delete from first\nEnter 8 to Delete from last\nEnter 9 to Delete value\nEnter 10 to Delete before
value\nEnter 11 to Delete after value\nEnter 12 to Delete from position\nEnter 13 to Count the
number of nodes present in a linked list\nEnter 14 to find out The sum of all values in the linked
list\nEnter 15 to Count the number of even and add numbers present in a list\nEnter 16 to Search for
an element\nEnter 17 to sort the list\nEnter 18 to Reverse the list\n");
while(1)
{
printf("Enter your choice : ");
scanf("%d",&op);
if(op==0)
{
printf("Khaaataam tata bye bye !!!");
break;
}
else
{
switch(op)
{
case 1: traversal();
break;
case 2: addinfirst();
break;
case 3: addinlast();
break;
case 4: addinposition();
break;
case 5: addbeforevalue();
break;
case 6: addaftervalue();
break;
case 7: deletefromfirst();
break;
case 8: deletefromlast();
break;
case 9: deletevalue();
break;
case 10: deletebeforevalue();
break;
case 11: deleteaftervalue();
break;
case 12: deletefromposition();
break;
case 13: coutnodes();
break;
case 14: sumofallvalues();
break;
case 15: even_odd_cout();
break;
case 16: search();
break;
case 17: sort();
break;
case 18: reverse();
break;
default:
printf("Wrong input\n");
}
}
}
}