Q. If sum of two integer is 14 and if one of them is 8 then find other integer. Ans: Well, this is very simple. Simply, one integer is 8 and sum of two integers is 14. So, we need to subtract the number 8 from their sum of 14 to get the second number. So, the second number is: 14 - 8 = 6 C-Program of this task Code---> #include<stdio.h> int main () { int num1 = 8 , num2 , sum = 14 ; num2 = sum - num1 ; printf ( "The second number is: %d" , num2 ); return 0 ; }
In this C program we will perform operations on Singly Linked List(i.e: Data insert Operations(at begining or at end or at any position), Data remove Operation(from begining or from end or from any position), Data Reverse Operation and display Operation). The Choice(i.e: data insert,remove or display) will be made by the user and The Number will be taken from the user(i.e: For Data insert Operation).
input:
The Choice(i.e Data insert, remove, reverse or display) & The Number (integers) (i.e. for Data insert Operations) (15,10,128 etc.)output:
The Operations will be excecuted as choosen by the user.
CODE---->
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *head=NULL;
node *getnode(int x)
{
node *n;
n=(node *)malloc(sizeof(node));
n->data=x;
n->next=NULL;
return (n);
}
void createlist()
{
int x;
node *n,*ptr;
printf("\n Enter element : ");
scanf("%d",&x);
n=getnode(x);
if(head==NULL)
head=n;
else
{
ptr=head;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=n;
}
}
void insertbeg()
{
int x;
node *n;
printf("\n Enter element : ");
scanf("%d",&x);
n=getnode(x);
if(head==NULL)
head=n;
else
{
n->next=head;
head=n;
}
}
void insertmid()
{
int x,pos,c;
node *n,*p1,*p2;
printf("\n Enter element : ");
scanf("%d",&x);
n=getnode(x);
printf("\n Enter Position : ");
scanf("%d",&pos);
if(pos==1)
{
n->next=head;
head=n;
}
else
{
c=1;
p1=head;
while(c<pos&&p1!=NULL)
{
p2=p1;
p1=p1->next;
c++;
}
if(p1==NULL&&c!=pos)
printf("\n Wrong position.");
else
{
n->next=p1;
p2->next=n;
}
}
}
void insertend()
{
int x;
node *n,*ptr;
printf("\n Enter element : ");
scanf("%d",&x);
n=getnode(x);
if(head==NULL)
head=n;
else
{
ptr=head;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=n;
}
}
void delbeg()
{
node *ptr;
if(head==NULL)
printf("\n No Linked List Exists.");
else
{
ptr=head;
head=head->next;
free(ptr);
printf("\n First node deleted successfully.");
}
}
void delend()
{
node *p,*q;
if(head==NULL)
printf("\n No Linked List Exists.");
else
{
p=head;
while(p->next!=NULL)
{
q=p;
p=p->next;
}
q->next=NULL;
free(p);
printf("\n Last node deleted successfully.");
}
}
void delmid()
{
int pos,c;
node *p,*q;
printf("\n Enter position : ");
scanf("%d",&pos);
if(head==NULL)
printf("\n No Linked List Exists.");
else if(pos==1)
{
p=head;
head=head->next;
free(p);
}
else
{
c=1;
p=head;
while(c<pos&&p!=NULL)
{
q=p;
p=p->next;
c++;
}
if(p==NULL&&c!=pos)
printf("\n Wrong position.");
else
{
q->next=p->next;
p->next=NULL;
free(p);
printf("\n The node at position %d is deleted successfully.",pos);
}
}
}
void reverse()
{
node *p,*q,*r;
if(head==NULL||head->next==NULL)
printf("\n Reverse is not possible.");
else
{
p=q=r=head;
p=p->next->next;
q=q->next;
r->next=NULL;
q->next=r;
while(p!=NULL)
{
r=q;
q=p;
p=p->next;
q->next=r;
}
head=q;
printf("\n Linked List Reversed successfully.");
}
}
void display()
{
node *ptr;
if(head==NULL)
printf("\n No Linked List Exists.");
else
{
printf("\n The element(s) of the list are...\n");
ptr=head;
while(ptr!=NULL)
{
printf(" %d",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
}
void main()
{
int choice;
while(1)
{
printf("\n==========Menu==========\n");
printf("\n1.Creation of Linked List\n2.Insert node at begining\n3.Insert node at end\n4.Insert node at any position");
printf("\n5.Delete node from Begining\n6.Delete node from End\n7.Delete node from any Position");
printf("\n8.Reverse the Linked List\n9.Display\n10.Exit");
printf("\n\n Enter Your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: createlist();
break;
case 2: insertbeg();
break;
case 3: insertend();
break;
case 4: insertmid();
break;
case 5: delbeg();
break;
case 6: delend();
break;
case 7: delmid();
break;
case 8: reverse();
break;
case 9: display();
break;
case 10: exit(0);
default : printf("\n Wrong choice.");
}
}
}
Download the C-Program file of this Program.
RESULT{for 1.Creation} :
* The Display function is called at last for displaying the stored data.
1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 1 Enter element : 10 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 1 Enter element : 20 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 1 Enter element : 30 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 1 Enter element : 40 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 9 The element(s) of the list are... 10 20 30 40
RESULT{for 2.Insert at begining} :
* The Display function is called at last for displaying the stored data.
==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 2 Enter element : 10 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 2 Enter element : 20 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 2 Enter element : 30 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 2 Enter element : 40 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 9 The element(s) of the list are... 40 30 20 10
RESULT{for 3.Insert at End} :
* The Display function is called at last for displaying the stored data.
==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 3 Enter element : 10 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 3 Enter element : 20 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 3 Enter element : 30 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 3 Enter element : 40 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 3 Enter element : 50 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 3 Enter element : 60 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 9 The element(s) of the list are... 10 20 30 40 50 60
RESULT{for 4.Insert at any position} :
* The Display function is called at last for displaying the stored data.
==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 4 Enter element : 10 Enter Position : 1 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 4 Enter element : 20 Enter Position : 2 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 4 Enter element : 5 Enter Position : 1 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 4 Enter element : 15 Enter Position : 3 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 9 The element(s) of the list are... 5 10 15 20
RESULT{for 5.Delete from begining} :
* The Display function is called at last for displaying the stored data.
The element(s) of the list are... 10 20 30 40 50 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 5 First node deleted successfully. ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 9 The element(s) of the list are... 20 30 40 50
RESULT{for 6.Delete from end} :
* The Display function is called at last for displaying the stored data.
The element(s) of the list are... 20 30 40 50 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 6 Last node deleted successfully. ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 9 The element(s) of the list are... 20 30 40
RESULT{for 7.Delete from any position} :
* The Display function is called at last for displaying the stored data.
The element(s) of the list are... 20 30 40 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 7 Enter position : 2 The node at position 2 is deleted successfully. ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 9 The element(s) of the list are... 20 40
RESULT{for 8.Reverse the Linked List} :
* The Display function is called at last for displaying the stored data.
The element(s) of the list are... 10 20 30 40 50 ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 8 Linked List Reversed successfully. ==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 9 The element(s) of the list are... 50 40 30 20 10
RESULT{for 9.Display} :
==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 9 The element(s) of the list are... 10 20 30 40 50
RESULT{for 10.Exit} :
* The Display function is called at last for displaying the stored data.
==========Menu========== 1.Creation of Linked List 2.Insert node at begining 3.Insert node at end 4.Insert node at any position 5.Delete node from Begining 6.Delete node from End 7.Delete node from any Position 8.Reverse the Linked List 9.Display 10.Exit Enter Your choice : 10 -------------------------------- Process exited after 286.3 seconds with return value 0 Press any key to continue . . .
Images for better understanding :
Comments
Post a Comment