Saturday, August 13, 2011
Data Structures (Queue operations using arrays)
#include<stdio.h>
#include<conio.h>
int queue[10];
int front = -1;
int rear = -1;
int n = 9;
void queue_insert(int data)
{
     if(front==n)
    {
         printf("Queue rull");
    }
    else if(front==-1)
    {
         queue[0] = data;
         front = rear = 0;
     }
     else
     {
         queue[++rear] = data;
    }
}
void queue_delete()
{
     if(front==-1 && rear==-1)
     {
         printf("Queue empty");
         exit(0);
     }
     else if(front==rear)
     {
          printf("\n%d deleted from the queue.\n",queue[front]);
         front=rear=-1;
     }
     else
     {
     printf("%d deleted from the queue.\n",queue[front]);
     front++;
     }
}
int main()
{
    queue_insert(10);
    queue_insert(20);
    queue_delete();
    getch();
}
Thursday, August 11, 2011
Data Structures (Stack operations using arrays)
#include<stdio.h>
#include<conio.h>
int top = -1;
int stack[10];
void push(int data)
{
stack[++top] = data;
printf("Data pushed\n");
}
void pop()
{
if(top!=-1)
{
printf("\n%d is popped out of the stack",stack[top]);
top--;
}
else
printf("Stack empty");
}
int main()
{
push(10);
push(20);
pop();
getch();
}
#include<conio.h>
int top = -1;
int stack[10];
void push(int data)
{
stack[++top] = data;
printf("Data pushed\n");
}
void pop()
{
if(top!=-1)
{
printf("\n%d is popped out of the stack",stack[top]);
top--;
}
else
printf("Stack empty");
}
int main()
{
push(10);
push(20);
pop();
getch();
}
Data Structures (Stack operations using linked lists)
#include<stdio.h>
#include<conio.h>
/*
Define the structure of the node with the data it holds and a link to another node.
*/
struct node
{
int item;
struct node *link;
};
/*
Declare a pointer top to point the TOP of the stack. Initially the pointer top is pointing to NULL
*/
struct node *top;
/*
The push function.
*/
void push(int data)
{
//create a temporary node, add data to it, link it with the stack and update the top
struct node temp;
if(&temp!=NULL) // if node is not created, then there is no memory for new node
{
temp.item = data;
temp.link = top;
top = &temp;
printf("Data Inserted\n");
}
else
printf("Stack is full");
}
/*
The pop function
*/
void pop()
{
struct node *temp;
if(top!=NULL)//top NULL implies that stack is empty
{
temp = top;
printf("popped data is:%d",temp->item);
top = top->link;
}
else
printf("Stack empty");
}
int main()
{
push(10);
push(20);
pop();
getch();
return 0;
}
#include<conio.h>
/*
Define the structure of the node with the data it holds and a link to another node.
*/
struct node
{
int item;
struct node *link;
};
/*
Declare a pointer top to point the TOP of the stack. Initially the pointer top is pointing to NULL
*/
struct node *top;
/*
The push function.
*/
void push(int data)
{
//create a temporary node, add data to it, link it with the stack and update the top
struct node temp;
if(&temp!=NULL) // if node is not created, then there is no memory for new node
{
temp.item = data;
temp.link = top;
top = &temp;
printf("Data Inserted\n");
}
else
printf("Stack is full");
}
/*
The pop function
*/
void pop()
{
struct node *temp;
if(top!=NULL)//top NULL implies that stack is empty
{
temp = top;
printf("popped data is:%d",temp->item);
top = top->link;
}
else
printf("Stack empty");
}
int main()
{
push(10);
push(20);
pop();
getch();
return 0;
}
Friday, February 4, 2011
Assembly Line Scheduling Algorithm in java
/**
* @author Naveen Kumar BS
* @copyright 2010
*
* Module: Assembly Line Scheduling
*
* Developed On: 03/08/2010
*
* Work: Implementing Assembly Line Scheduling.
* File Name: Fast.java
*/
import java.util.*;
class Fast
{
public static void main(String args[])
{
int[] a1={7,9,3,4,8,4},a2={8,5,6,4,5,7},t1={2,3,1,3,4},t2={2,1,2,2,1};
int e1=2,e2=4,x1=3,x2=3,f,l,k;
int[] f1,f2,l1,l2;
f1=new int[6];
f2=new int[6];
l1=new int[6];
l2=new int[6];
f1[0]=e1+a1[0];
f2[0]=e2+a2[0];
System.out.println("\n\nAssembly times for stations on line 1");
System.out.print("Station\t\t:");
for (int i=0;i<6;i++)
System.out.print(" "+(i+1));
System.out.print("\nAssembly Time\t:");
for(int i=0;i<6;i++)
System.out.print(" "+a1[i]);
System.out.println("\n\nAssembly times for stations on line 2");
//System.out.println("Assembly times for stations on line 1");
System.out.print("Station\t\t:");
for (int i=0;i<6;i++)
System.out.print(" "+(i+1));
System.out.print("\nAssembly Time\t:");
for(int i=0;i<6;i++)
System.out.print(" "+a1[i]);
System.out.println();
for(int j=1;j<6;j++)
{
if((f1[j-1]+a1[j]) <=(f2[j-1]+t2[j-1]+a1[j]))
{
f1[j]=f1[j-1]+a1[j];l1[j]=1;
}
else
{
f1[j]=f2[j-1]+t2[j-1]+a1[j]; l1[j]=2;
}
if((f2[j-1]+a2[j])<=(f1[j-1]+t1[j-1]+a2[j]))
{
f2[j]=f2[j-1]+a2[j]; l2[j]=2;
}
else
{
f2[j]=f1[j-1]+t1[j-1]+a2[j];l2[j]=1;
}
}
if(f1[5]+x1 < f2[5]+x2)
{
f=f1[5]+x1;l=1;
}
else
{
f=f2[5]+x2;l=2;
}
System.out.println("\n\n");
System.out.print("f1\t:");
for(int i=0;i<6;i++)
System.out.print(f1[i]+ " ");
System.out.println();
System.out.print("f2\t:");
for(int i=0;i<6;i++)
System.out.print(f2[i]+ " ");
System.out.println();
System.out.println();
System.out.print("l1\t:");
for(int i=1;i<6;i++)
System.out.print(l1[i]+ " ");
System.out.println();
System.out.print("l2\t:");
for(int i=1;i<6;i++)
System.out.print(l2[i]+ " ");
System.out.println();
System.out.println();
System.out.println("Entry Times e1="+e1+" and e2="+e2);
System.out.println("Exit Times x1="+x1+" and x2="+x2);
System.out.println();
System.out.println("Optimal Time is: "+f);
System.out.println("Optimal line is: "+l);
System.out.println("\n\nOptimal Path\n");
System.out.println("Line "+l+" Station "+6);
k=l;
for(int j=5;j>=1;j--)
{
if (k==1)
{
k=l1[j];
System.out.println("Line "+k+" Station "+(j)+" ");
}
else
{
k=l2[j];
System.out.println("Line "+k+" Station "+(j)+" ");
}
}
System.out.println();
}
}
Output:
* @author Naveen Kumar BS
* @copyright 2010
*
* Module: Assembly Line Scheduling
*
* Developed On: 03/08/2010
*
* Work: Implementing Assembly Line Scheduling.
* File Name: Fast.java
*/
import java.util.*;
class Fast
{
public static void main(String args[])
{
int[] a1={7,9,3,4,8,4},a2={8,5,6,4,5,7},t1={2,3,1,3,4},t2={2,1,2,2,1};
int e1=2,e2=4,x1=3,x2=3,f,l,k;
int[] f1,f2,l1,l2;
f1=new int[6];
f2=new int[6];
l1=new int[6];
l2=new int[6];
f1[0]=e1+a1[0];
f2[0]=e2+a2[0];
System.out.println("\n\nAssembly times for stations on line 1");
System.out.print("Station\t\t:");
for (int i=0;i<6;i++)
System.out.print(" "+(i+1));
System.out.print("\nAssembly Time\t:");
for(int i=0;i<6;i++)
System.out.print(" "+a1[i]);
System.out.println("\n\nAssembly times for stations on line 2");
//System.out.println("Assembly times for stations on line 1");
System.out.print("Station\t\t:");
for (int i=0;i<6;i++)
System.out.print(" "+(i+1));
System.out.print("\nAssembly Time\t:");
for(int i=0;i<6;i++)
System.out.print(" "+a1[i]);
System.out.println();
for(int j=1;j<6;j++)
{
if((f1[j-1]+a1[j]) <=(f2[j-1]+t2[j-1]+a1[j]))
{
f1[j]=f1[j-1]+a1[j];l1[j]=1;
}
else
{
f1[j]=f2[j-1]+t2[j-1]+a1[j]; l1[j]=2;
}
if((f2[j-1]+a2[j])<=(f1[j-1]+t1[j-1]+a2[j]))
{
f2[j]=f2[j-1]+a2[j]; l2[j]=2;
}
else
{
f2[j]=f1[j-1]+t1[j-1]+a2[j];l2[j]=1;
}
}
if(f1[5]+x1 < f2[5]+x2)
{
f=f1[5]+x1;l=1;
}
else
{
f=f2[5]+x2;l=2;
}
System.out.println("\n\n");
System.out.print("f1\t:");
for(int i=0;i<6;i++)
System.out.print(f1[i]+ " ");
System.out.println();
System.out.print("f2\t:");
for(int i=0;i<6;i++)
System.out.print(f2[i]+ " ");
System.out.println();
System.out.println();
System.out.print("l1\t:");
for(int i=1;i<6;i++)
System.out.print(l1[i]+ " ");
System.out.println();
System.out.print("l2\t:");
for(int i=1;i<6;i++)
System.out.print(l2[i]+ " ");
System.out.println();
System.out.println();
System.out.println("Entry Times e1="+e1+" and e2="+e2);
System.out.println("Exit Times x1="+x1+" and x2="+x2);
System.out.println();
System.out.println("Optimal Time is: "+f);
System.out.println("Optimal line is: "+l);
System.out.println("\n\nOptimal Path\n");
System.out.println("Line "+l+" Station "+6);
k=l;
for(int j=5;j>=1;j--)
{
if (k==1)
{
k=l1[j];
System.out.println("Line "+k+" Station "+(j)+" ");
}
else
{
k=l2[j];
System.out.println("Line "+k+" Station "+(j)+" ");
}
}
System.out.println();
}
}
Output:
Subscribe to:
Posts (Atom)