Showing posts with label CS6212 – PROGRAMMING AND DATA STRUCTURES I LAB. Show all posts
Showing posts with label CS6212 – PROGRAMMING AND DATA STRUCTURES I LAB. Show all posts

Monday, February 23, 2015

Addition of Polynomials Using Linked list

AIM:
            To write a C program to perform the addition of two given polynomials using   
           linked list.

ALGORITHM:
            Step 1: Start the program.
            Step 2: Declare the structure with two data fields; one coefficient field, one
exponent field and one pointer to point the next element in the linked list.
Step 3: Get the coefficients and exponents of polynomials as input.
 Step 4: For the given polynomials create node for each term using malloc and               
            link each term with the next term of the same polynomial.
Step 5: Point last term of each polynomial to NULL.
Step 6: Compare the exponent terms of the polynomials one by one.
i) If they are same add the respective coefficients and then create node and
place them in the resultant polynomial
ii) Else if they do not match with any term then place the term as such in the resultant polynomial.
 Step 7: Display the resultant polynomial
 Step 8: Stop the program.

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<alloc.h>
#include<stdlib.h>
typedef struct polynode
{
float coef;
int exp;
struct polynode *next;
}p;
void main()
{
p *p1,*p2,*p3;
p *getpoly();
p *add(p*,p*);
void display(p*);
clrscr();
printf("\n Enter the 1st polynomial:");
p1=getpoly();
clrscr();
printf("\n Enter the 2nd polynomial:");
p2=getpoly();
clrscr();
printf("\n The 1st poly is:");
display(p1);
printf("\n The 2nd poly is:");
display(p2);
p3=add(p1,p2);
printf("\nAddition of polynomial is:");
display(p3);
exit(0);
}
p *getpoly()
{
p *temp,*New,*last;
int Exp,flag;
float Coef;
p *getnode();
char ans='y';
temp=NULL;
flag=1;
printf("\n Enter the polynomial in descending order of exponent:");
do
{
printf("\nEnter the coeff and expo of a term:");
scanf("%f%d",&Coef,&Exp);
New=getnode();
if(New==NULL)
printf("\n Memory not allocated:");
New->coef=Coef;
New->exp=Exp;
if(flag==1)
{
temp=New;
last=temp;
flag=0;
}
else
{
last->next=New;
last=New;
}
printf("\nDo you want to add more terms(y/n)?");
ans=getch();
}while(ans=='y');
return(temp);
}
p *getnode()
{
p *temp;
temp=(p*)malloc(sizeof(p));
temp->next=NULL;
return(temp);
}

Sunday, February 22, 2015

Implementation of Doubly Linked List

AIM:
            To write a C program to implement doubly linked list.

ALGORITHM:
            Step 1: Define a list as a node of a structure with one data, one pointer
                        pointing to next element in the structure and another pointer pointing to
previous element in the structure.
            Step 2: Declare the function prototypes creation (), insertion (), deletion () and
display () to perform the list function.
Step 3: Declare the necessary pointer variables as structure node data type.
Step 4: Get a choice from the user.
Step 5: If the choice is create get first data from the user by calling the function
create () and display  contents of the list.
Step 6: If the choice is insert, get data and its position by calling function
insert (), assign the next and previous field of the given data node according to the position. Display the contents of the list.
Step 7: If the choice is delete, get the position of data which is going to be
removed from the list and assign next field of previous node to address.
Step 8: Repeat the steps 4, 5&6 until the choice is exit.
Step 9: Terminate the execution.       

PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{                 
int data;
struct node*next,*prev;
}*New,*New1,*temp,*start,*dummy;
void add();
struct node *get_node();
void display();
void Delete();
int find(int num);
int first=1;
void main()
{
char ans='y';
int choice,num,found,flag=0;
start=NULL;
do
{
clrscr();
printf("\nProgram for DLL\n");
printf("\n1.Insert\n2.Delete\n3.Diplay\n4.Search\n5.Exit\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
add();break;
case 2:
Delete();break;
case 3:
display();break;
case 4:
       printf("\nEnter the number to be searched");
       scanf("%d",&num);
       temp=start;
       while((temp!=NULL)&&(flag==0))
       {
       found=find(num);
       flag=found;
       }
       if(found==1)
       printf("\nThe number is present\n");
       else
       printf("\nThe number is not present\n");
       break;
case 5:
       exit(0);break;
default:
            printf("\nInvalid option");break;
}
printf("\nDo you want to continue?(y/n)");
ans=getche();
}while(ans=='y'||ans=='Y');
getch();
}
void add()
{
clrscr();
New=get_node();
printf("\nEnter the element");
scanf("%d",&New->data);
if(first==1)
{
start=New;
first=0;
}
else
{
dummy=start;
while(dummy->next!=NULL)
dummy=dummy->next;
dummy->next=New;
New->prev=dummy;
}
}
struct node *get_node()
{
New1=malloc(sizeof(struct node));
New1->next=NULL;
New1->prev=NULL;
return(New1);
}
void display()
{
clrscr();
temp=start;
if(temp==NULL)
printf("\nThe DLL is empty\n");
else
{
while(temp!=NULL)
{
printf("\n\n%d=>",temp->data);
temp=temp->next;
}
printf("NULL");
}
getch();
}
int find(int num)
{
if(temp->data==num)
return 1;
else
temp=temp->next;
return 0;
}

Saturday, February 21, 2015

Implementation of Binary search

AIM:
          To write a C program to implement Binary search

ALGORITHM:
            Step 1: Start the program.
            Step 2: Read the upper limit of the array as n
Step 3: Setup for loop from i=1 until i<n increment i
Step 4: Read element to array[]
Step 5: Read element to be searched in the array as search
Step 6: Assign first=0,last=n-1
Step 7: Repeat step 8 to 9 until (first<=last)
Step 8: middle=(first+last)/2
Step 9: if(search<a[middle])
            True: n=middle=1
            False : if(search>a[middle])
                        True:first=middle+1
                        False: found the element in middle+1 position
Step 10: Stop the program.

PROGRAM
#include<stdio.h>
#include<conio.h>
 int main()
{
   int c, first, last, middle, n, search, array[100];
   printf("Enter number of elements\n");
   scanf("%d",&n);
   printf("Enter %d integers in ascending order\n", n);
   for ( c = 0 ; c < n ; c++ )
   scanf("%d",&array[c]);
   printf("Enter value to find\n");
   scanf("%d",&search);
   first = 0;
   last = n - 1;
   middle = (first+last)/2;
   while( first <= last )
   {
      if ( array[middle] < search )
         first = middle + 1;   
      else if ( array[middle] == search )
      {
         printf("%d found at location %d.\n", search, middle+1);
         break;
      }
      else
         last = middle - 1;
        middle = (first + last)/2;
   }
   if ( first > last )
   printf("Not found! %d is not present in the list.\n", search);
   getch();
   return 0;  
}

Friday, February 20, 2015

Implementation of Linear Search

AIM:
          To write a C program to implement Linear Search

ALGORITHM:
            Step 1: Start the program.
Step 2: Get the total number of elements to be entered n
Step 3: Setup a for loop c=0 ;until c<n; increment i
Step 4: Read the numbers into the array
Step 5: Read which element to be searched in search
Step 6: Setup a for loop c=0 ;until c<n; increment i
Step 7: Evaluate if(search==array[i])
            If it is found then print the number and its position
Step 8: Stop the program.

