Hello to you all ,

I am Trying to Implement Code for Void** Array which will Store
Strings in a QUEUE system .
I can see the Strings in the Queue .
I am sure i dis the Casting OK, But i need a fresh pair of eyes.
Also is there a way to REALLOC The array each Dequeue?

Thanx

Yotam , Israel

P.S Code Attached - Using Regular C Compiler (Borland)

Nope, no code was attached. Why don't you just post it using code tags anyways?

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

typedef struct Queue
{
 int first;
 int full_size;
 int present_size;
 void** place;
}queue, *q_ptr;

//__ PROTOTYPES __//
char* MakeString();

q_ptr Create(int);
void Enqueue(void* Element,q_ptr Q);
void Dequeue(q_ptr Q);
void GetSize(q_ptr Q);
void ShowQ_Str(q_ptr Q);

void main()
{
 q_ptr Q;
 int choise,limit;

clrscr();
printf("Welcome to HW8 - String Example for Void* Queue\n");
printf("Please Choose of the Following : \n");
printf("1. Work with String \n2. Exit from HW8\n");
scanf("%d",&choise);
switch (choise)
      {
	case 1: {
	       int op=0;
	       printf("Enter Size you Wish To Limit Queue\n");
	       scanf("%d",&limit);
	       Q=Create(limit);
	       while (op!=4)                    //Repeat Menu
	       {
		printf("Choose of the Following Operations : \n");
		printf("1.Enqueue\n2.Dequeue\n3.Get Size of Queue\n4.Exit\n");
		scanf("%d",&op);
		switch (op)
		{
		case 1: {                       //Enqueue
			 char* Str=NULL;
			 Str=MakeString();
			 Enqueue(&Str,Q);
			 flushall();
			 break;
			}
		case 2: { Dequeue(Q); break; }  //Dequeue
		case 3: { GetSize(Q); break; }  //Get Size
		case 4: { ShowQ_Str(Q); getch(); free(Q->place); free(Q); exit(1); break; }     //Exit Program
		}
	       } //End Menu Of Queue
	case 2: { printf("Bye Bye\n"); exit(1); break; } //Exit Program
	}
     }

}

void ShowQ_Str(q_ptr Q)
{
int j;
for (j=0;j<Q->present_size;j++)
  printf("Q Place [%d] is %s \n",j,*(char*)(Q->place[j]));

}

char* MakeString()
{
 char* temp;
 printf("Enter String\n");
 gets(temp);
 return temp;
}

q_ptr Create(int limit_size)
{
 q_ptr new_q;
 new_q=(q_ptr)malloc(sizeof(queue));
 if (new_q==NULL) { printf("Error in Queue Alloc , Bye \n"); exit(1); }
 new_q->first=new_q->present_size=0;
 new_q->full_size=limit_size;
 return new_q;
}

void Enqueue(void* Element,q_ptr Q)
{
 int i;
 if ( (Q->first==0) )
    {
    Q->place=(void**)malloc(sizeof(void*));
    if (Q->place==NULL)
	{ printf("No Q 4 U \n"); exit(1); }
    Q->place[Q->first]=Element;
    Q->present_size++;
    }
  else
   {
    if (Q->present_size!=Q->full_size)
      {
       Q->place=(void**)realloc(Q->place,Q->present_size+1);      //Make New Cell
       if (Q->place==NULL) { printf("No Realloc! \n"); exit(1); } //Mem ALLOC Check
       Q->present_size+=1;                                        //Present_Size increase
       for (i=Q->present_size-1;i>=0;i--)                         //Left Shifting in Q
	   Q->place[i]=Q->place[i-1];
       Q->place[Q->first]=Element;                                //Enqueue to Head Of Q
      }
    }
  }



void Dequeue(q_ptr Q)
{
 Q->place[Q->present_size]=NULL;
 Q->present_size--;
}
void GetSize(q_ptr Q)
{
 printf("Size of Queue is : %d\n",Q->present_size);
}
This article has been dead for over six months. Start a new discussion instead.