Help w/project + linked list Plz Read

This is a discussion on Help w/project + linked list Plz Read within the C++ Programming forums, part of the General Programming Boards category; Ok I have this project to do and its like impossible to do!? Im supposed to create a double linked ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    19

    Help w/project + linked list Plz Read

    Ok I have this project to do and its like impossible to do!?
    Im supposed to create a double linked list implemented as a class and have ( insert, delete, search, traverse ) The structure used for it should have fields for ( name, ssn, age & occupation ) The search, delete func perform their functions give a persons name. The program fills up the linked list using a data file. Then the program presents the user with options ( display a record, add a record, delete a record and exit ) The display, add, and delete operations should request a name and display an error if the name doesnt exist in the list. Then on exit the program writes the linked list back to the data file.

    A same file I guess looks like this
    Smith, Bob
    123-34-3333
    43
    Engineer

    Im soo lost on this project.

    The code for the class though is this:


    Code:
    class LL
    {
    private:
    record *head;
    record *tail;
    public:
    LL();
    int insert (record *);
    record *search(char[]);
    int delete_item(char[]);
    void traverse[];
    }
    LL:: ( LL )
    {
    head = NULL;
    tail = NULL;
    }

    I need u geniouses
    Last edited by havoq93; 10-25-2002 at 11:04 AM.

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    19
    So far Ive gotten this far: Can anyone help me one like the insert function and tell me if im even close to being right on this project? IM in desperate need!!!

    Code:
    #include "stdafx.h"
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    
    struct record
    {
        char name[50];
        char ssn[12];
        char age[4];
        char occupation[50];
        struct record *next;
        struct record *prior;
    };
    
    struct record *start;  /* pointer to first entry in list */
    struct record *last;  /* pointer to last entry */
    struct record *find(char *);
    
    
    Class LL {
    private:
    record *head;
    record *tail;
    public:
    LL();
    int insert (record *);
    record *search(char[]);
    int delete_item(char[]);
    void traverse[];
    };
    LL:: ( LL )
    {
    head = NULL;
    tail = NULL;
    };
    
    
    int LL:insert(     )
    
    
    
    void LL:traverse(     )
    
    
    
    
    
    void LL: delete_item(struct record **start, struct record **last)
    {
      struct record *info;
      char s[80];
    
      inputs("Enter name: ", s, 30);
      info = find(s);
      if(info) {
        if(*start==info) {
          *start=info->next;
          if(*start) (*start)->prior = NULL;
          else *last = NULL;
        }
        else {
          info->prior->next = info->next;
          if(info!=*last)
              info->next->prior = info->prior;
          else
            *last = info->prior;
        }
        free(info);  /* return memory to system */
      }
    }
    
    
    /* Look for a name in the list. */
    void search(void)
    {
      char name[50];
      struct record *info;
    
      printf("Enter name to find: ");
      gets(name);
      info = find(name);
      if(!info) printf("Not Found\n");
      else display(info);
    }
    
    
    /* Load the record file. */
    void load()
    {
      struct record *info;
      FILE *fp;
    
      fp = fopen("assign6", "rb");
      if(!fp) {
        printf("Cannot open file.\n");
        exit(1);
      }
    
      /* free any previously allocated memory */
      while(start) {
        info = start->next;
        free(info);
        start = info;
      }
    
      /* reset top and bottom pointers */
      start = last = NULL;
    
      printf("\nLoading File\n");
      while(!feof(fp)) {
        info = (struct record *) malloc(sizeof(struct record));
        if(!info) {
          printf("Out of Memory");
          return;
        }
        if(1 != fread(info, sizeof(struct record), 1, fp)) break;
        dls_store(info, &start, &last);
      }
      fclose(fp);
    }
    
    
    /* Save the file to disk. */
    void save(void)
    {
      struct record *info;
    
      FILE *fp;
    
      fp = fopen("assign6", "wb");
      if(!fp) {
        printf("Cannot open file.\n");
        exit(1);
      }
      printf("\nSaving File\n");
    
      info = start;
      while(info) {
        fwrite(info, sizeof(struct record), 1, fp);
        info = info->next;  /* get next address */
      }
      fclose(fp);
    }
    
    
    
    
    int main(int argc, char* argv[])
    {
    
     start = last = NULL;  /* initialize start and end pointers */
    
      for(; {
        switch(menu()) {
          case 1: display(); 
            break;
          case 2: add_rec(); 
            break;
          case 3: DeleteNode(); 
            break;
          case 4: exit(0); 
         
        }
      }
      return 0;
    }
    
    
    
    
    int menu ()
    {
        int item;
        // display menu and return output
    
        cout << "===============================" << endl;
        cout << "      Please Select An Option            " << endl << endl;
        cout << " 1. Display a record" << endl;
        cout << " 2. Add a record" << endl;
        cout << " 3. delete a record" << endl;
        cout << " 4. Exit" << endl;
        
        
        cin >> item;
        cin.ignore ();
    
        // return item
        return item;
    }

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    You posted too much code. Post the errors and their origins.

    Kuphryn

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    19
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(35) : error C2182: '<Unknown>' : illegal use of type 'void'
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(35) : warning C4200: nonstandard extension used : zero-sized array in struct/union
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(37) : error C2589: '(' : illegal token on right side of '::'
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(37) : error C2143: syntax error : missing ';' before '::'
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(38) : error C2143: syntax error : missing ';' before '{'
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(38) : error C2447: missing function header (old-style formal list?)
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(44) : error C2239: unexpected token ':' following declaration of 'LL'
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(48) : error C2612: trailing ''<L_ALIGN>'' illegal in base/member initializer list
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(195) : fatal error C1004: unexpected end of file found
    Error executing cl.exe.


    Sorry about all the code, but its as far as I have gotten and I have no idea if its right or not. I need some major help, cuz I suck at programming. Plus I need an insert function which I dont know how to write.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    19
    ok i fixed those, now I get these errors

    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(37) : error C2143: syntax error : missing ';' before ':'
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(37) : error C2143: syntax error : missing ';' before ':'
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(39) : error C2065: 'head' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(40) : error C2065: 'tail' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(41) : warning C4508: 'LL' : function should return a value; 'void' return type assumed
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(44) : error C2373: 'LL' : redefinition; different type modifiers
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(37) : see declaration of 'LL'
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(44) : error C2239: unexpected token ':' following declaration of 'LL'
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(50) : error C2182: 'LL' : illegal use of type 'void'
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(50) : error C2373: 'LL' : redefinition; different type modifiers
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(37) : see declaration of 'LL'
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(50) : error C2239: unexpected token ':' following declaration of 'LL'
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(63) : error C2146: syntax error : missing ';' before identifier 'start'
    C:\Program Files\Microsoft Visual Studio\MyProjects\assign6\assign6.cpp(63) : fatal error C1004: unexpected end of file found


    the only code i have now is

    Code:
    #include "stdafx.h"
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    
    struct record
    {
    	char name[50];
    	char ssn[12];
    	char age[4];
    	char occupation[50];
    	struct record *next;
    	struct record *prior;
    };
    
    struct record *start;  /* pointer to first entry in list */
    struct record *last;  /* pointer to last entry */
    struct record *find(char *);
    
    
    class LL {
    private:
    record *head;
    record *tail;
    public:
    LL();
    int insert (record *);
    record *search(char[]);
    int delete_item(char[]);
    void traverse(void);
    };
    LL:LL ( )
    {
    head = NULL;
    tail = NULL;
    }
    
    
    int LL:insert( /* add parameters here */ )
    {
    
    }
    
    
    void LL:traverse( /* add parameters here */   )
    {
    
    }
    
    
    
    
    
    
    
    int main(int argc, char* argv[])
    
    start = last = NULL;  /* initialize start and end pointers */
    
    
    do {
    
    	
    	
    	temp = menu ();
        switch(temp)
    	{
    
          case 1: display(); 
            break;
          case 2: add_rec(); 
            break;
          case 3: DeleteNode(); 
            break;
          case 4: exit(0); 
         
        }
      }
    
      return 0;
    }

    This project is just tuff. I dunno if im even doing it right. I had someone help me on the code. Who knows. Im lost

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    19
    well I think I got the class workin,

    it was
    LL:LL ( )

    it was supposed to be

    LL::LL ( )


    Can I get help on the insert function? can someone write that?

    Heres my working code so far:

    Code:
    #include "stdafx.h"
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    
    struct record
    {
    	char name[50];
    	char ssn[12];
    	char age[4];
    	char occupation[50];
    	struct record *next;
    	struct record *prior;
    };
    
    struct record *start;  /* pointer to first entry in list */
    struct record *last;  /* pointer to last entry */
    struct record *find(char *);
    
    
    void display(void), add_rec(void), delete_item(void);
    int menu();
    
    class LL 
    {
    private:
    record *head;
    record *tail;
    public:
    LL();
    int insert (record *);
    record *search(char[]);
    int delete_item(char[]);
    void traverse(void);
    };
    LL::LL ( )
    {
    head = NULL;
    tail = NULL;
    }
    
    
    
    
    
    
    int main(int argc, char* argv[])
    {
    
    	int temp =0;
    
    do {
    	
    	
    	temp = menu ();
        switch(temp)
    	{
    
          case 1: display(); 
            break;
          case 2: add_rec(); 
            break;
          case 3: delete_item(); 
            break;
          case 4: exit(0); 
            break;
    
    	  default:
    		  cout << temp << " is invalid!" << endl;
    		  }
    } while (temp != 4);
    
    return 0;
    
    }
    
    int menu ()
    {
        int item;
        // display menu and return output
    
        cout << "===============================" << endl;
        cout << "      Please Select An Option            " << endl << endl;
        cout << " 1. Display a record" << endl;
    	cout << " 2. Add a record" << endl;
    	cout << " 3. delete a record" << endl;
    	cout << " 4. Exit" << endl;
        
        
        cin >> item;
        cin.ignore ();
    
        // return item
        return item;
    }

    Oh and I have no idea how to write the code to fill up the double linked list, using a data file. I need help big time on that, thanx guys ! Or if anyone wants to write all the code feel free LOL, id pay hehe sorry its just due tuesday so... and im getting desperate, last project !
    Last edited by havoq93; 10-25-2002 at 12:18 PM.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    19
    ok this is my code so far again!

    Im I on the right track? Good god !

    Code:
    struct record
    {
    	char name[50];
    	char ssn[12];
    	char age[4];
    	char occupation[50];
    	struct record *next;
    	struct record *prior;
    };
    
    struct record *start;  /* pointer to first entry in list */
    struct record *last;  /* pointer to last entry */
    struct record *find(char *);
    
    
    void list(void);
    
    void search(void);
    void display(struct record *);
    void dls_store(struct record *i, struct record **start, struct record **last);
    
    int menu();
    
    class LL 
    {
    private:
    record *head;
    record *tail;
    public:
    LL();
    int insert(record data);
    record *search(char[]);
    int delete_item(char[]);
    void traverse(void);
    };
    LL::LL ( )
    {
    head = NULL;
    tail = NULL;
    }
    
    
    /* Load the record file. */
    void load()
    {
      struct record *info;
      FILE *fp;
    
      fp = fopen("assign6", "rb");
      if(!fp) {
        printf("Cannot open file.\n");
        exit(1);
      }
    
      /* free any previously allocated memory */
      while(start) {
        info = start->next;
        free(info);
        start = info;
      }
    
      /* reset top and bottom pointers */
      start = last = NULL;
    
      printf("\nLoading File\n");
      while(!feof(fp)) {
        info = (struct record *) malloc(sizeof(struct record));
        if(!info) {
          printf("Out of Memory");
          return;
        }
        if(1 != fread(info, sizeof(struct record), 1, fp)) break;
        dls_store(info, &start, &last);
      }
      fclose(fp);
    }
    
    
    
    /* Create a doubly linked list in sorted order. */
    int LL::insert ( record data ) 
    {
      record *newnode = new record;  // make a new node
      *newnode = data;  // copy the data
      newnode->next = NULL;
      newnode->prior= NULL; // initialise the links
      if ( head == NULL ) {
        // list is empty, and now there is one
        head = tail = newnode;
      } else {
        // add node to the head of the list
        newnode->next = head;
        head->prior = newnode;
        head = newnode;
      }
      return 0;
       }
    
    
    
    
    
    /* Display the entire list. */
    void list(void)
    {
      struct record *info;
    
      info = start;
      while(info) {
        display(info);
        info = info->next;  /* get next address */
      }
      printf("\n\n");
    }
    
    
    /* This function actually prints the fields in each record. */
    void display(struct record *info)
    {
        printf("%s\n", info->name);
        printf("%s\n", info->ssn);
        printf("%s\n", info->age);
        printf("%s\n", info->occupation);
        printf("\n\n");
    }
    
    
    int menu ()
    {
        int item;
        // display menu and return output
    
        cout << "===============================" << endl;
        cout << "      Please Select An Option            " << endl << endl;
        cout << " 1. Display a record" << endl;
    	cout << " 2. Add a record" << endl;
    	cout << " 3. delete a record" << endl;
    	cout << " 4. Exit" << endl;
        
        
        cin >> item;
        cin.ignore ();
    
        // return item
        return item;
    }
    
    
    int main(int argc, char* argv[])
    {
    
    	int temp =0;
    
    do {
    	
    	
    	temp = menu ();
        switch(temp)
    	{
    
          case 1: list();
    	    break;
         
          case 4: exit(0); 
            break;
    
    	  default:
    		  cout << temp << " is invalid!" << endl;
    		  }
    } while (temp != 4);
    
    return 0;
    
    }
    Arghh, im at a point we're Id even pay someone for code
    Last edited by havoq93; 10-25-2002 at 02:43 PM.

  8. #8
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Unhappy

    Is this for a data structures class- if so you should know better
    Mr. C: Author and Instructor

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    19
    no, Salem? Can ya help?
    Last edited by havoq93; 10-26-2002 at 01:29 PM.

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    19
    Seriously, cant anyone help? No one is responding and I got 2 days left not even..

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    19
    This cant be that hard of a project for some of u smart programmers
    Cant anyone help or take money pleaze?

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    If only Havoq had AIM

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >This cant be that hard of a project for some of u smart programmers
    Probably because we've learned to use little programs to solve the problems in bigger ones. How about instead of trying to get the entire program done at once, write just the list and test it thoroughly. Then once you know it works you can use it with confidence.

    >Cant anyone help or take money pleaze?
    Don't offer us money to do your homework, it's insulting. I'll be glad to help when I have time to heavily modify your code so that it is testable. I'm a bit pressed for time right now.

    -Prelude
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Oct 2002
    Posts
    19
    I have AIM, u can contact me with chugger93

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 01:56 PM
  2. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-28-2008, 12:24 AM
  3. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 09:38 PM
  4. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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