Thread: Array-Linked List Problem

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Array-Linked List Problem

    I was wondering how the format for converting a structured Array (in which the data is loaded from a text file) to a Linked List would look. And how i would edit the menu functions that run my program.

    code:
    (IN MAIN.CPP)
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <sstream>
    #include <string>
    #include <stdlib.h>
    #include "StudentRec.h"
    #include "DatabaseIO.h"
    #include "RecordProcessing.h"
    
    
    
    
    using namespace std;
    //Global Declaration
    
    
    StudentRec students[ISIZE];
    ifstream inFile;
    int arraysize;
    
    
    int main()
    {
    string buffer;
    int selection = 0;
    const int NOTOK = 1;
    
    read_file(students, "studentrecord.txt");
    SortStudentArray(students, ISIZE);
    
    while(selection != 5)
    {
    cout << "Please choose" << endl <<
    "(1) Add a student record" << endl <<
    "(2) Delete a student record" << endl <<
    "(3) Find a student's information" << endl <<
    "(4) Display all information in the database" << endl <<
    "(5) Exit Program" << endl;
    cin >> selection;
    
    if(!cin)
    {
    cout << "You have entered an invalid selection, Terminating program!..." << endl;
    exit(NOTOK);
    }
    
    if(selection < 0 or selection > 5)
    {
    cout << "Invalid selection, Terminating program!..." << endl;
    exit(NOTOK);
    }
    
    if(selection == 1)
    {
    add_a_record(students);
    }
    
    if(selection == 2)
    {
    StudentRec dummy;
    delete_a_record(dummy);
    }
    
    if(selection == 3)
    {
    string ID;
    cout << "Please enter Student ID: ";
    cin >> ID;
    int position;
    position = BinarySearch(students, 0, arraysize, ID);
    if (position == -1)
    { 
    cout << "Not Found" << endl;
    }
    else
    cout << students[position].Name << "\t";
    cout << students[position].ID << "\t";
    cout << students[position].GPA << endl;
    }
    
    if(selection == 4)
    {
    display_data(students);
    }
    
    if(selection == 5)
    {
    save_database(students, "studentrecord.txt");
    }
    } //Ends While loop
    
    } //Ends Int main
    
    (IN STUDENTREC.H)
    
    using namespace std;
    struct StudentRec
    {
    string Name;
    string ID;
    double GPA;
    };
    
    struct StudentListNode
    {
    StudentRec students;
    StudentListNode *next;
    };
    
    const int ISIZE = 100;
    ****(Function definitions left out)*****
    Last edited by Salem; 03-17-2010 at 11:26 AM. Reason: Added [code][/code] tags - didn't help, indenting is crap -

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    30
    suppose you have "N" student records, stored in array "stu[]", now you want a linked list with "head" as the pointer to the first element of the list, then

    Code:
    StudentListNode *ptr = head = new StudentListNode();
    ptr->students = stu[0];
    for(int i=1; i<N; i++){
      ptr -> next = new StudentListNode();
      ptr = ptr -> next;
      ptr -> students = stu[i];
    }
    ptr -> next = 0;
    It's pretty standard. But if you want to use a list, you could always use STL's list, instead of writing your own.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing an array to linked list
    By bar338 in forum C Programming
    Replies: 7
    Last Post: 04-08-2009, 09:15 PM
  2. I need help on this particular Linked List problem
    By sangken in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 12:26 AM
  3. Linked list problem
    By mr_glass in forum C Programming
    Replies: 4
    Last Post: 03-07-2006, 02:12 AM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM