Friday, September 24, 2010

DSA EX.NO.1a IMPLEMENTATION OF SINGLY LINKED LIST

EX.NO.1a         IMPLEMENTATION OF SINGLY LINKED LIST
                           

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

ALGORITHM:

Step 1: Start the program.

Step 2: Declare a  structure  named as  node with  two fields, one  is data
field  and  another  one  address field , address   field  must  be a  pointer pointing  to next  node.

Step 3: CREATION:
          a. Get the size of lists that has to be created.
          b. Allocate memory space for each node.
          c. Create two pointers to point first node link all other nodes using
              pointers.

Step 4: INSERTION:
          a. Allocate memory space for new node.
          b. Get the position where the new node wants to be  inserted.
          c. Change the links.

Step 5: DELETION:
          a. Get the position of the node that has to be deleted.
          b. Change the links.

Step 6: DISPLAY:
          a. Check weather the list is empty or not.
          b. If list is not empty print the data until the address field is null.

Step 7: Stop the process.





PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdio.h>
struct node
{
int data;
struct node*next;
};
struct node*start,*p,*new1;
int item,i,pos,n,num,null;
void display();
void create()
{
printf("Enter the size of list:");
scanf("%d",&n);
{
new1=(struct node*)malloc(sizeof(struct node));
printf("enter the data");
scanf("%d",&item);
new1->data=item;
new1->next=NULL;
if(i==1)
{
start=new1;
p=new1;
}
else
{
p->next=new1;
p=new1;
}
}
display();
}
void insert()
{
p=start;
printf("enter the position");
scanf("%d",&pos);
new1=(struct node*)malloc(sizeof(struct node));
printf("\n enter the data");
scanf("%d",&item);
new1->data=item;
new1->next=NULL;
for(i=1;i<pos;i++)
p=p->next;
new1->next=p->next;
p->next=new1;
n=n+1;
display();
}
void delet()
{
printf("enter the position");
scanf("%d",&pos);
p=start;
for(i=1;i<pos;i++)
p=p->next;
p->next=p->next->next;
printf("node deleted");
n=n-1;
display();
}
void search()
{
p=start;pos=0;
printf("Enter the demand to be found");
scanf("%d",&num);
for(i=1;i<n;i++)
{
if(p->data==num)
{
printf("Elements is found at %d position\n",i-1);
pos=i;
break;
}
else
p=p->next;
}
if(pos==0)
printf("\nElement not found");
}
void display()
{
p=start;
for(i=1;i<=n;i++)
{
printf("%d->",p->data);
p=p->next;
}
printf("NULL");
}
void main()
{
int c;
clrscr();
for(;;)
{
printf("........MENU.........");
printf("\n1.create");
printf("\n2.insert");
printf("\n3.delete");
printf("\n4.search");
printf("\n5.display");
printf("\n6.exit");
printf("\nEnter the choice");
scanf("%d",&c);
switch(c)
{
case 1:create();break;
case 2:insert();break;
case 3:delet();break;
case 4:search();break;
case 5:display();break;
case 6:exit(0);break;
default:printf("Enter correct choice");
}
}

DSA Ex. No: 1b IMPLEMENTATION OF DOUBLY LINKED LIST

Ex. No: 1b           IMPLEMENTATION OF DOUBLY LINKED LIST


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

ALGORITHM:
         
          Step1: Start the program.

Step2: Declare the structure named as node with two fields (address) 
           and one data field pointing the previous and next node.

Step3:  CREATION

(a)   Get the size of list that has to be created.
(b)  Allocate the memory space for each node.
(c)   Link all the nodes using pointer.

           Step4:  INSERTION

(a)   Allocate memory space for new node.
(b)  Get the position where the new node wants to be inserted.
(c)   Change the links.

Step5:  DELETION

                    (a)Get the position where the new node wants to be deleted.
                    (b) Change the links
         
Step6:  DISPLAY

(a)   Check whether the list is empty or not.
(b)  If it is not empty print the data until the list address field is empty.

Step7: Stop the process.


PROGRAM:

#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node*next;
struct node*prev;
};
struct node *start,*p,*new1;
int item,i,n,pos,num,null;
void display();
void create()
{
printf("\n Enter the size of the list:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
new1=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&item);
new1->data=item;
new1->next=NULL;
new1->prev=NULL;
if(i==1)
{
start=new1;
p=new1;
}
else
{
p->next=new1;
new1->prev=p;
p=new1;
}
}
display();
}

