please help i'm screwed "linked list"

This is a discussion on please help i'm screwed "linked list" within the C Programming forums, part of the General Programming Boards category; hi, my desired output is something like this eros>>./robot >>>> >oops error: invalid command (oops) >>>> >print list is empty ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    5

    Question please help i'm screwed "linked list"

    hi, my desired output is something like this
    eros>>./robot
    >>>> >oops
    error: invalid command (oops)
    >>>> >print
    list is empty
    >>>> >add 100 100 get 5 bolts
    >>>> >print
    list contains 1 instruction:
    100 100 get 5 bolts
    >>>> >add 200 200 get 5 nuts
    >>>> >add 300 300 get 5 washers
    >>>> >print
    list contains 3 instructions:
    100 100 get 5 bolts
    200 200 get 5 nuts
    300 300 get 5 washers
    eros>>
    the program is stoped by pressing control d

    heres my code, any help would be much appreciated



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "/home/sit172/include/list.h" 
    
    struct command
    {
    char control[50];
    struct command *nextPtr;
    };
    
    typedef struct command COMMAND;
    typedef COMMAND *COMMANDPTR;
    
    void insert(COMMANDPTR *, char);
    void printlist(COMMANDPTR);
    
    int main(void)
    {
    	COMMANDPTR startPtr = NULL;
    	char commands[10];	
    
    	while(commands != "^d"){
    		gets(commands);
    	
    		
    	switch(commands){
    		case "print":
    			printlist(COMMANDPTR);
    			break;
    
    		case "add":
    			gets(command->control);
    			insert(&startPtr, command->control);	
    			break;
    
    		case "^d":
    			break;
    
    		default:
    			printf("error: invalid command (%s)", commands);
    	}	
    	}
    
    return(0);
    }
    
    void insert(COMMANDPTR *sPtr, char value){
    	COMMANDPTR newPtr, previousPtr, currentPtr;
    
    	newPtr = malloc(sizeof(COMMAND));
    		if(newPtr != NULL){
    			newPtr->data = value;
    			previousPtr = NULL;
    			currentPtr = *sPtr;
    		
    			while(currentPtr != NULL && value > currentPtr->data){
    				previousPtr = currentPtr;
    				currentPtr = currentPtr->nextPtr;
    			}	
    
    			if(previousPtr == NULL){
    				newPtr->nextPtr = *sPtr;
    				*sPtr = newPtr;
    				}
    				else{
    				previousPtr->nextPtr =newPtr;		
    				newPtr->nextPtr = currentPtr;
    				}
    			}
    		else{
    			printf("not enough memory");
    		}
    
    }
    
    void printlist(COMMANDPTR currentPtr){
    	if(currentPtr == NULL)
    		printf("the list is empty\n");
    	else{
    		while(currentPtr != NULL) {
    			printf("%s", currentPtr->data);
    			currentPtr = currentPtr->nextPtr;
    		}
    	}
    }

    there are to many errors to note. thanks guys.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > there are to many errors to note. thanks guys.
    Here's a tip - write a few lines, then compile and fix. Post when you get stuck
    Don't write a whole program, get lots of errors and dump it on a message board.

    > #include "/home/sit172/include/list.h"
    Nobody has this, so nobody can help you.

    Hint - you can't do switch/case on strings
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    5
    thanks, i fixed up the case statement and turned it into if statements.

    My problem is i'm not sure how use the string in the structure called control.

    i scan in add and it goes to the add function, and i want insert the rest of the line into a list.
    add 100 100 command

    i can scan add in, but how do i add 100 100 command to the list

    Thats one of the problems; i'm not pointing to my structure correctly or something along those lines.

    to store it i've tried using the insert function but i'm not sure about this either. thanks.

    Code:
    if(commands == "add"){
      	gets(command->control);
    	insert(&startPtr, command->control);	
    }
    ?

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    I'm pretty sure that
    Code:
    if(commands == "add")
    is completely wrong. Trying including <string.h> and doing this:

    Code:
    if (strcmp(commands, "adds")==0)   //commands is adds
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    I looked more closely at your last post. Are you trying to determine what numbers the person typed in after add? Look up strtok() in the string.h header file. You can scan the entire line of what the person typed in and separate it by spaces.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    5
    nah, i just want "add" stored into a string called commands, and then the rest of the line stored into my list.

    add hello my name is fred.
    scans in add
    and then "hello my name is fred" is first item in the list

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Post your latest code
    Oh, and read the FAQ about using (or more accurately NOT using) gets
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by jspri
    nah, i just want "add" stored into a string called commands, and then the rest of the line stored into my list.

    add hello my name is fred.
    scans in add
    and then "hello my name is fred" is first item in the list

    You can still use strtok() to separate it out.

    Code:
    char *add = strtok(commands, " ");
    char *theRest = strtok(NULL, "\r\n");
    assuming commands contains "add hello my name is fred" then add will be "add" and theRest will be "hello my name is fred".
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I screwed my pendrive
    By abh!shek in forum Tech Board
    Replies: 0
    Last Post: 06-30-2008, 01:32 PM
  2. Any suggestions for this mass screwed up comp?
    By KneeGrow in forum Tech Board
    Replies: 10
    Last Post: 11-16-2004, 02:59 PM
  3. Im so screwed
    By face_master in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 11-28-2002, 01:32 AM
  4. i was told something and it screwed me up!
    By DarkViper in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2002, 01:02 PM
  5. ARG.... My Texture Loader is screwed... PLEASE HELP!
    By minime6696 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-22-2002, 04:45 PM

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