Wednesday 22 February 2017

Creation and manipulation of doubly linked list

Double linked list is a data structure similar to linked list except that the list can be traversed from both sides. Each node in the list contains two pointers one for the previous node and the other for the next node.

//C program to create, insert and delete elements from a doubly linked list
#include<stdio.h>
#include<conio.h>
struct node
{
   struct node *pre,*next;
   int data;
}*head,*newnode;
int count=0;
void create()
{
  newnode=(struct node *)malloc(sizeof(struct node));
  printf("Enter data for the node\n");
  scanf("%d",&newnode->data);
  newnode->next=NULL;
  newnode->pre=NULL;
}
void insert(int pos)
{
  struct node *tmp;
  int i=1;
  create();
  if (pos==1)
  {
    if (head==NULL)
    {
       head=newnode;
    }
    else
    {
       newnode->next=head;
       head->pre=newnode;
       head=newnode;
    }
   }
   else if (pos>count)
   {
 tmp=head;
 while (tmp->next != NULL)
 tmp=tmp->next;
 tmp->next=newnode;
 newnode->pre=tmp;
    }
    else
    {
       tmp=head;
       while (i<pos)
       {
   tmp=tmp->next;
   i++;
       }
       newnode->pre=tmp->pre;
       tmp->pre->next=newnode;
       tmp->pre=newnode;
       newnode->next=tmp;
    }
     count++;
}
void delete(int pos)
{
 struct node *tmp;
 int i=0;
 if(head==NULL)
 {
  printf("Empty list\n");
  exit(0);
 }
 if(pos==1)
 {
  tmp=head;
  head=head->next;
  head->pre=NULL;
  free(tmp);
 }
 else if(pos==count)
 {
  tmp=head;
  while(tmp->next!=NULL)
  {
    tmp=tmp->next;
  }
  tmp->pre->next=NULL;
  free(tmp);
 }
 else
 {
  tmp=head;
  while(i<pos-1)
  {
   tmp=tmp->next;
   i++;
  }
  tmp->next->pre=tmp->pre;
  tmp->pre->next=tmp->next;
  free(tmp);
 }
 count--;
}
void display()
{
   struct node *tmp;
   tmp=head;
   if (tmp==NULL)
   printf("List empty");
   else
   {
      while (tmp != NULL)
      {
  printf("%d->",tmp->data);
  tmp=tmp->next;
      }
   }
   printf("\n");
}
main()
{
  int choice,pos;
  struct node *tmp;
  clrscr();
  head=NULL;
  do
  {
    printf("1. Create\n");
    printf("2. Insert\n");
    printf("3. Delete\n");
    printf("4. Display\n");
    printf("Enter your choice ");
    scanf("%d",&choice);
    switch (choice)
    {
      case 1: create();
       if (head==NULL)
       {
   head=newnode;
   count=1;
       }
       else
       {
    tmp=head;
    while (tmp->next!=NULL)
    tmp = tmp->next;
    tmp->next=newnode;
    newnode->pre=tmp;
    count++;
        }
        break;
       case 2: printf("Enter the position to insert ");
        scanf("%d",&pos);
        insert(pos);
        break;
       case 3:printf("Enter position to delete ");
       scanf("%d",&pos);
       delete(pos);
       break;
       case 4: display();
  break;
       default: exit(0);
    }
   }while(1);
}
/*
OUTPUT
--------
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 1
Enter data for the node
1
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 1
Enter data for the node
2
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 4
1->2->
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 4
1->2->
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 2
Enter the position to insert 1
Enter data for the node
0
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 4
0->1->2->
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 3
Enter position to delete 1
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 4
1->2->
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 3
Enter position to delete 2
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 4
1->
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 5
*/

Thursday 16 February 2017

Program using array of structures

Array of structure is a way to process a group of related data. Define a structure to represent the general structure of the data and then declare an array variable of type structure. This way a group of records of related data can be processed easily.

//C program to process employee records
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct employee
{
     int id;
     char name[20],address[100],job[50];
     float salary;
}e[10];