PROGRAM
#include <stdio.h>
int main()
{
   int array[100], search, c, n;
  printf("Enter the number of elements in array\n");
  scanf("%d",&n);
  printf("Enter %d integer(s)\n", n);
  for (c = 0; c < n; c++)
  scanf("%d", &array[c]);
   printf("Enter the number to search\n");
   scanf("%d", &search);
   for (c = 0; c < n; c++)
   {
      if (array[c] == search)     /* if required element found */
      {
         printf("%d is present at location %d.\n", search, c+1);
         break;
      }
   }
   if (c == n)
   printf("%d is not present in array.\n", search);
   return 0;
}

Thursday, February 19, 2015

Implementation of Radix Sort

AIM:
          To write a C program to implement radix sort

ALGORITHM:
            Step 1: Start the program.
Step 2: Each key is first figuratively dropped into one level of buckets                                            corresponding to the value of the rightmost digit.
Step 3: Each bucket preserves the original order of the keys as the keys are
            dropped into the bucket.
Step 4: There is a one-to-one correspondence between the number of buckets and
            the number of values that can be represented by a digit.
Step 5: Then, the process repeats with the next neighboring digit until there are no
            more digits to process. In other words:
i)                    Take the least significant digit of each key.
ii)                  Group the keys based on that digit, but otherwise keep the  
original order of keys. 
iii)                Repeat the grouping process with each more significant digit.
Step 6: Stop the program.

PROGRAM
#include<stdio.h>
#include<conio.h>
radix_sort(int array[], int n);
void main()
{
int array[100],n,i;
clrscr();
printf("Enter the number of elements to be sorted: ");
scanf("%d",&n);
printf("\nEnter the elements to be sorted: \n");
for(i = 0 ; i < n ; i++ )
{
printf("\tArray[%d] = ",i);
scanf("%d",&array[i]);
}
printf("\nArray Before Radix Sort:");  //Array Before Radix Sort
for(i = 0; i < n; i++)
{
printf("%8d", array[i]);
}
printf("\n");
radix_sort(array,n);
printf("\nArray After Radix Sort: ");  //Array After Radix Sort
for(i = 0; i < n; i++)
{
printf("%8d", array[i]);
}
printf("\n");
getch();
}
radix_sort(int arr[], int n)
{
int bucket[10][5],buck[10],b[10];
int i,j,k,l,num,div,large,passes;
div=1;
num=0;
large=arr[0];
for(i=0 ; i<n ; i++)
{
if(arr[i] > large)
{
large = arr[i];
}
while(large > 0)
{
num++;
large = large/10;
}

Wednesday, February 18, 2015

Implementation of Shell Sort

AIM:
          To write a C program to implement shell sort

ALGORITHM:
            Step 1: Start the program.
Step 2:  Set up a inc number. inc number is set according to given elements in the   
             list.
            Step 3:  Mark each elements which is comes in inc element.
Step 4: Sort marking elements such as smallest to greater is set as left to right and 
            not change remain element.
Step 5: Reduce inc number to one
            Step 6: Repeat step 2,3 and 4 till all the elements not sorted.
Step 7: Stop the program.

PROGRAM
#include<stdio.h>
#include<conio.h>
void shell(int a[],int n);
int i,j,k,n,temp,array[25];
void main()
{
clrscr();
printf("\n SHELL SORT");
printf("\n **********");
printf("\n Enter the limit:");
scanf("%d",&n);
printf("\n Enter the elements\n\n");
for(i=0;i<n;i++)
scanf("%d",&array[i]);
shell(array,n);
printf("\n Sorted list:");
for(i=0;i<n;i++)
printf("\n %d",array[i]);
getch();
}
void shell(int a[],int n)
{
for(i=(n+1)/2;i>=1;i/=2)
{
for(j=i;j<=n-1;j++)
{
temp=a[j];
k=j-i;
while(k>=0&&temp<a[k])
{
a[k+i]=a[k];
k=k-i;
}
a[k+i]=temp;
}}}

Tuesday, February 17, 2015

Implementation of Merge Sort

AIM:
          To write a C program to implement Merge sort

ALGORITHM:
            Step 1: Start the program.
Step 2: To sort A[p .. r]: Divide Step: If a given array A has zero or one element,
simply return; it is already sorted. Otherwise, split A[p .. r] into two
sub arrays A[p .. q] and A[q + 1 .. r], each containing about half of the
elements of A[p .. r]. That is, q is the halfway point of A[p .. r].
Step 3: Conquer Step: Conquer by recursively sorting the two sub arrays A[p .. q]
            and A[q + 1 .. r].
Step 4: Combine Step : Combine the elements back in A[p .. r] by merging the
            two sorted sub arrays A[p .. q] and A[q + 1 .. r] into a sorted sequence. To
            accomplish this step, we will define a procedure MERGE (A, p, q, r).
Step 5: Stop the program.

PROGRAM
#include<stdio.h>
#define MAX 50
void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);
int main()
{
int merge[MAX],i,n;
printf("Enter the total number of elements: ");
scanf("%d",&n);
printf("Enter the elements which to be sort: ");
for(i=0;i<n;i++)
{
scanf("%d",&merge[i]);
}
partition(merge,0,n-1);
printf("After merge sorting elements are: ");
for(i=0;i<n;i++)
{
printf("%d ",merge[i]);
}
return 0;
}
void partition(int arr[],int low,int high){
int mid;
if(low<high)
{
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}

Monday, February 16, 2015

Implementation of Quick Sort

AIM:
          To write a C program to implement Quick sort

ALGORITHM:
            Step 1: Start the program.
Step 2: Declare the function quicksort(array)
Step 3: less, equal, greater := three empty arrays
Step 4: if length(array) > 1 
Step 5: Then pivot := select any element of array
Step 6: for each x in array
            if x < pivot then add x to less
            if x = pivot then add x to equal
            if x > pivot then add x to greater
        quicksort(less)
        quicksort(greater)
Step 7: array := concatenate(less, equal, greater)
Step 8: Stop the program.

PROGRAM
#include<stdio.h>
void quicksort(int [10],int,int);
int main()
{
int x[20],size,i;
printf("Enter size of the array: ");
scanf("%d",&size);
printf("Enter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("Sorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
return 0;
}
void quicksort(int x[10],int first,int last)
{
int pivot,j,temp,i;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j)
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);

}
}

