1

I've written my own implementation of linked list which stores numbers. It was quite simple. Now I have to change it to store information about students. Every student that is added to the list must contain:

  • index

  • name,

  • surname,

  • university,

  • field of study,

  • year of study,

  • age

The index is sth like key, if we want to find the student in the list, we type index to find it and display full info about student. The same thing with deleting specified student - we type index to delete it.

Can anyone give me an advice or an example how to change it to store informations about students? I thought about asking about every info in constructor - when the object is created, it would display a query about the item and the object would be added to the list. :

linkedList::linkedList(){
head = NULL;
cout << "Type index: ";
cin >> index;
cout << "Type name: ";
cin >> name;
.
.
.
}

But I don't know if it would work properly and if it is generally good idea. Any advice would be helpful. Thank you in advance!

Here is the code that stores numbers in list:

class node{
public:
node();
int data;
node *next;
};

node::node(){
}

class linkedList{
node * head;
public:
linkedList();
node * createNode(int);
void insertBegin();
void insertPos();
void insertLast();
void deletePos();
void display();
void deleteList();
~linkedList();
};

linkedList::linkedList(){
head = NULL;
}

node *linkedList::createNode(int value){
node *temp;
temp = new(node);
temp->data = value;
temp->next = NULL;
return temp;
}

void linkedList::insertBegin(){
int value;
cout << "------------------------" << endl;
cout << "Type value: ";
cin >> value;
node *temp, *p;
temp = createNode(value);
if (head == NULL){
head = temp;
head->next = NULL;
}
else{
p = head;
head = temp;
head->next = p;
}
cout << "Element added." << endl;
cout << "------------------------" << endl;
}

void linkedList::insertLast(){
int value;
cout << "------------------------" << endl;
cout << "Type a value:: ";
cin >> value;
node *temp, *s;
temp = createNode(value);
if (head == NULL){
head = temp;
head->next = NULL;
} else{
s = head;
while (s->next != NULL){
  s = s->next;
}
temp->next = NULL;
s->next = temp;
}
cout << "Element added." << endl;
cout << "------------------------" << endl;
}

