Exemplo n.º 1
0
/* bfs: do bfs. the times of bfs has to equal to ttl
 * return unvisited node number */
int bfs(int pos, int ttl)
{
    int times, sum;
    Node *pt;
    sum = numnode;
    /* no such node */
    if (pos == -1)
        return sum;
    /* init visited array, queue tag and graph[pos] times */
    memset(visited, FALSE, MAXNODE * sizeof(int));
    front = end = graph[pos].times = 0;

    visited[pos] = TRUE;
    sum--;
    if (graph[pos].times == ttl)
        return sum;

    addq(pos);
    while (front != end) {
        pos = deleteq();
        for (times = graph[pos].times + 1, pt = graph[pos].next; pt; pt = pt->next) {
            pos = pt->field.pos;
            if (!visited[pos]) {
                visited[pos] = TRUE;
                sum--;
                graph[pos].times = times;
                if (times < ttl)
                    addq(pos);
            }
        }
    }

    return sum;
}
Exemplo n.º 2
0
void bfs(int v,int n)
{
	int i,j,u,w,q;
    u=v;
    visited[v]=1;
    printf("\t%d",u);
    do
	{
		for(i=0;i<n;i++)
		{
		 for(j=0;j<n;j++)
		 {
			w=*(G+u*n+i);
			    if(w==1)
			{
		   w=i;
		if(visited[w]==0)
			   {
				   add(&que,w);
		       visited[w]=1;
		       printf("\t%d",w);
			   }
			}
		 }
		}
	q=isempty(&que);
	if(q!=0)
	return;
	u=deleteq(&que);
	}while(true);
}
Exemplo n.º 3
0
int main()
{
  Queue *p = (Queue *)malloc(sizeof(Node)*5);
  Queue *q = (Queue *)malloc(sizeof(Node)*5);
  int sx = 100;
  addq(p, q, sx);
  addq(p, q, sx);
  printf(":%d\n", deleteq(p));
  return 0;
}
void Level_traversal(treePointer ptr)
{
    if(!ptr)return;
    addq(ptr);
    for(;;) {
        ptr = deleteq();
        if(ptr) {
            printf("%s ",ptr->data);
            if(ptr->leftchild)
                addq(ptr->leftchild);
            if(ptr->rightchild)
                addq(ptr->rightchild);
        }
        else break;
    }
}
Exemplo n.º 5
0
/**
 * breadth first traversal of a graph, starting with node v
 * the global array visited is initialized to 0, the queue
 * operations are similar to those described in Chapter 4
 */
