09.07.2006 at
7:44 am · Saved under
C/C++ Help
#include<stdio.h>
main()
{
int pt[10][10],a[10][10],at[10],pname[10][10],i,j,n,k=0,q,sum=0;
float avg;
printf(”\n\nEnter the number of processes : “);
scanf(”%d”,&n);
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
pt[i][j]=0;
a[i][j]=0;
}
}
for(i=0;i<n;i++)
{
j=0;
printf(”\n\nEnter the process time for process %d: “,i+1);
scanf(”%d”,&pt[i][j]);
}
printf(”\n\nEnter the time slice : “);
scanf(”%d”,&q);
printf(”\n\n”);
for(j=0;j<10;j++)
{
for(i=0;i<n;i++)
{
a[2*j][i]=k;
if((pt[i][j]<=q)&&(pt[i][j]!=0))
{
pt[i][j+1]=0;
printf(”%d P%d %d\n”,k,i+1,k+pt[i][j]);
k+=pt[i][j];
a[2*j+1][i]=k;
}
else if(pt[i][j]!=0)
{
pt[i][j+1]=pt[i][j]-q;
printf(” %d P%d %d\n”,k,i+1,(k+q));
k+=q;
a[2*j+1][i]=k;
}
else
{
a[2*j][i]=0;
a[2*j+1][i]=0;
}
}
}
for(i=0;i<n;i++)
{
sum+=a[0][i];
}
for(i=0;i<n;i++)
{
for(j=1;j<10;j++)
{
if((a[j][i]!=0)&&(a[j+1][i]!=0)&&((j+1)%2==0))
{
sum+=((a[j+1][i]-a[j][i]));
}
}
}
avg=(float)sum/n;
printf(”\n\nAverage waiting time= %f msec”,avg);
sum=avg=0;
for(j=0;j<n;j++)
{
i=1;
while(a[i][j]!=0)
{
i+=1;
}
sum+=a[i-1][j];
}
avg=(float)sum/n;
printf(”\n\n Average turnaround time =%f msec\n\n”,avg);
return 0;
}
30.06.2006 at
6:24 am · Saved under
C/C++ Help
Definition: A variant of a linked list in which each item has a link to the previous item as well as the next. This allows easily accessing list items backward as well as forward and deleting any item in constant time.
Also known as two-way linked list, symmetrically linked list.
Below is the simple exapmle code which will make you understand how to program it with C.
#include <iostream.h>
#include <process.h>
class doubly
{
doubly * next;
doubly * pre;
int data,ch;
public:
doubly * link(doubly * );
void traverse (doubly * );
};
doubly * doubly::link(doubly * temp)
{
doubly * newlink;
doubly * cur;
newlink = new doubly;
cout<<”\nEnter Element (only possitive number):”;
cin>>data;
cur = temp;
if(temp == NULL)
{
newlink->data = data;
newlink->pre = NULL;
newlink->next = NULL;
temp = newlink;
return temp;
}
else
{
while(cur->next != NULL)
{
cur = cur->next ;
}
newlink->data = data;
newlink->pre = cur;
newlink->next = NULL;
cur->next = newlink;
return temp;
}
}
void doubly::traverse(doubly *temp)
{
int ch;
doubly * dummy;
dummy = temp;
cout<<”\n[1] Start From Begainning\n[2] Start From End\n”;
cout<<”\nEnter Your Choice :”;
cin>>ch;
switch(ch)
{
case 1:
if(dummy != NULL)
{
while(temp->next !=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
if (temp->next == NULL)
cout<<temp->data<<endl;
}
break;
case 2:
if(temp != NULL)
{
temp = dummy;
while(dummy->next !=NULL)
{
dummy=dummy->next;
}
while(dummy->pre !=NULL)
{
cout<<dummy->data<<endl;
dummy=dummy->pre;
}
if (dummy->pre == NULL)
cout<<dummy->data<<endl;
}
break;
}
}
void main()
{
doubly *first=NULL,d1;
int choice;
while(1)
{
cout<<”\n************** DOUBLY LINK LIST **************\n\n”;
cout<<”Choices Are :-\n=>[1] For Insert \n=>[2] For Traverse \n=>[3] For Exit”;
cout<<”\n\nEnter Your choice : “;
cin>>choice;
switch (choice)
{
case 1:
first=d1.link(first);
break;
case 2:
d1.traverse(first);
break;
case 3:
exit(0);
}
}
}
30.06.2006 at
4:32 am · Saved under
C/C++ Help
Stack is a list of elements in which insertions and deletions are at the same end of the list called top. The other end is known as Bottom.Insertion is also known as push, deletion is also known as pop
This code gives the simplest way to implement the Stack Data structure using Link List instead of using Arrays.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
main()
{
struct node
{
int data,n;
struct node *link;
};
int n,item;
typedef struct node node;
node *temp,*sp=NULL;
while(1)
{
clrscr();
printf(”Please select your choice\n1. PUSH\n2. POP\n3. Search\n4. Display\n5. Ex!t\n”);
scanf(”%d”,&n);
switch(n)
{
case 1:
printf(”Enter the data to PUSH into Stack\n”);
scanf(”%d”,&item);
temp=(node *) malloc(sizeof(node));
temp->data=item;
temp->link=sp;
sp=temp;
break;
case 2:
if(sp==NULL)
{
printf(”\nSTACK underflow\n”);
getch();
}
else
{
item=sp->data;
sp=sp->link;
printf(”\nPOP Successful Removed Data Is %d”,item);
getch();
}
break;
case 3:
if(sp==NULL)
{
printf(”\nSTACK underflow\n”);
}
else
{
temp=sp;
printf(”\nEnter data to search from Stack”);
scanf(”%d”,&item);
n=0;
while(temp->data!=item)
{
if(temp->link==NULL)
goto print;
temp=temp->link;
n++;
}
print:
if(temp->data==item)
{
printf(”Data %d Found In Stack at position %d”,item,n+1);
}
else
{
printf(”Data not present in Stack”);
}
getch();
break;
case 4:
if(sp==NULL)
{
printf(”\nSTACK is empty\n”);
}
else
{
temp=sp;
printf(”\n%d”,temp->data);
while(temp->link!=NULL)
{
temp=temp->link;
printf(”\n%d”,temp->data);
}
}
getch();
break;
case 5:
exit(0);
break;
}
}
}
}
30.06.2006 at
2:58 am · Saved under
C/C++ Help
Its very first program for link list, which helps you to learn Pointer & Link List easily.
Please do not enter any Data type other then Integer Data otherwise It’ll go in Infinite Loop.
#include <iostream.h>
#include <process.h>
class linklist
{
linklist *next;
int data;
public:
linklist * ifirst(linklist *);
void traverse(linklist *);
linklist * ilast(linklist *);
void del(linklist *);
};
linklist * linklist::ifirst(linklist *temp)
{
linklist *newlink;
newlink=new linklist;
cout<<"\nEnter Data\n";
cin>>newlink->data;
newlink->next=temp;
temp=newlink;
return temp;
}
void linklist::traverse(linklist *temp)
{
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
linklist * linklist::ilast(linklist *temp)
{
linklist *newlink;
newlink=new linklist;
if (temp==NULL)
{
linklist *newlink;
newlink=new linklist;
cout<<"\nEnter Data :";
cin>>newlink->data;
newlink->next=temp;
temp=newlink;
}
else
{
linklist *cur;
cur=temp;
while(cur->next!=NULL)
{
cur=cur->next;
}
cout<<"\nEnter Data :";
cin>>newlink->data;
newlink->next=NULL;
cur->next=newlink;
}
return temp;
}
void main()
{
linklist *first=NULL,l1;
int choice;
while(1)
{
cout<<"**************** LINK LIST (Only For Integer) ****************\n\n";
cout<<"Choices Are :-\n=> [1] For Insert First\n=> [2] For Insert Last\n=> [3] For Traverse\n=> [4] For Exit";
cout<<"\n\nEnter Your choice : ";
cin>>choice;
switch (choice)
{
case 1:
first=l1.ifirst(first);
break;
case 2:
first=l1.ilast(first);
break;
case 3:
l1.traverse(first);
break;
case 4:
exit(0);
}
}
}