Showing posts with label Complexity. Show all posts
Showing posts with label Complexity. Show all posts

Monday, 6 November 2017

C program to Implement Priority Queue Menu Driven Program

/*Program of priority queue using linked list*/
#include<stdio.h>
#include<stdlib.h>

struct node
{
int priority;
int info;
struct node *link;
}*front=NULL;

void insert(int item, int item_priority);
int del();
void display();
int isEmpty();

main()
{
int choice,item,item_priority;
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("03.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Input the item to be added in the queue : ");
scanf("%d",&item);
printf("Enter its priority : ");
scanf("%d",&item_priority);
insert(item, item_priority);
break;
case 2:
printf("Deleted item is %d\n",del());
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/

void insert(int item,int item_priority)
{
struct node *tmp,*p;

tmp=(struct node *)malloc(sizeof(struct node));
if(tmp==NULL)
{
printf("Memory not available\n");
return;
}
tmp->info=item;
tmp->priority=item_priority;
/*Queue is empty or item to be added has priority more than first element*/
if( isEmpty() || item_priority < front->priority )
{
tmp->link=front;
front=tmp;
}
else
{
p = front;
while( p->link!=NULL && p->link->priority<=item_priority )
p=p->link;
tmp->link=p->link;
p->link=tmp;
}
}/*End of insert()*/

int del()
{
struct node *tmp;
int item;
if( isEmpty() )
{
printf("Queue Underflow\n");
exit(1);
}
else
{
tmp=front;
item=tmp->info;
front=front->link;
free(tmp);
}
return item;
}/*End of del()*/

int isEmpty()
{
if( front == NULL )
return 1;
else
return 0;

}/*End of isEmpty()*/

void display()
{
struct node *ptr;
ptr=front;
if( isEmpty() )
printf("Queue is empty\n");
else
{    printf("Queue is :\n");
printf("Priority       Item\n");
while(ptr!=NULL)
{
printf("%5d        %5d\n",ptr->priority,ptr->info);
ptr=ptr->link;
}
}
}/*End of display() */

Sunday, 10 September 2017

SNAKE GAME SIMPLIFIED IN C++ [Easiest Way]


//SNAKE GAME CREATED BY MOHD MUJTABA
// Follow Me on Insta @mujtaba.aamir1
// Facebook @mujtaba.aamir1
#include<iostream>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
using namespace std;
bool gameover;
int x,y;
const int width=20,height=20;
 int score=0;
int fruitX,fruitY;
enum DIRECTION{STOP=0,UP,DOWN,LEFT,RIGHT};
DIRECTION dir;

void draw()
{
system("cls");
    int i=0;
for(i=0;i<width+2;i++)
cout<<"#";
cout<<endl;
for(int j=0;j<height;j++)
{
for(i=0;i<width+2;i++)
{
if(i==0)
cout<<"#";
if(i==x&&j==y)
cout<<"O";
else
if(fruitX==i&&fruitY==j)
cout<<"F";
else 
cout<<" ";

if(i==width-1)
cout<<"#";
if(x==fruitX&&y==fruitY)
        {
        score+=10;
    fruitX= rand()%width;
    fruitY= rand()% height;
        }  

}
cout<<endl;
}

for(int i=0;i<width+1;i++)
cout<<"#";
cout<<endl;
cout<<"SCORE="<<score;
}


void input()
{
if(_kbhit())  // _kbhit is to find Keyboard input
{
switch(_getch())
{
case 'a':
dir=LEFT;
break;
case 'd':
dir=RIGHT;
break;
case 's':
dir=DOWN;
break;
case 'w':
dir=UP;
break;
case 'x':
gameover=true;
default:
break;
}
}
}

void logic()
{
switch(dir)
{
case UP:
--y;
break;

case DOWN:
++y;
break;

case RIGHT:
++x;
break;

case LEFT:
--x;
break;

default:
break;
}
if(x>=width)
x=0;
else if(x<0)
x=width;
if(y>=height)
y=0;
else if(y<0)
y=height;

}
void start()
{
gameover=false;
x=width/2;
 y=height/2;
fruitX=rand()%width;
fruitY=rand()%height;
score=0;
}

main()
{

start();
while(!gameover)
{
Sleep(50); // This function is used to delay the output
draw();
input();
logic();
}
}//end of main()

Amazon1Ads