void bfs(int v)
{
	node_pointer w;
	queue_pointer front, rear;
	front  = rear = NULL;	
	printf("%5d", v);
	visited[v] = TRUE;
	addq(&front, &rear, v);
	while(front){
		v = deleteq(&front);
		for(w = graph[v]; w; w = w->link)
			if(!visited[w->vertex]){
				printf("%5d",w->vertex);
				addq(&front, &rear, w->vertex);
				visited[w->vertex] = TRUE;
			}
	}
}
Exemplo n.º 6
0
void graph_bfs(fsnode **graph, int vert)
{
    fsnode *w;
    visited[vert] = TRUE;
    printf("%5d", vert);
    addq(vert);
    while (front != FSNODE_N - 1)
    {
        vert = deleteq();
        for (w = graph[vert]; w != NULL; w = w->link)
        {
            if (visited[w->vert] == FALSE)
            {
                printf("%5d", w->vert);
                addq(w->vert);
                visited[w->vert] = TRUE;
            }
        }
    }
}
main(){
    element test;
    test.key=1;

    int i=0;
    for(i=0;i<7;i++){
        addq(test);
    }
    for(i=0;i<3;i++){
        deleteq(test);
    }
    for(i=0;i<3;i++){
        addq(test);
    }




    printf("front:%d\n",front);
    printf("rear:%d\n",rear);

}
Exemplo n.º 8
0
void level_order(tree_ptr ptr)
{
int front=rear=0;
tree_ptr queue[MAX];
if(ptr == NULL)
	return;
	addq(&front,&rear,ptr);
	for( ; ; )
	{
		ptr=deleteq(&front,&rear);
		if(ptr!=NULL)
		{
		   printf("\n");
			printf("%5c",ptr->data);
			if(ptr->left_child)
				addq(&front,&rear,ptr->left_child);
			if(ptr->right_child)
					addq(&front,&rear,ptr->right_child);
		}
		else
			break;
	}
}
Exemplo n.º 9
0
int main(int argc, char const *argv[])
{
	char choice;
	while(1)
	{
		int i;
		element item;
		printf("There are some oparetions:   \n");
		printf("a) add            d) delete\n");
		printf("Your choice: ");
		scanf("%c",&choice);
		switch(choice)
		{
			case 'a':
				printf("Please enter an integer: ");
				scanf("%d",&item.key);
				addq(&rear,item);
				break;
			case 'd':
				deleteq(&front,rear);
				break;
		}
		/*消除 \n */
		while(getchar() != '\n')
			continue;
		if(rear>=0)
		{
			for(i=0;i<=rear;i++)
			{
				printf("%d->",queue[i].key);
			}
		}
		printf("\n");
	}
	return 0;
}
Exemplo n.º 10
0
void main()
{

// VARIABLE DECLARATIONS

int i=0,j=0,a,front=-1,rear=-1;
char ch,ch1;
int item;

// FUNCTION PROTOTYPE DECLARATIONS

void show();
void slidebar(int *rear,int *front);
void showqueuesize();
void slidebar();
void cpoiters();
void drawq();
void moveptrs(int *rear,int *front);
void insertq(int *rear,char c);
void deleteq(int *front);
void qdelete(int q[],int rear,int front);
void hidequeueinfo();
void shownumbers(int x1,int y1,int x2,int y2);
int initmouse();
void getmousepos(int *button,int *x,int *y);
void showmouseptr();
void restrictmouseptr(int a,int b,int c,int d);
void exitbutton(int x1,int y1,int x2,int y2,int e_x,int e_y,int xit_x,int xit_y);
void zoomin();

// CHECKING THE GRAPHICS DRIVER

int gd=DETECT,gm,maxx,maxy,x,y,button;
initgraph(&gd,&gm,"c:\\TC");

// GET THE MAXIMUM X AND Y CO ORDINATES

	maxx=getmaxx();
	maxy=getmaxy();

// DRAW THE BORDER FOR THE WINDOW

	rectangle(0,0,maxx,maxy);

// SET THE AREA WITHIN THE SCREEN WHERE GRAPHICAL O/P DISPLAYED

	setviewport(1,1,maxx-1,maxy-1,1);

// WRITE THE HEADDING

	gotoxy(150,1);
	settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
	setcolor(WHITE);
	setbkcolor(216);
	settextjustify(0,2);
	outtextxy(220,2," LINEAR QUEUE ");
	setcolor(RED);
	settextstyle(8,HORIZ_DIR,3);
	outtextxy(40,35,"DEVELOPED IN 'C'  : IT'S A KVSM PRODUCT");
	settextstyle(0,HORIZ_DIR,0);

// INITIALIZE MOUSE,IF IT DOESN'T GET INITIALIZED CLOSE GRAPHICS MODE

	if (initmouse()==0)
	{
		closegraph();
		restorecrtmode();
		printf("\nMouse Driver not installed");
		exit(1);
	}

// IF MOUSE DRIVER INITIALISED THEN
// RESTRICT THE MOUSE POINTER WITHIN THE COORDINATES GIVEN GIVEN BELOW

	restrictmouseptr(1,57,maxx-1,maxy-1);

// SHOW THE MOUSE POINTER

	showmouseptr();

// SET THE TEXT COLOR TO WHITE

	setcolor(WHITE);
	line(0,70,getmaxx(),70);
	line(0,400,getmaxx(),400);

	setcolor(14);
	outtextxy(65,115,"QUEUE OPERATIONS                      QUEUE STATUS");
	outtextxy(140,440,"Press the Key that is Highlighted or use Mouse.");
	setcolor(CYAN);
	line(65,125,190,125);
	line(368,125,464,125);

// CALL THE SHOW FUNCTION TO DRAW THE VARIOUS BUTTONS

	show();

// DRAW THE QUEUE

	drawq();

// DRAW THE POINTERS THAT IS FRONT AND REAR

	moveptrs(&rear,&front);

// SET THE LABEL M THAT WILL BE USED LATER FOR UNCONDITIONAL JUMP

	m:

// GO ON CHECKING WHETHER A KEY IS PRESSED OR NOT

	while (i==0)
	       {
	       if (kbhit())
	       {
	       ch=getche();

	       // CHECK IF THE KEY PRESSED IS E OR e
	       switch(ch)
	       {
	       case 'e':
	       case 'E':

	       {
	       // CALL THE FUNCTION THAT WILL BE FOR EXIT
	       zoomin();
	       }
	       reset:
	       case 'R':
	       case 'r':
	       {
	       // CALL THE RESET QUEUE FUNCTION
	       hidequeueinfo();
	       // CALL THE PROGRESSBAR
	       slidebar(&rear,&front);
	       // AGAIN DRAW THE QUEUE
	       drawq();
	       restrictmouseptr(1,57,maxx-1,maxy-1);
	       goto m;
	       }
	       queueinfo:
	       case 'A' :
	       case 'a':
	       {
	       // CALL THE FUNCTION TO GIVE INFORMATION ABOUT QUEUE
	       hidequeueinfo();
	       showqueuesize();
	       goto m;
	       }
	       delet:
	       case 'D':
	       case 'd':
	       {
	       //  CALL THE DELETE QUEUE OPERATIONS
	       hidequeueinfo();
	       deleteq(&front);
	       // CHECK IF QUEUE IS EMPTY, IF SO PRINT THE MESSAGE
		       if(rear==-1 || front==5 ||rear<front)
			{
			// CLEAR THE INFO, THERE IN INFORMATION PART
			hidequeueinfo();
			// DISPLAY THE TEXT IN THE INFORMATION PART
			outtextxy(200,430,"Queue is empty, you can't delete");
			}
			else
			{
			front=front+1;
			moveptrs(&rear,&front);
			}
		goto m;
	       }
	       ins:
	       case 'I':
	       case 'i':
	       {
	       // HERE IS THE OPERATIONS FOR INSERTING AN ELEMENT
	       hidequeueinfo();
	       if(rear==4)
			{
			hidequeueinfo();
			outtextxy(200,430,"Queue is full, you can't insert");
			goto m;
			}

	       // SHOWS THE NUMBERS AND CANCEL BUTTON
	       shownumbers(20,430,60,470);
	       // CHECK FOR WHAT KEY PRESSED

	       n:

	       while(j==0)
	       {
		 if (kbhit())
		  {
		   ch1=getche();

			       if(front==-1)
			       {
			       front=front+1;
			       }
		   switch(ch1)
		   {
		   cancel:
		   case 'C':
		   case 'c':
		       {
		       hidequeueinfo();
		       restrictmouseptr(1,57,maxx-1,maxy-1);
		       goto m;
		       }
		   zero:
		   case '0':
		       {
			       insertq(&rear,'0');
			       rear=rear+1;
			       moveptrs(&rear,&front);
			       hidequeueinfo();
			       restrictmouseptr(1,57,maxx-1,maxy-1);
			       goto m;
		       }
		   one:
		   case '1':
		       {
			       insertq(&rear,'1');
			       rear=rear+1;
			       moveptrs(&rear,&front);
			       hidequeueinfo();
			       restrictmouseptr(1,57,maxx-1,maxy-1);
			       goto m;
		       }
		   two:
		   case '2':
		       {
			       insertq(&rear,'2');
			       rear=rear+1;
			       moveptrs(&rear,&front);
			       hidequeueinfo();
			       restrictmouseptr(1,57,maxx-1,maxy-1);
			       goto m;
		       }
		   three:
		  case '3':
		       {
			       insertq(&rear,'3');
			       rear=rear+1;
			       moveptrs(&rear,&front);
			       hidequeueinfo();
			       restrictmouseptr(1,57,maxx-1,maxy-1);
			       goto m;
		       }
		  four:
		   case '4':
		       {
			       insertq(&rear,'4');
			       rear=rear+1;
			       moveptrs(&rear,&front);
			       hidequeueinfo();
			       restrictmouseptr(1,57,maxx-1,maxy-1);
			       goto m;
		       }
		   five:
		   case '5':
		       {
			       insertq(&rear,'5');
			       rear=rear+1;
			       moveptrs(&rear,&front);
			       hidequeueinfo();
			       restrictmouseptr(1,57,maxx-1,maxy-1);
			       goto m;
		       }
		   six:
		   case '6':
		       {
			       insertq(&rear,'6');
			       rear=rear+1;
			       moveptrs(&rear,&front);
			       hidequeueinfo();
			       restrictmouseptr(1,57,maxx-1,maxy-1);
			       goto m;
		       }
		   seven:
		   case '7':
		       {
			       insertq(&rear,'7');
			       rear=rear+1;
			       moveptrs(&rear,&front);
			       hidequeueinfo();
			       restrictmouseptr(1,57,maxx-1,maxy-1);
			       goto m;
		       }
		  eight:
		   case '8':
		       {
			       insertq(&rear,'8');
			       rear=rear+1;
			       moveptrs(&rear,&front);
			       hidequeueinfo();
			       restrictmouseptr(1,57,maxx-1,maxy-1);
			       goto m;
		       }
		   nine:
		   case '9':
		       {
			       insertq(&rear,'9');
			       rear=rear+1;
			       moveptrs(&rear,&front);
			       hidequeueinfo();
			       restrictmouseptr(1,57,maxx-1,maxy-1);
			       goto m;
		       }
		   default :
				hidequeueinfo();
				outtextxy(200,430,"Invalid Key Pressed ! Press a valid key");
				delay(550);
				hidequeueinfo();
				shownumbers(20,430,60,470);
				goto n;
		   }
		}
		else
		{
		//write code here to press the 0-----9 and cancel
		       getmousepos(&button,&x,&y);
		       if((button & 1)==1)
		       {
			       if(front==-1)
			       {
			       front=front+1;
			       }
		       }
		       if( (x>500 && x<getmaxx()-10 && y>445 && y<470) && (button & 1)==1)
			{
			goto cancel;
			}
		       if( (x>20 && x<60 && y>430 && y<470) && (button & 1)==1)
			{
			goto zero;
			}
		       if( (x>60 && x<100 && y>430 && y<470) && (button & 1)==1)
			{
			goto one;
			}
		       if( (x>100 && x<140 && y>430 && y<470) && (button & 1)==1)
			{
			goto two;
			}
		       if( (x>140 && x<180 && y>430 && y<470) && (button & 1)==1)
			{
			goto three;
			}
		       if( (x>180 && x<220 && y>430 && y<470) && (button & 1)==1)
			{
			goto four;
			}
		       if( (x>220 && x<260 && y>430 && y<470) && (button & 1)==1)
			{
			goto five;
			}
		       if( (x>260 && x<300 && y>430 && y<470) && (button & 1)==1)
			{
			goto six;
			}
		       if( (x>300 && x<340 && y>430 && y<470) && (button & 1)==1)
			{
			goto seven;
			}
		       if( (x>340 && x<380 && y>430 && y<470) && (button & 1)==1)
			{
			goto eight;
			}
		       if( (x>380 && x<420 && y>430 && y<470) && (button & 1)==1)
			{
			goto nine;
			}
		}
	}
	}
	 default :
			hidequeueinfo();
			outtextxy(220,420,"Please Press a Valid Key");
			goto m;
	 }
	 }
	 // ELSE CONDITION FOR THE IF AFTER THE FIRST WHILE
	 else
	       {

		  getmousepos(&button,&x,&y);
		  if( (x>50 && x<200 && y<370 && y>350) && (button & 1)==1)
			{
			//exit button click
			zoomin();
			}
		  if( (x>50 && x<200 && y>150 && y<170) && (button & 1)==1)
			{
			//clicked the queueinfo button
			goto queueinfo;
			}
		  if( (x>50 && x<200 && y>200 && y<220) && (button & 1)==1)
			{
			//clicked the insert button
			goto ins;
			}

		  if( (x>50 && x<200 && y>250 && y<270) && (button & 1)==1)
			{
			//clicked the delete button
			delay(500);
			goto delet;
			}
		  if( (x>50 && x<200 && y>300 && y<320) && (button & 1)==1)
			{
			//clicked the reset button
			goto reset;
			}

		}


	}

getche();
}