Linked List Help

This is a discussion on Linked List Help within the C++ Programming forums, part of the General Programming Boards category; I have read the tutorials on this site and have received a decent understanding of how the linked list works, ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    13

    Linked List Help

    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;
        }    
    }

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Is there any specific reason why you can't use an existing linked list? C doesn't have one in the standard library, but there must be a lot of implementations out there.

    To get the commands into the buffer, use memcpy or strcpy or a similar function.

    Edit: wait, this ain't the C board. Use a std::list of std::strings, for heaven's sake!

    Oh, and don't use void main. See the FAQ for why.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21