Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h> // for random numbers
struct node // node for linked list
{
int f1;
float f2;
char f3;
struct node *next; // PTR to next node
};
struct node *start, *end; // first and last nodes in linked list
struct node *new, *TEMP, *p;
char RandomName(char randomNAME);
struct node tokenize(char line[]);
int runs = 1; // tracks runs, 1st run
// for stack:
void push(void), pop(void);
// for queue:
void dequeue(void), add(void), enqueue();
int main(int argc, char*argv[])
{
char runagain = 'Y';
while (runagain == 'Y') // entire program in loop
{
new = (struct node*)malloc(sizeof(struct node));
start = NULL;
end = NULL;
int whileloop = 1;
srand((unsigned)(time(NULL)));
int j = 0;
int random = 0; // random number
char *file = argv[1]; // file name is entered from command line
FILE *data;
char decision[10]; // will be either stack(S) or queue(Q)
char line[100]; // hold the line
int lcount = 0; // initialize line count
int i = 1;
char input[10]; // holds the action for the queue/stack
register int t;
// make a file
data = fopen(file, "w"); // open for writing
if (data == NULL)
{
printf("Cannot open %s\n", data);
exit(0);
}
for (j=0; j<10; j++) // write 10 random structs to file
{
new->next = NULL;
new->f1 = rand()%90+10; // random integer
new->f2 = (rand()%100)/9.9; // random float
new->f3 = RandomName(new->f3); // random name
fprintf(data, "%d %g %c\n", new->f1, new->f2, new->f3);
} // print each data field of new to file
fclose(data); // close file
printf("Create a stack(S) or a queue(Q): "); // get decision
scanf("%c", decision);
if (runs > 1)
scanf("%c", decision);
*decision = toupper(*decision); // converts to uppercase always
data = fopen(file, "r"); // open file for reading
// queue:
if (*decision == 'Q')
{
while(whileloop == 1) // will execute until quit
{
printf("Choose an action- Enqueue(E), Print(P), Dequeue(D), Quit(Q): ");
if (i==1)
gets(input); // have to gets() twice the first run,
gets(input); // or will print above line twice.
*input = toupper(*input); // converts to an uppercase char
switch(*input) // will go to one of 3 methods, or exit
{
case 'E':
if (fgets(line, sizeof(line), data) != NULL)
{
lcount++;
printf("%d. %s", lcount, line); // print line # and data
}
*new = tokenize(line);
enqueue();
break;
case 'P':
// print(idkyet, idk);
break;
case 'D':
dequeue();
printf("Struct dequeued was %d \n", start->f1);
break;
case 'Q':
whileloop = 0;
break;
default:
printf("Please enter an A, P, D or Q.\n"); // if none are entered
}
i++; // now only one gets()
}
}
// stack:
else if (*decision == 'S')
{
int i = 1;
while(whileloop == 1)
{
printf("Choose an action- PUSH(U), POP(O), Quit(Q): ");
if (i==1)
gets(input); // have to gets() twice the first run,
gets(input);
*input = toupper(*input); // converts to an uppercase char
switch(*input) // will go to one of 3 methods, or exit
{
case 'U':
if (fgets(line, sizeof(line), data) != NULL)
{
lcount++;
printf("%d. %s", lcount, line); // print line # and data
}
*new = tokenize(line);
push();
break;
case 'O':
pop();
break;
case 'Q':
whileloop = 0;
break;
default:
printf("Please enter a U, O or Q.\n"); // if none are entered
}
i++; // now only one gets()
}
}
else
printf("Error: please enter 'Q' or 'S' only! \n");
fclose(data); // close file
printf("Would you like to create another data structure: Y or N? ");
scanf("%s", &runagain); // will run again if 'y'
runagain = toupper(runagain); // always convert to uppercase
runs++; // increase # of runs
}
printf("BYE!!\n\n");
return 0;
}
char RandomName(char randomNAME)
{
...
return randomNAME;
}
struct node tokenize(char line[])
{
struct node *new;
new = (struct node*)malloc(sizeof(struct node));
.....................
new->f1 = (tempINT[0]*10 + tempINT[1]);
new->f2 = (tempFLOAT[0] + tempFLOAT[1]/10.0);
new->f3 = tempSTRING;
return *new;
} // new is now updated with values in the line of the file
// ********** STACK METHODS: ***********
void push(void) // pushes number onto stack
{
new->next = NULL;
if (start == NULL)
start = new;
else
{
p = start;
while (p->next != NULL) // traverses to last node
p = p->next;
p->next = new; // assigns last node's pointer to new node
}
}
void pop(void) // pops node from stack
{
if (start == NULL) // no nodes in list
printf("Stack is empty!\n");
else if (start->next == NULL) // only one node in list
{
printf("Deleted node was start node %d\n", start->f1);
free(start);
start = NULL;
}
else
{
p = start;
while (p->next != NULL)
{
TEMP = p;
p = p->next; // traverse to last node
}
printf("Deleted node was %d\n", p->f1);
TEMP->next = NULL; // new last node points to NULL
free(p); // free deleted node's memory space
}
}
// ********** QUEUE METHODS: ***********
void enqueue(void) // add number to queue
{
new->next = NULL;
if (start == NULL)
start = new;
else
{
end->next = new;
end = new;
printf("end is %d\n", end->f1);
}
}
void dequeue(void) // delete number from queue
{
struct node *temp1, *temp2;
if (start == NULL)
{
printf("Empty: cannot delete!.\n");
exit(1);
}
else
{
p = start;
printf("Deleted int is %d \n", p->f1);
start = start->next;
free(p); // free the memory allocated to del
}
}