Skip to main content

C programs QnA

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 ; }

C-Program of Singly Linked List



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.

Don't just read, run on your pc !!!


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

Popular posts from this blog

C-program of printing "Hello World", 10 times on the screen.

In this C program we will print "Hello World", 10 times on the screen. We will use "body-less-loop" for this C-Program. "Body-less-loop" : it means that the inner part(code block to be executed) of the loop will be empty and the loop will have a semi-colon(;) in it's end. i.e: for ( .....  ; .....  ; .....  ) ; input: Null. output: "Hello World" will be printed on the screen, 10 times. CODE----> #include<stdio.h> int main() {      int i;      for( i=1 ; i<=10 ;printf( "   Hello World ! \n ", i++ )) ;      return 0 ; } Don't just read, write it, run it..... RESULT :

Calculate sum of 5 numbers using Array in C-language

In this C -program we will scan the numbers using array and then we will calculate the sum of the given numbers, also using array. The numbers will be taken from the user. input: 5 numbers.(i.e : 5,6,9,56,548) output: The sum of the given numbers will be printed on the screen. CODE----> #include<stdio.h> #include<stdlib.h> main() { int arr[5],i,sum=0; printf ("       Enter Five numbers : "); for ( i=0 ; i<5 ; i++ ) {       scanf (" %d ",&arr[i]); } for ( i=0 ; i<5 ; i++ ) {      sum=sum+arr[i]; } printf (" \n      The sum of the given 5 numbers is : %d\n ",sum); system (" pause "); } Don't just read, write it, run it..... RESULT :

Linear Queue Program in C-Language

In this C program we will perform operations over Linear Queue ( i.e: Data insert Operation, Data remove 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 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<stdlib.h> #define MAXSIZE 10 int Q [ MAXSIZE ], front =- 1 , rear =- 1 ; void qinsert ( int x ) { if ( rear == MAXSIZE - 1 ) printf ( "\n Queue is Full." ); else if ( front ==- 1 ) { front = 0 ; rear = 0 ; Q [ front ]= x ; } else { rear ++; Q [ rear ]= x ; } } void qdelete () { if ( front ==- 1 ) printf ( "\n Queue is Empty." ); else if ( front == rear ) { printf ( "