techpreparation-homepage

Home  Interview Questions  Certifications  Aptitude Questions  Tutorials  Placement Papers  Search  Resume  Soft Skills  Video  Forum  Blog


Technical Interview Questions

Visual Basic Interview Question
ASP .NET Interview Questions
C++ Interview Questions
C Interview Questions
                              .........More

Programming Source Codes
C/C++ Source Codes
C# Source Codes
                              .........More

Aptitude Interview Questions
C/C++ Aptitude Questions
C Aptitude Questions
                              .........More

Tutorials
C Tutorial
C++ Tutorial
                              .........More

Soft Skills
Communication Skills
Leadership Skills
                              .........More

 

 

  

C/C++ Source Codes

programme on implementation on queue

//queue
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Queue{
struct Queue *node;
char item[1];
};
struct Queue *volatile q;
int QueueAdd(const char *_item)
{
int _q;
struct Queue *queue;
_q = sizeof(struct Queue) + strlen(_item) + 4;
queue = (struct Queue *)malloc(_q); /* allocate the space in memory we need */
if (queue == 0) return 1;
memset(queue, 0, _q);
strcpy(queue->item, _item);
queue->node = q;
q = queue;

return 0;
}
void printq()
{
struct Queue *qq;

int n,j,num;
n=0;
for (qq=q, num=0; qq; qq=qq->node, num++); /* enumerate items in queue */
if(num==NULL)
{
printf("No items currently in the Queue");
return;
}
printf("Here are the items in the Queue \n");
cycle:
for (j=0,qq=q; (j < n) && qq; qq=qq->node, j++); /* lets hop to the next node */
printf("%s \n",qq->item);
n++;
if(n<num)
goto cycle;
}
int main()
{
struct Queue *qq;
int i,num;
char in[100]; /* array of 100 bytes */
/* print to stdout */
printf("Welcome to the Queue!");
printf("\n Dequeue not implemented \n");
printf("Usage: \n");
input: /* input label */
printf("\n1. Add to the queue\n");
printf("2. Print number of items in Queue \n");
printf("3. Print the items in the Queue \n");
printf("4. Exit the program \n");

scanf("%d",&i); /* check for inputted data */
switch(i)
{
case 1:
printf("Enter Item to add to the Queue \n");
scanf("%s",&in);

QueueAdd(in); /* call 'QueueAdd', let us assume everything goes o.k. */
printf("%s successfully added to the queue",in);
goto input;
break;
case 2:
for (qq=q, num=0; qq; qq=qq->node, num++);
/* Null statement above to enumerate items in the queue */
printf("There are %d items in the queue",num);
goto input;
break;
case 3:
printq();
goto input;
break;
case 4:
printf("Exiting program");
exit(0);
break; /* why not? :) */

default:
printf("Tu esta estupido!");
goto input;
}
}


<<<----- Return to C/C++ Source Code Questions Page.


Have a Question ? post your questions here. It will be answered as soon as possible.

Check Object Oriented Interview Questions for more Object Oriented Interview Questions with answers

Check Data Structure Interview Questions for more data structure interview questions with answers