How do I convert this Array Program into a linked list program??

This is a discussion on How do I convert this Array Program into a linked list program?? within the C++ Programming forums, part of the General Programming Boards category; Hey Guys what i need to do is turn my current menu operated structured array of student records ( NAME, ...

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

    How do I convert this Array Program into a linked list program??

    Hey Guys what i need to do is turn my current menu operated structured array of student records ( NAME, NAME, ID, GPA) that consists of Adding a student, finding a student, display all data in database, save data, load data, and deleting a student, into a linked list that has the same functionality as the structured array program. Can someone please help me?



    (IN MAIN.CPP)

    #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 beelyshe; 03-16-2010 at 07:28 AM.

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    Something like this... I'm sure someone will come in after me to put in their corrections.

    In a linked list, each node contains a pointer to the node created after it -- except that last node, which points to NULL.

    Basically you're going to be doing is creating a struct:

    Code:
    struct studentnode {  
    	std::string name,  
    	int id,  
    	int gpa,  
    	struct studentnode* next 
    };
    The first node:

    Code:
     
    struct studentnode* firstnode = new struct studentnode;  
    
    first->name = "Jerry"; 
    first->id = 1; 
    first->gpa = 3 
    first->next = NULL;  
    
    lastnode = firstnode;
    Then your second node:

    Code:
     
    struct studentnode* newnode = new struct studentnode;  
    
    newnode->name = "Bobby" 
    newnode->id = 2; 
    newnode->gpa = 4 
    newnode->next = NULL;  
    
    lastnode->next = newnode; 
    lastnode = newnode;
    The third node:

    Code:
     
    struct studentnode* newnode = new struct studentnode;  
    
    newnode->name = "Robbin" 
    newnode->id = 3; 
    newnode->gpa = 4 
    newnode->next = NULL  
    
    lastnode->next = newnode; 
    lastnode = newnode;
    And so on and so on...

    Jerry --> Bobby --> Robbin --> NULL

    Now, to iterate through the linked list:

    Code:
     
    struct studentnode* itr;  
    
    for (itr = firstnode; itr->next != NULL; itr = itr->next) {  
    	std::cout << "This students name is: " << itr->username << std::endl; 
    }
    And finally to remove Bobby:

    Code:
     
    struct studentnode* itr = NULL; 
    struct studentnode* prev = NULL;  
    
    for (itr = firstnode; itr->next != NULL; itr = itr->next) {  
    	if (itr->username == "Bobby") {   
    		if (itr == firstnode)    
    			firstnode = itr->next;   
    		else if (itr == lastnode)    
    			lastnode = prev;   
    		else    
    			prev->next = itr->next     
    
    		delete itr;   
    		break;  
    	}  
    
    	prev = itr; 
    }
    I havn't used linked lists in a long time, so I'm sure there are some mistakes in here,
    but hopefully it will give you a decent idea. Also, I'd recommend looking into vectors.

    edit: what happened to my code tags?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List and Array of Pointers
    By Nish in forum C Programming
    Replies: 4
    Last Post: 03-04-2005, 03:28 PM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  3. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 09:07 PM
  4. 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