I have read the tutorials on this site and have received a decent understanding of how the linked list works, but when I try to actually implement code - well... I kind of get lost. Mainly because what I need to do with my linked list it does not really have an example on how to implement it.
I am in school and for my datastructures class we are constructing a simple DBMS system. I have come to the part of my program where I need to implement a linked list so whenever the user types in "list" it shows a list of all the commands the user has typed in; if they type in "list first" it shows the first command they ever entered, and if they type in "list last" it shows the last command they ever entered.
I mostly just need a few pointers (no pun intended) on how to actually implement a linked list itself for my purposes. I read the tutorials and practiced, which made it seem simple enough, but when I open up my code and try to implement it my mind gets all muddied, so to speak. Mainly I do not understand how I store my commands into the buffer char command[64] I have made.
Code:
#include <iostream>
#include <string>
using namespace std;
char inputLine[64];
int index = 0;
char buffer[64];
char bufferIndex = 0;
char* token;
char* getToken() ;
void parseFieldName();
int isValidFieldName();
void parseTableName();
int isValidTableName();
void parseFrom();
// Linked List Variables
struct node {
char command[64];
node *next;
node *prev;
};
struct node *head = NULL;
struct node *tail = NULL;
void addNode(char* cmd) {
node *n1 = (node*)malloc(sizeof(node)); // allocate memory for node
}
int isWhiteSpace(char w) {
if(w == ' ' || w == '\t' || w == '\r')
return 1;
else
return 0;
}
void parseSelect() {
if(strcmp(token,"select")==0) {
cout << "SELECT command parsed" << endl;
parseFieldName();
} else {
if(strcmp(token,"from")==0) {
cout << "Illegal FROM - Missing SELECT" << endl;
} else {
cout << "Missing SELECT command" << endl;
}
}
}
void parseFieldName() {
token=getToken();
if(isValidFieldName()) {
if(strcmp(token,"*")==0) {
cout << "* command parsed" << endl;
token = getToken();
parseFrom();
} else {
if(strcmp(token,"from")!=0) {
//token assumed field name
token=getToken();
//token is either FROM or , or error
while(strcmp(token,"from")!=0) {
if(strcmp(token,",")==0) {
token = getToken();
if(isValidFieldName()) {
token = getToken();
}
else {
cout << "Missing field name" << endl;
return;
}
}
else {
cout << "Missing , or FROM" << endl;
return;
}
}
}
cout << "Field Name(s) Parsed" << endl;
parseFrom();
}
}
}
void parseTableName() {
token=getToken();
if(strcmp(token,"")==0) {
cout << "Missing table name" << endl;
} else {
if(isValidTableName()){
cout << "Table Name Parsed" << endl;
}
}
}
void parseFrom() {
if(strcmp(token,"from")==0) {
cout << "FROM command parsed" << endl;
parseTableName();
} else {
if(strcmp(token,"select")==0) {
cout << "Illegal SELECT - Missing FROM" << endl;
} else {
cout << "Missing FROM command" << endl;
}
}
}
int isValidFieldName(){
if(strcmp(token,"select")==0){
cout << "Illegal SELECT - Invalid field name" << endl;
return 0;
}
if(strcmp(token,"from")==0){
cout << "Illegal FROM - Invalid field name" << endl;
return 0;
}
return 1;
}
int isValidTableName(){
if(strcmp(token,"select")==0){
cout << "Illegal SELECT - Invalid table name" << endl;
return 0;
}
if(strcmp(token,"from")==0){
cout << "Illegal FROM - Invalid table name" << endl;
return 0;
}
return 1;
}
char* getToken() {
if(inputLine[index] == NULL) {
index = 0;
buffer[0]=0;
return buffer;
}
/*if(inputLine[index] == "list") {
}*/
if(inputLine[index] == ',') {
index++;
buffer[0] = ',';
buffer[1] = 0;
return buffer;
}
while(isWhiteSpace(inputLine[index])) {
index++;
if(inputLine[index] == NULL) {
index = 0;
buffer[0]=0;
return buffer;
}
}
bufferIndex = 0;
while(!isWhiteSpace(inputLine[index]) && inputLine[index] != NULL && inputLine[index] != ',') {
buffer[bufferIndex] = tolower(inputLine[index]);
index++;
bufferIndex++;
}
buffer[bufferIndex]=0;
return buffer;
}
void main() {
cout << "John Clancy's Database Management System" << endl << endl;
while(1) {
cout << "Command: ";
cin.getline(inputLine,64);
index=0;
token = getToken();
if(strcmp(token,"")) {
if(strcmp(token,"quit")==0) {
break;
}
}
cout << endl;
parseSelect();
cout << endl;
}
}