void insert()
{
p=start;
printf("\n Enter the position:");
scanf("%d",&pos);
new1=(struct node*)malloc(sizeof(struct node));
printf("Enter the data:");
scanf("%d",&item);
new1->data=item;
new1->next=NULL;
new1->prev=NULL;
for(i=1;i<pos;i++)
p=p->next;
new1->next=p->next;
p->next=new1;
n=n+1;
display();
}
void delet()
{
printf("\n Enter the position:");
scanf("%d",&pos);
p=start;
for(i=1;i<pos;i++)
p=p->next;
p->next=p->next->next;
p->next->prev=p;
printf("\n node deleted");
n=n-1;
display();
}
void search()
{
p=start;
pos=0;
i=1;
printf("\n Enter the element to be found");
scanf("%d",&num);
while(p!=NULL)
{
if(p->data==num)
{
printf("Element is found at%dposition\n",i-1);
pos=i;
break;
}
else
p=p->next;
i++;
}
if(pos==0)
printf("\n Element is not found");
}
void display()
{
p=start;
for(i=1;i<=n;i++)
{
printf("%d",p->data);
p=p->next;
}
printf("NULL");
}
void main()
{
int c;
clrscr();
for(;;)
{
printf("\n*****MENU******");
printf("\n1.create");
printf("\n2.Insert");
printf("\n3.delete");
printf("\n4.search");
printf("\n5.display");
printf("\n6.exit");
printf("\n Enter the choice");
scanf("%d",&c);


switch(c)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
delet();
break;
case 4:
search();
break;
case 5:
display();
break;
case 6:
exit();
break;
default:
printf("Enter the correct choice:");
getch();
}
}
}

DSA EX.NO:2 ADDITION OF TWO POLYNOMIAL.

EX.NO:2              ADDITION OF TWO POLYNOMIAL.
             
AIM:
       To write a program for the addition of  polynomial.  

ALGORITHM:

     Step 1:Start the program.
     Step 2:Declare the variables,structure and functions to be used.s
     Step 3:In the main create two polynomial poly1 and poly2 to perform
              addition and then display the result.
     Step 4:stop the program.

CREATE

              *Get the size of the list and the two polynomial coefficient
                and the power.
              *Display the both polynomial.
SHOW

            *Display  the two polynomial input such as poly1 and poly2 in the
             Form
               x^n+x^2+x^1+c
ADD

          *Check that the power ofpoly1 is greater than power of poly2.
          *If greater ,then the power and coefficient of poly1 is assigned to the
            result poly.
          *If it is less,then the power and coefficient of poly2 is assigned to the
            result poly.
          *If the powers of poly1 and poly2 are equal then the addition of the
            two coefficient is performed.
          *If any leftout polynomial is found in the input then assign the
           polynomial  to the output.
          
                     




PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct link
{
int coeff;
int pow;
struct link *next;
};
struct link*poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link*node)
{
char ch;
int i,n;
printf("\n Enter the size of the list:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the coeff:");
scanf("%d",&node->coeff);
printf("\nEnter power:");
scanf("%d",&node->pow);
node->next=(struct link*)malloc(sizeof(struct link));
node=node->next;
node->next=NULL;
}
}
void show(struct link*node)
{
while(node->next!=NULL)
{
printf("%dx^%d",node->coeff,node->pow);
node=node->next;
if(node->next!=NULL)
printf("+");
}
}
void polyadd(struct link*poly1,struct link *poly2,struct link *poly)
{
while(poly1->next&&poly2->next)
{
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
else
if(poly1->pow<poly2->pow)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
else
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff+poly2->coeff;
poly1=poly1->next;
poly2=poly2->next;
}
poly->next=(struct link*)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
while(poly1->next||poly2->next)
{
if(poly1->next)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
if(poly2->next)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
poly->next=(struct link*)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
}
void main()
{
clrscr();
char ch;
do
{
poly1=(struct link*)malloc(sizeof(struct link));
poly2=(struct link*)malloc(sizeof(struct link));
poly=(struct link*)malloc(sizeof(struct link));
printf("*****POLYNOMIAL ADDITION*****");
printf("\n\n");
printf("\nEnter 1st polynomial p1(x):");
create(poly1);
printf("\nEnter 2nd polynomial p2(x):");
create(poly2);
clrscr();
printf("\n 1st number:");
show(poly1);
printf("\n 2nd number:");
show(poly2);
polyadd(poly1,poly2,poly);
printf("\n\n Added polynimial:");
show(poly);
printf("\n\n Doyou want to contiue(Y/N):");
ch=getch();
}
while(ch=='y'||ch=='y');
}

DSA EX.NO:3 INFIX TO POSTFIX CONVERSION

EX.NO:3               INFIX TO POSTFIX CONVERSION



AIM:
       To write a c program to convert the given expression to postfix expression using stack.

ALGORITHM:
       Step 1:
                  Start the program.
       Step 2:
                  Given the precedence for left and right give higher precedence   
                  for right.
       Step 3:
                 Assign the precedence to the operator.
       Step 4:
                 Get the infix expression.
       Step 5:
                 Calculate the length of the infix expression.
       Step 6:
                If it is left parenthesis ,push it into the stock .if it is right
                parenthesis then it is popped out until the ‘(‘ is seen..
       Step 7:
                   If it is operand ,then it is given directly to output.   
                
     Step 8:
                   If it is operator then check the precedence.

     Step 9:
                  If the input precedence is less than the precedence is less than the  
                  precedence of the top element,pop out then the top element of  
                  stock,if it is greater push the scanned value into the stock.
     Step 10:
                 Repeat step6 to10 untill end of expression is scanned.
     Step 11:
                 After reading all character pop the leftout character in stock.
      Step 12:
                  Stop the process

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define N 64
#define LP 10
#define RP 20
#define OPERATOR 30
#define OPERAND 40
#define LPP 0
#define AP 1
#define SP AP
#define MP 2
#define DP MP
#define REMP 2
#define NONE 9
static char infix[N+1],stack[N],postfix[N+1];
static int top;
void infixtopostfix(void);
int gettype(char);
void push(char);
char pop(void);
int getprec(char);
void main()
 {
 clrscr();
 char ch;
 do
   {
          top=-1;
          printf("\n Enter an infix expression\n");
          fflush(stdin);
          gets(infix);
          infixtopostfix();
          printf("\n infix=%s\n Postfix= %s\n",infix,postfix);
          printf("\n Do you wish to cotinue\n");
          ch=getch();
   }while(ch=='Y'||ch=='Y');
 }
void infixtopostfix(void)
 {
   int i,p,l,type,prec;
   char next;
   i=p=0;
   l=strlen(infix);
   while(i<l)
          {
          type=gettype(infix[i]);
          switch(type)
                   {
                   case LP:
                     push(infix[i]);
                   break;
                   case RP:
                     while((next=pop())!='(')
                     postfix[p++]=next;
                   break;
                   case OPERAND:
                     postfix[p++]=infix[i];
                   break;
                   case OPERATOR:
                     prec=getprec(infix[i]);
                     while(top>-1 && prec <= getprec(stack[top]))
                     postfix[p++]=pop();
                     push(infix[i]);
                   break;
                }
                i++;
          }while(top>-1)
     postfix[p++]=pop();
     postfix[p]='\0';
  }
int gettype(char sym)
       { switch(sym)
       {
            case '(':
             return(LP);
             case ')':
             return(RP);
             case '+':
             case '-':
             case '*':
             case '/':
             case '%':
             return(OPERATOR);
            default:
             return(OPERAND);
       }
 }
  void push(char sym)
  {
   if(top>N)
          {
                   printf("\nStack is full\n");
                   exit(0);
          }
          else
                   stack[++top]=sym;
          }
          char pop(void)
          {
                   if(top<=-1)
                   {
                             printf("\n Stack is empty\n");
                             exit(0);
                   }
                   else
                   return(stack[top--]);
                   }
          int getprec(char sym)
          {
          switch(sym)
          {
                   case '(':return(LPP);
                   case '+':return(AP);
                   case '-':return(SP);
                   case '*':return(MP);
                   case '/':return(DP);
                   case '%':return(REMP);
                   default:return(NONE);
          }
}