main()
{
     int n,i;
     struct employee getdata();
    
     clrscr();
     printf("Enter the number of details required\t");
     scanf("%d",&n);
     for(i=0;i<n;i++)
    {
         printf("Enter employee %d details\n",i+1);
         e[i]=getdata();
    }
    for(i=0;i<n;i++)
    {
        printf("Employee %d details",i+1);
        printf("\n----------------------\n");
        display(e[i]);
    }
   getch();
}
struct employee getdata()
{
      struct employee e;
      printf("enter name   ");
      scanf("%s",e.name);
      printf("enter emp_id  ");
      scanf("%d",&e.id);
      printf("enter job  ");
      scanf("%s",e.job);
      printf("enter salary  ");
      scanf("%f",&e.salary);
      printf("enter address  ");
      scanf("%s",e.address);
      return e;
 }
 display(struct employee e)
 {
     printf("Name: %s\n",e.name);
     printf("Emp-id: %d\n",e.id);
     printf("Job: %s\n",e.job);
     printf("Salary: %6.2f\n",e.salary);
     printf("Address: %s\n",e.address);
 }
 /*
 OUTPUT
 -------
Enter the number of details required   2
Enter employee 1 details
enter emp_id  1
enter job  SSA
enter salary  400000
enter address  SG
Enter employee 2 details
enter name   sasha
enter emp_id  2
enter job  AP
enter salary  444444
enter address  IN
Employee 1 details
----------------------
Name: shan
Emp-id: 1
Job: SSA
Salary: 400000.00
Address: SG
Employee 2 details
----------------------
Name: sasha
Emp-id: 2
Job: AP
Salary: 444444.00
Address: IN
*/

Monday 13 February 2017

C program using structure

Structure is an extended data type in C. Structure is a collection of related data of different data types. The following program shows how to work with complex numbers using structure in C.

#include<stdio.h>
#include<conio.h>
struct complex
{
 int r,i;
};
struct complex c1,c2,c3;
main()
{
 clrscr();
 printf("Enter the first complex number\n");
 scanf("%d%d",&c1.r,&c1.i);
 printf("The first complex number is\n");
 printf("%d+i%d\n",c1.r,c1.i);
 printf("Enter the second complex number\n");
 scanf("%d%d",&c2.r,&c2.i);
 printf("The second complex number is\n");
 printf("%d+i%d\n",c2.r,c2.i);
 c3.r=c1.r+c2.r;
 c3.i=c1.i+c2.i;
 printf("The result of addition of two complex numbers is\n");
 printf("%d+i%d\n",c3.r,c3.i);
 c3.r=(c1.r*c2.r)-(c1.i*c2.i);
 c3.i=(c1.i*c2.r)+(c1.r*c2.i);
 printf("The result of multiplication of two complex numbers is\n");
 printf("%d+i%d\n",c3.r,c3.i);
 getch();
}
*/
OUTPUT:
_______
Enter the first complex number
4 6
The first complex number is
4+i6
Enter the second complex number
1 9
The second complex number is
1+i9
The result of addition of two complex numbers is
5+i15
The result of multiplication of two complex numbers is
-50+i42                                                                               
                                                                            

Wednesday 8 February 2017

C program to count number of lines, words and characters in a file

File handling is an important concept in any programming language. The data needed to run a program can be stored permanently in a file and can be taken as input to the program from the file at any time. The following program reads any given file and displays the number of lines, words and characters contained in the file.

//Program to count number of characters, words and lines in a file
#include<stdio.h>
#include<conio.h>
main()
{
  FILE *fp;
  int cc=0,wc=0,lc=0;
  char c,pc='\0',*fname;

  clrscr();
 /* if (argc<2)
  {
    printf("Less number of arguments");
    exit();
  }              */
  printf("Enter the file name\n");
  scanf("%s",fname);
  fp=fopen(fname,"r");
  if (fp==NULL)
  {
     printf("Error in opening the file");
     exit();
  }
  while ((c=fgetc(fp))!=EOF)
  {
     cc++;
     if (cc==1)
     lc=1;
     if (c=='\n')
     lc++;
     if ((c==' ' || c=='\n' ) && (pc!=' ' && pc!='\n'))
     wc++;
     pc=c;
   }
   if (c==EOF && pc!='\n')
   wc++;
   printf("The file  contains %d lines, %d words and %d characters",lc,wc,cc);
   getch();
}
/*
test.dat
---------
hello
   welcome hai
how are you


OUTPUT
---------
Enter the file name
test.dat
The file  contains 3 lines, 6 words and 32 characters
*/