void linkedList::insertPos(){
int value, pos, counter = 0;
cout << "------------------------" << endl;
cout << "Type value: ";
cin >> value;
node *temp, *s, *ptr;
temp = createNode(value);
cout << "Type position: ";
cin >> pos;
cout << "------------------------" << endl;
int i;
s = head;
while (s != NULL){
s = s->next;
counter++;
}
if (pos == 1){
if (head == NULL){
head = temp;
head->next = NULL;
}
else{
ptr = head;
head = temp;
head->next = ptr;
}
}
else if (pos > 1  && pos <= counter){
s = head;
for (i = 1; i < pos; i++){
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else{
cout << "------------------------" << endl;
cout << "There is no such position. Type a value from 1 to " << counter << 
endl;
cout << "------------------------" << endl;
}
cout << "Element added." << endl;
cout << "------------------------" << endl;
}

void linkedList::deletePos(){
int pos, i, counter = 0;
if (head == NULL){
cout << "------------------------" << endl;
cout << "List is empty" << endl;
cout << "------------------------" << endl;
return;
}
cout << "------------------------" << endl;
cout << "Type position: ";
cin >> pos;
cout << "------------------------" << endl;
node *s, *ptr;
s = head;
if (pos == 1){
head = s->next;
}
else{
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos > 0 && pos <= counter){
s = head;
for (i = 1;i < pos;i++){
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else{
cout << "There is no such position. Type a value from 1 to " << counter <<                 
endl;
cout << "------------------------" << endl;
}
delete s;
cout << "Element added." << endl;
cout << "------------------------" << endl;
}
}

void linkedList::display(){
node *temp;
if (head == NULL){
cout << "------------------------" << endl;
cout << "List is empty" << endl;
cout << "------------------------" << endl;
return;
}
temp = head;
cout << "------------------------" << endl;
cout << "List elements:: " << endl;
while (temp != NULL){
cout << temp->data << "->";
temp = temp->next;
}
cout << "NULL" << endl;
cout << "------------------------" << endl;
}

void linkedList::deleteList (){
node *temp;
while (head != NULL){
temp = head;
head = head -> next;
delete temp;
}
cout << "------------------------" << endl;
cout << "List deleted." << endl;
cout << "------------------------" << endl;
}

linkedList::~linkedList(){
deleteList();
}
class menu{
public:
void displayMenu();
};

void menu::displayMenu(){
linkedList link;
int option;
do
{
cout << "1.Add element at the first pos" << endl;
cout << "2.Add element at the last pos" << endl;
cout << "3.Add element at the pos" << endl;
cout << "4.Delete specific pos" << endl;
cout << "5.Delete whole list" << endl;
cout << "6.Print list" << endl;
cout << "0.End " << endl;
cout << "Choose an option: ";
cin >> option;
switch(option){
case 1:
link.insertBegin();
break;
case 2:
link.insertLast();
break;
case 3:
link.insertPos();
break;
case 4:
link.deletePos();
break;
case 5:
link.deleteList();
break;
case 6:
link.display();
break;
}
} while (option != 0);
}

int main(){
menu k;
k.displayMenu();
return 0;
}
Ensz
  • 75
  • 4

2 Answers2

4

Use templates. C++ version of generic programming.

Define your linked list node with a template:

template<class T>
class node<T>{
   public:
     node<T>();
     T data;
     node<T> *next;
};

This means that you can now have a node of any kind of data type, just by accessing node's data attribute member.

Then define a new container class/struct to hold that other relevant information you were mentioning to include:

class data {
   public:
      /* ctor, dtor, accesor and modifier operations */
   private:
      unsigned int index, age;
      std:string name, surname, university, fieldOfStudy, yearOfStudy;

};

You would need to redefine your linkedList class to also use templates:

template<class T>
class linkedList<T> {
    /* ...declaration and implementation taking in to account <T> data type.
};

Then outside your scope, in main() for example, you can do the following:

int main() {
    linkedList<data> list;

    list.insertBegin();
    list.insertLast();

    // ... etc.

    return 0;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
Santiago Varela
  • 2,199
  • 16
  • 22
  • Thanks a lot. I'll try to do it with templates. – Ensz Mar 26 '17 at 17:59
  • Templates are quite tricky to implement at first and mostly show millions of errors when there is just **one simple error**. Always look at resolving the first one that appears. Also, declare and implement your class definitions and implementations in your **.h** files. It will save you a lot of trouble, trust me! http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – Santiago Varela Mar 26 '17 at 18:03
  • Sure, I will mark as correct. But I have one more question. I've just checked that it's my next task to make it with templates and first I have to make it without them. Do u have any idea how to do it without templates? – Ensz Mar 26 '17 at 18:12
  • @Ensz will another answer for that. – Santiago Varela Mar 26 '17 at 18:13
1

You could also NOT use generic programming/templates and make your node have specific data you want

This will also impact your code less, because when adding templates you would probably have to add and change more lines of code.

What you can do is to simply change your node class adding all the attributes that we had added to the data class, like so:

class node {
   public:
      /* ctor, dtor, accesor and modifier operations */
   private:
      node();
      node *next;
      unsigned int index, age;
      std:string name, surname, university, fieldOfStudy, yearOfStudy; // Now we have all the attributesyou needed and we omit the data attribute member.

};

You will need to make simple modifications to your linkedList methods, mainly only when you're instantiating node objects.

The problem with this solution is that it binds you to those attributes you chose above. The previous generic solution does not bind you to any data type and still all the linkedList functionality will work for any data type you decide to use (C++ int, double, float, std::string, custom classes).

Santiago Varela
  • 2,199
  • 16
  • 22