[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.3

Stack and Queue implemented with dynamic arrays in C

Asked by xiromdim in C Programming Language, Algorithms

We have a file e.g. cars0.txt (as seen in the code snippet). We read each line of the file, if there is S then we push plateNumber to Stack, if there is a Q, we enqueue plateNum to Queue.
Stack and Queue are implemented with dynamic arrays.
When the stack is full I export it to files with increasing numbers...the same with queue.
The problem I have is: The program reads twice the last line of the cars0.txt file, and then crashes...
If I solve the problem with the double reading I think crashing will be fixed easily
BTW tell me for a good debugger...because the one from DevCpp, is not so good I think
Thanks in advance
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 
/* node structure */
typedef struct node {
   char *Name;       /* CAR ID */
} NODE;
 
/* stack structure */
typedef struct Stack {
   int Capacity;   /* the max elements stack may hold */
   int TopOfStack; /* index to the top element */
   NODE *Array;    /* array holding elements */
} STACK;
 
/* queue structure */
typedef struct Queue {
   int Capacity; /* the max elements queue may hold */
   int Front;    /* index to the first element */
   int Rear;     /* index to the last element */
   int Size;     /* current queue size */
   NODE *Array;  /* array holding elements */
} QUEUE;
 
void initStack(STACK *S);
void push(STACK *S, char* plate);
int stackIsFull(STACK *S);
void exportStackToFile(STACK *S, char* exportStackFileName);
NODE pop(STACK *S);
int isEmptyStack(STACK *S);
char* createStackFileName(char* stackFileName, int stackFileIndex);
 
int isEmptyQueue(QUEUE *Q);
NODE remove(QUEUE *Q);
void initQueue(QUEUE *Q);
void add(QUEUE *Q, char* plate);
int  queueIsFull(QUEUE *Q);
char* createQueueFileName(char* qFileName, int queueFileIndex);
void printq(QUEUE *Q);
 
int main(){
   FILE *fp,*stackFilePtr, *queueFilePtr;
   STACK S;
   QUEUE Q;
   char plate[11], ferry[2];
   NODE tempNode;
   char stackFileName[20]="",queueFileName[20]="";
   char arg4[20]="out0_s",arg5[20]="out0_q";
   int stackFileIndex=0,queueFileIndex=0;
   
   fp=fopen("cars0.txt","r");
   if (fp==NULL){printf("cannot open file!");return 1;}
   
   initStack(&S);
   initQueue(&Q);   
   //export file to stack and queue ferries
   while(!feof(fp)){
      fscanf(fp, "%s %s",plate, ferry);
      if (strcmp(ferry, "S")==0){
            //if S, push car to ferry S
            push(&S, plate);
            //if stack is full, export stack to file
            if (stackIsFull(&S)){
               puts("full stack!!");getchar();
               strcpy(stackFileName, arg4);strcpy(stackFileName, createStackFileName(stackFileName,stackFileIndex));
               stackFilePtr = fopen(stackFileName, "w");if (stackFilePtr==NULL){puts("error opening file");getchar();exit(1);}
               while(!isEmptyStack(&S)){
               //pop(&S) returns a NODE
               tempNode = pop(&S);
               fprintf(stackFilePtr,"%s\n",tempNode.Name);
               } 
               fclose(stackFilePtr);
               stackFileIndex++;
               //initialize stackFileName to ""
               stackFileName[0]='\0';    
            } 
      }
      else if (strcmp(ferry,"Q")==0){
         printf("plate from file: %s",plate);getchar(); 
         add(&Q,plate);
         if(queueIsFull(&Q)){
            puts("full queue!");
               strcpy(queueFileName, arg5);
               strcpy(queueFileName, createQueueFileName(queueFileName,queueFileIndex));
               queueFilePtr = fopen(queueFileName, "w");if (queueFilePtr==NULL){puts("error opening file");getchar();exit(1);}
               while(!isEmptyQueue(&Q)){
               //remove returns a NODE
               tempNode = remove(&Q);
               fprintf(queueFilePtr,"%s\n",tempNode.Name);
               } 
               fclose(queueFilePtr);
               queueFileIndex++;
               //initialize queueFileName to ""
               queueFileName[0]='\0'; 
               initQueue(&Q);   
            } 
   }
}
               //if car file has ended and stack has still elements, export the rest elements to the file   
               if(!isEmptyStack(&S)){
                  strcpy(stackFileName, arg4);
                  strcpy(stackFileName, createStackFileName(stackFileName,stackFileIndex));
                  stackFilePtr = fopen(stackFileName, "w");if (stackFilePtr==NULL){puts("error opening file");getchar();exit(1);}
                  while(!isEmptyStack(&S)){
                  //pop(&S) returns a NODE
                  tempNode = pop(&S);
                  fprintf(stackFilePtr,"%s\n",tempNode.Name);
                  }
               }
               //if car file has ended and queue has still elements, export the rest elements to the file   
               if(!isEmptyQueue(&Q)){
                  strcpy(queueFileName, arg5);
                  strcpy(queueFileName, createQueueFileName(queueFileName,queueFileIndex));
                  queueFilePtr = fopen(queueFileName, "w");if (queueFilePtr==NULL){puts("error opening file");getchar();exit(1);}
                  while(!isEmptyQueue(&Q)){
                  //remove(&Q) returns a NODE
                  tempNode = remove(&Q);
                  fprintf(queueFilePtr,"%s\n",tempNode.Name);
                  } 
               }
   fclose(stackFilePtr);
   fclose(queueFilePtr);         
   fclose(fp);
   printf("press any key to continue...");
   getchar();
   return 0;
   
}
void add(QUEUE *Q, char* plate){
   
   if (Q->Size==0){//empty queue
      printf("Q is empty");getchar();
      Q->Front=Q->Rear=0;
      Q->Array = (NODE*)malloc(sizeof(NODE));//allocate Array[0]
      Q->Array[Q->Front].Name=(char*)malloc(11*sizeof(char));
      strcpy(Q->Array[Q->Front].Name,plate);//Array[0]=plate
      printf("Name= %s",Q->Array[Q->Front].Name);getchar();
      Q->Size=1;printf("add completed!");getchar();
      return;
   }
   printf("queue not empty");getchar();
   Q->Rear++;
   Q->Size++;
   //reallocating Array with 1 more element (size elements total)
   Q->Array = (NODE*)realloc(Q->Array, Q->Size*sizeof(NODE));printf("reallocation ok!");getchar();
   //allocating memory for char* Name
   printf("rear= %d",Q->Rear);getchar();
   Q->Array[Q->Rear].Name=(char*)malloc(11*sizeof(char));printf("array[rear].name= %s",Q->Array[Q->Rear].Name);getchar();
   strcpy(Q->Array[Q->Rear].Name,"");printf("array[rear].name= %s",Q->Array[Q->Rear].Name);getchar();
   strcpy(Q->Array[Q->Rear].Name, plate);
   printf("rear.name= %s",Q->Array[Q->Rear].Name);getchar();
   printf("add completed!");getchar();
   return;
}
void printq(QUEUE *Q){
   printf("size= %d front= %d rear= %d  ",Q->Size, Q->Front, Q->Rear);
   }
int isEmptyQueue(QUEUE *Q){
   if (Q->Size == 0) return 1;
     else return 0;
}
int queueIsFull(QUEUE *Q){
   if (Q->Size == Q->Capacity) return 1;
      else return 0;
}
void initQueue(QUEUE *Q){
   printf("init queue running..");getchar();
   Q->Capacity = 2;
   Q->Front=0;
   Q->Rear=0;
   Q->Size=0;
   Q->Array=NULL;
   printf("Queue initialized!!\n");getchar();
}
 
 
NODE remove(QUEUE *Q){
   
   NODE element;
   int i;
   //element = the element to remove from queue
   element=Q->Array[Q->Front];
   //shift elements of Array
   for(i=0; i<Q->Size-1; i++){
      Q->Array[i] = Q->Array[i+1];
   }
   //realloc array of plates
   Q->Array = (NODE*)realloc(Q->Array, (Q->Size-1)*sizeof(NODE));
   Q->Rear--;
   Q->Size--;
   return element;
}
char* createQueueFileName(char* qFileName, int queueFileIndex){
   
   char queueFileIndexStr[10];
   char temp[20]="";
   sprintf(queueFileIndexStr,"%d",queueFileIndex);printf("queueFileIndexStr: %s\n",queueFileIndexStr);getchar();
   strcat(temp,qFileName);
   printf("queueFileName: %s\n",temp);getchar();
   strcat(temp,queueFileIndexStr);printf("queueFileName: %s\n",temp);getchar();
   strcat(temp,".txt");printf("queueFileName: %s\n",temp);getchar();
   strcpy(qFileName, temp);
   return qFileName;
}
char* createStackFileName(char* sFileName, int stackFileIndex){
   
   char stackFileIndexStr[10];
   char temp[20]="";
   sprintf(stackFileIndexStr,"%d",stackFileIndex);printf("stackFileIndexStr: %s\n",stackFileIndexStr);getchar();
   strcat(temp,sFileName);
   printf("stackFileName: %s\n",temp);getchar();
   strcat(temp,stackFileIndexStr);printf("stackFileName: %s\n",temp);getchar();
   strcat(temp,".txt");printf("stackFileName: %s\n",temp);getchar();
   strcpy(sFileName, temp);
   return sFileName;
}
   
   
NODE pop(STACK *S){
   NODE tempNode;
   
   tempNode=S->Array[S->TopOfStack];
   S->TopOfStack--;
   return tempNode;
}
int isEmptyStack(STACK *S){
   
   if (S->TopOfStack == -1)
     return 1;
   else return 0;
}  
void initStack(STACK *S){
   int i;
   
   S->Capacity = 2;
   S->TopOfStack = -1;
   S->Array = (NODE*) malloc(S->Capacity * sizeof(NODE));
   if (S->Array == NULL) {printf("error stack malloc!"); exit(1);}
   printf("stack initialized!!\n");
}
void push(STACK *S, char* plate){
   int i;
   
   //top++
   S->TopOfStack++;printf("top= %d plate= %s ",S->TopOfStack,plate);getchar();
   //malloc Array[topOfStack].Name to assign plateNUmber read from file
   S->Array[S->TopOfStack].Name = (char*) malloc(11* sizeof(char));
   //make the assignment
   strcpy(S->Array[S->TopOfStack].Name, plate);//Name = plate
   //print Name to test
   printf("Array[top]= %s ",S->Array[S->TopOfStack].Name);getchar();
   
}//end push
int stackIsFull(STACK *S){
   if (S->TopOfStack == S->Capacity - 1) 
      return 1;
   else return 0;
}
Attachments:
 
cars file
 
 
Related Solutions
Keywords: Stack and Queue implemented with dy…
 
Loading Advertisement...
 
[+][-]11/22/08 12:19 PM, ID: 23022114Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/22/08 12:20 PM, ID: 23022118Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: C Programming Language, Algorithms
Sign Up Now!
Solution Provided By: ozo
Participating Experts: 2
Solution Grade: A
 
 
Loading Advertisement...
20091021-EE-VQP-81 - Hierarchy / EE_QW_2_20070628