Sunday, February 15, 2015

Implementation of Bubble Sort

AIM:
          To write a C program to implement bubble sort

ALGORITHM:
            Step 1: Start the program.
Step 2: Bubble sort algorithm starts by comparing the first two elements of an
            array and swapping if necessary,
Step 3: if you want to sort the elements of array in ascending order and if the first
            element is greater than second then, you need to swap the elements but, if
            the first element is smaller than second, you mustn't swap the element.
Step 4: Then, again second and third elements are compared and swapped if it is
necessary and this process go on until last and second last element is compared and swapped. This completes the first step of bubble sort.
Step 5: If there are n elements to be sorted then, the process mentioned above
            should be repeated n-1 times to get required result.
Step 6: Exit

PROGRAM 
#include<stdio.h>
int main()
{
int s,temp,i,j,a[20];
printf("Enter total numbers of elements: ");
scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
//Bubble sorting ALGORITHM
for(i=s-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

printf("After sorting: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
return 0;
}

Saturday, February 14, 2015

Implementation of Selection Sort

AIM:
          To write a C program to implement selection sort

ALGORITHM:
            Step 1: Start the program.
Step 2: Selection sort algorithm starts by comparing first two elements of an array
and swapping if necessary.
Step 3:  If you want to sort the elements of array in ascending order and if the first            element is greater than second then, you need to swap the elements.
Step 4:But, if the first element is smaller than second, leave the elements as it is. Step 5: Then, again first element and third element are compared and swapped if
            necessary.
Step 6: This process goes on until first and last element of an array is compared.
            This completes the first step of selection sort.
Step 7:If there are n elements to be sorted then, the process mentioned above
            should be repeated n-1 times to get required result.
Step 8: Stop the program

PROGRAM
#include<stdio.h>
int main()
{
 int s,i,j,temp,a[20];
 printf("Enter total elements: ");
 scanf("%d",&s);
 printf("Enter %d elements: ",s);
  for(i=0;i<s;i++)
  scanf("%d",&a[i]);
  for(i=0;i<s;i++)
{
      for(j=i+1;j<s;j++)
{
           if(a[i]>a[j])
{
               temp=a[i];
              a[i]=a[j];
              a[j]=temp;
           }    
 } 
}
  printf("After sorting is: ");
  for(i=0;i<s;i++)
  printf(" %d",a[i]);
  return 0;
}

Friday, February 13, 2015

Implementation of Insertion Sort

AIM:
          To write a C program to implement insertion sort

ALGORITHM:
            Step 1: Start the program.
            Step 2: The second element of an array is compared with the elements that
                        appears before it (only first element in this case). If the second element is
                        smaller than first element, second element is inserted in the position of
                        first element. After first step, first two elements of an array will be sorted.
Step 3: The third element of an array is compared with the elements that appears
 before it (first and second element). If third element is smaller than first
element, it is inserted in the position of first element. If third element is
larger than first element but, smaller than second element, it is inserted in
 the position of second element. If third element is larger than both the
elements, it is kept in the position as it is. After second step, first three
elements of an array will be sorted.
Step 3: Similarly, the fourth element of an array is compared with the elements
            that appears before it (first, second and third element) and the same
            procedure is applied and that element is inserted in the proper position.
            After third step, first four elements of an array will be sorted.
Step 4: If there are n elements to be sorted. Then, this procedure is repeated n-
            1 times to get sorted list of array
Step 5: Stop the program

PROGRAM
#include<stdio.h>
int main(){
int i,j,s,temp,a[20];
printf("Enter total elements: ");
scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=1;i<s;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("After sorting: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
return 0;
}

Thursday, February 12, 2015

Implementation of Queue using Linked List

AIM:
          To write a C program to implement queue using linked list

ALGORITHM:
            Step 1: Start the program.
            Step 2: For queue insertion operation, check for queue overflow
Step 3: If R>=N then print queue overflow else increment rear pointer and insert  
            the element.
Step 4: For queue deletion operation, check for underflow of the queue.
Step 5: If F=0 then print queue underflow else delete the element and increment 
            the front pointer
Step 6: Stop the program.

PROGRAM
#include<stdio.h >
#include<conio.h >
#include<alloc.h >
struct queue
{
int data;
struct queue *next;
};
struct queue *addq(struct queue *front);
struct queue *delq(struct queue *front);
void main()
{
struct queue *front;
int reply,option,data;
clrscr();
front=NULL;
do
{
printf("\n1.addq");
printf("\n2.delq");
printf("\n3.exit");
printf("\nSelect the option");
scanf("%d",&option);

Wednesday, February 11, 2015

Implementation of Stack Using Linked List

AIM:
To write a c program to implement stack using linked list

ALGORITHM:
            Step 1: Start the program.
            Step 2: For Push operation, check for stack overflow
Step 3: If Top>=N then print stack overflow else increment Top and insert the 
            element.
Step 4: For Pop operation, check for underflow of the stack.
Step 5: If Top=0 then print stack underflow else decrement Top and delete the
           element
Step 6: Stop the program.

PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int Data;
struct Node *next;
}*top;
void popStack()
{
struct Node *temp, *var=top;
if(var==top)
{
top = top->next;
free(var);
}
else
printf("\nStack Empty");
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (top == NULL)
{
top=temp;
top->next=NULL;
}
else
{
temp->next=top;
top=temp;
}
}
void display()
{
struct Node *var=top;
if(var!=NULL)
{
printf("\nElements are as:\n");
while(var!=NULL)
{
printf("\t%d\n",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}
int main()
{
int i=0;
top=NULL;
clrscr();
printf(" \n1. Push to stack");
printf(" \n2. Pop from Stack");
printf(" \n3. Display data of Stack");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);

Tuesday, February 10, 2015

Evaluation of Postfix Expression

AIM:
          To write a C program to evaluate the postfix expression

ALGORITHM:
            Step 1: Start the program.
Step 2: Read the postfix expression from left to right
Step 3: If the symbol read is an operand then push it onto the stack
Step 4: If the operator is read POP two operands and perform arithmetic 
            operations if operator is 
            + then result=operand 1 + operand 2
            -  then result=operand 1 - operand 2
            * then result=operand 1 * operand 2
            /  then result=operand 1 / operand 2
Step 5: Push the result onto the stack
Step 6: Repeat steps 2-5 till the postfix expression is not over
Step 7: Stop the program.

PROGRAM
#include <stdio.h>
#include <string.h>
int top = -1;
int stack[100];
/* push the given data into the stack */
void push (int data) {
stack[++top] = data;
}
/* Pop the top element from the stack */
int pop () {
int data;
if (top == -1)
return -1;
data = stack[top];
stack[top] = 0;
top--;
return (data);
}
int main()
 {
char str[100];
int i, data = -1, operand1, operand2, result;
/* Get the postfix expression from the user */
printf("Enter ur postfix expression:");
fgets(str, 100, stdin);
for (i = 0; i < strlen(str); i++)
 {
if (isdigit(str[i]))
{
/*
* if the i/p char is digit, parse
* character by character to get
* complete operand
*/
data = (data == -1) ? 0 : data;
data = (data * 10) + (str[i] - 48);
continue;
}

Monday, February 9, 2015

Infix to Postfix Expression Conversion

AIM:
          To write a C program to convert the infix expression into postfix expression using  
           stack.

ALGORITHM:
            Step 1: Start the program.
            Step 2: Get the infix expression as input.
Step 3: Read the input from left to right.
 Step 4: If the input is operand then place it in the postfix expression.
Step 5: Else if the input symbol is an operator then check for the operator type and
also the precedence, pop entries from the stack and place them in the
postfix expression until the lowest priority operator is encountered.
Step 6: ‘(‘symbol will be popped from stack only when we get a ‘)’ symbol.
Step 7: When the input is completely read then pop the elements in stack until it
becomes empty.
 Step 8: Display the postfix expression.
Step 9: Stop the program.
              
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
int top=0,st[20];
char inf[40],post[40];
void postfix();
void push(int);
char pop();        
void main()
{
clrscr();
printf("Enter the infix expression:");
scanf("%s",inf);
postfix();
getch();
}
void postfix()
{int i,j=0;
for(i=0;inf[i]!=0;i++)
{switch(inf[i])
{
case '+':while(st[top]>=1)
post[j++]=pop();
push(1);
break;
case '-':while(st[top]>=1)
post[j++]=pop();
push(2);
break;
case '*':while(st[top]>=3)
post[j++]=pop();
push(3);
break;
case '/':while(st[top]>=4)
post[j++]=pop();
push(4);
break;
case '^':
post[j++]=pop();
push(5);
break;
case '(':push(0);
break;

Sunday, February 8, 2015

IMPLEMENTATION OF SINGLY LINKED LIST

AIM:
            To write a C program to implement singly linked list.

ALGORITHM:
            Step 1: Define a list as a node of a structure with one data and one pointer
pointing to next element in the structure.
            Step 2: Declare the function prototypes creation (), insertion (), deletion () and
display () to perform the list function
Step 3: Declare the necessary pointer variables as structure node data type.
Step 4: Get a choice from the user.
Step 5: If the choice is create get first data from the user by calling the function
create () and display  contents of the list.
Step 6: If the choice is insert, get data and its position by calling function
insert ()and assign the next field of the given data node according to the position. Display the contents of the list.
Step 7: If the choice is delete, get the position of data which is going to be
removed from the list and assign next field of previous node to address.
Step 8: Repeat the steps 4, 5&6 until the choice is exit.
Step 9: Terminate the execution.       

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdio.h>
typedef struct SLL
{               
int data;
struct SLL *next;
}node;
node *create();
void main()
{
int choice,val;
char ans;
node *head;
void display(node*);
node *search(node*,int);
void insert(node*);
void Delete(node**);
head=NULL;
do
{
clrscr();
printf("Program to perform various operations:");
printf("\n1.Create\n2.Display\n3.Search\n4.Insert\n5.Delete\n6.Exit");
printf("\nEnter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:
head=create();break;
case 2:
display(head);break;
case 3:
printf("Enter the element to be searched");
scanf("%d",&val);
search(head,val);break;
case 4:
insert(head);break;
case 5:
Delete(&head);break;
case 6:
exit(0);
default :
clrscr();
printf("Invalid choice");
getch();
}
}while(choice!=6);
}

node *create()
{
node *temp,*New,*head;
int val,flag;
char ans='y';
node *get_node();
temp=NULL;
flag=1;
do
{
printf("\nEnter your element:");
scanf("%d",&val);
New=get_node();
if(New==NULL)
printf("\nMemory not allocated");
New->data=val;
if(flag)
{
head=New;
temp=head;
flag=0;
}
else
{
temp->next=New;
temp=New;
}
printf("\nDo you want to enter more elements?(y/n)");
ans=getch();
}while(ans=='y');
printf("\nSLL is created");
getch();
clrscr();
return head;
}

Saturday, February 7, 2015

IMPLEMENTATION OF RANDOM ACCESS FILE

AIM
            To write a C program to implement random access of files.

ALGORITHM
Step-1: Start the program
Step-2: Open the file RANDOM in write mode and get the characters.
Step-3: If it is the End of File then print the number of characters entered.
Step-4: Close the file and open it in read mode.
Step-5: Find the position of each character using fseek and print it.
Step-6: Stop the program

PROGRAM
#inlcude<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
long n;
char c;
fp=fopen(“RANDOM”,”w”),;
while((c=getchar())!=EOF)
putc(c,fp);
printf(“no. of characters entered = %ld \n”,ftell(fp));
fclose(fp);
fp=fopen(“RANDOM”,”r”);
n=OL;
while(feof(fp)==0)
{
fseek(fp,n,0); /* position to ()n+1)th character */
printf(“Position of %c is %ld \n”, getc(fp),ftell(fp));
n=n+5L;
}
putchar(‘\n’);
fseek(fp,-1L,2);
do
{
putchar(getc(fp));
}
while(!fseek(fp,-2L,1));
fclose(fp);
}

Friday, February 6, 2015

IMPLEMENTATION OF SEQUENTIAL ACCESS FILE

AIM

            To write a C program to implement sequential access of file

ALGORITHM

Step-1: Start the program
Step-2: Open the student file in writing mode
Step-3: Get the user id, name and marks of the students
Step-4: Close the file and open the file in read mode
Step-5: For the search option, user id is given. If the record is found the details of the  
            Student of the student is displayed else failure will be displayed.
Step-6: The display option gives the entire details of the student
Step-7: End the program in the main function

PROGRAM
#include <stdio.h>
typedef struct
{
 int usn;
 char name[25];
 int m1,m2,m3;
}STD;
STD s;
void display(FILE *);
int search(FILE *,int);
void main()
{
 int i,n,usn_key,opn;
 FILE *fp;
 printf(" How many Records ? ");
 scanf("%d",&n);
 fp=fopen("stud.dat","w");
 for(i=0;i<n;i++)
 {
  printf("Read the Info for Student: %d (uid,name,m1,m2,m3) \n",i+1);
  scanf("%d%s%d%d%d",&s.usn,s.name,&s.m1,&s.m2,&s.m3);
  fwrite(&s,sizeof(s),1,fp);
 }
 fclose(fp);
 fp=fopen("stud.dat","r");
 do
 {
  printf("Press 1- Display\t 2- Search\t 3- Exit\t Your Option?");
  scanf("%d",&opn);
  switch(opn)
  {
  case 1: printf("\n Student Records in the File \n");
  display(fp);
  break;
  case 2: printf(" Read the uid of the student to be searched ?");
  scanf("%d",&usn_key);
  if(search(fp,usn_key))
  {
   printf("Success ! Record found in the file\n");
   printf("%d\t%s\t%d\t%d\t%d\n",s.usn,s.name,s.m1,s.m2,s.m3);
  }
  else
   printf(" Failure!! Record with USN %d not found\n",usn_key);
  break;
  case 3:  printf(" Exit!! Press a key . . .");
  getch();
  break;
  default:  printf(" Invalid Option!!! Try again !!!\n");
  break;
  }
 }while(opn != 3);
 fclose(fp);
}   /* End of main() */
void display(FILE *fp)
{
 rewind(fp);
 while(fread(&s,sizeof(s),1,fp))
  printf("%d\t%s\t%d\t%d\t%d\n",s.usn,s.name,s.m1,s.m2,s.m3);
}
int search(FILE *fp, int usn_key)
{
 rewind(fp);
 while(fread(&s,sizeof(s),1,fp))
  if( s.usn == usn_key) return 1;
 return 0;
}


Thursday, February 5, 2015

TO CALCULATE THE SUM OF THE ARRAY ELEMENT USING POINTERS AND FUNTIONS

AIM
             To write a C program to illustrate parameter passed to the function.

ALGORITHM
Step-1: Start the program
Step-2: Enter the size of the array
Step-3: Enter the elements of the array
Step-4: Print the array elements
Step-5: Call the function with base address of the array passed to it
Step-6: In the calling function gets the base address in the pointer variable
Step-7: Add the array elements
Step-8: Return the value from the calling function to the variable in the called function
Step-9: Print the sum in the called function
Step-10: End the program in the main function

PROGRAM
#include<stdio.h>
main()
{
            int a[10],i,no,sum=0;
            printf(“Enter the size of array...”);
            scanf(“%d”,&no);
            printf(“Enter the elements of the array...”);
            for(i=0;i<no;i++)
                        scanf(“%d”,&a[i]);
            for(i=0;i<no;i++)
                        printf(“\n%d”,a[i]);
            sum=add(&a[0],no);
            printf(“\nThe sum of %d numbers is...%d”,no,sum);
}
add(int *pt,int n)
{
            int i,a=0;
            for(i=0;i<n;i++)
            {
                        a=a+*pt;
                        pt++;
            }
return(a);
}

Wednesday, February 4, 2015

TO CHECK WHETHER THE GIVEN STRING IS PALINDROME

AIM
      To write a C program to find whether the string is palindrome or not.
        
ALGORITHM:
 Step-1:  Start the program
Step-2:  Enter the string
Step-3:  Find the string length using the strlen() function
Step-4:  Print the string length
Step-5:  Set a loop up to the half of the string length
Step-6:  Compare every character above the middle character with the below character
              of the middle character
Step-7:  If any character equal prints the given string is palindrome
Step-8:  If the character is not equal then print the given string is not a palindrome
Step-9:  Stop
     
PROGRAM
 #include<stdio.h>
#include<stdlib.h>
void main()
{
      int len=0,i,j;
      char name[25];
      printf(“Enter the string...”);
      scanf(“%s”,name);
      while(name[len]!=’\0')
            len++;
      printf(“\n%d”,len);
      for(i=0,j=len-1;i<len/2;i++,j-)
      {
            if(name[i]!=name[j])
            {
                  printf(“\nThe given string is not a palindrome”);
                  exit(0);
            }     }}