Click here to Skip to main content
15,885,948 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code is not taking input in string object s1. I wrote a program that stores information of students using linked list. How do I sort the list in alphabetical order according to student name?

C++
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

struct node
{
       int roll;
       int link;
       int score;
       string name;
};
void traverse();
//void search();
void insert();
//void del();
node abc[50];
int start = -1;
int avail = 0;
main()
{
      int choice;
      for(int i=0; i<49; i++)
      abc[i].link = i+1;
      abc[49].link = -1;
      do
      {
      cout<<"\nPress\n1. Insertion\n2. Traversing\n3. Searching\n4. Delete:  ";
      cin>>choice;
      switch(choice)
      {
                    case 1: insert(); break;
                    case 2: traverse(); break;
                    //case 3: search(); break;
                    //case 4: del(); break;
                    default: cout<<"\nInvalid Input"<<endl;
                    
      }
      }while(1);
      system("pause");
}
void insert()
{
     int item1, item2; string s1;
     if(avail == -1)
     {
              cout<<"Overflow!"<<endl;
              return;
     }
     cout<<"Enter Roll Number: "; cin>>item1;
     cout<<"Enter Score: "; cin>>item2;
     cout<<"Enter student name: "; getline(cin, s1);
     
     //insertion at the beginning
     
     if(start == -1 || s1<abc[start].name)
     {
              int newnode = avail;
              avail = abc[avail].link;
              abc[newnode].roll = item1;
              abc[newnode].score = item2;
              abc[newnode].name = s1;
              abc[newnode].link = start;
              start = newnode;
              return;
     }
     
     //insertion somewhere in the mid
     
     int prev = start;
     for(int i = abc[start].link; i!=-1; i = abc[i].link)
     {
             if(s1<abc[i].name)
             {
                  int newnode = avail;
                  avail = abc[avail].link;
                  abc[newnode].roll = item1;
                  abc[newnode].score = item2;
                  abc[newnode].name = s1;
                  abc[newnode].link = i;
                  abc[prev].link = newnode;
                  return;
             }
             else
             prev = i;
     }
     
     //insertion at the end
     
     int newnode= avail;
     avail = abc[avail].link;
     abc[newnode].roll = item1;
     abc[newnode].score = item2;
     abc[newnode].name = s1;
     abc[newnode].link = -1;
     abc[prev].link = newnode;
     return;
}
void traverse()
{
     if(start ==-1)
     {
              cout<<"\nList Empty"<<endl;
              return;
     }
     cout<<"Student Name"<<setw(20)<<"Roll No."<<setw(20)<<"Score\n";
     for(int i = start; i!=-1; i=abc[i].link)
     {
             cout<<setw(12)<<abc[i].name;
             cout<<setw(20)<<abc[i].roll;
             cout<<setw(20)<<abc[i].score;
             cout<<"\n";
     }
     
}
Posted
Comments
[no name] 23-May-15 12:03pm    
What does the tile of your "question" have to do with your actual question? You write a sorting algorithm if you want to sort something.

1 solution

You might use (at least two) approaches:
  1. Homework one: pick your favourite sorting algorithm, and implement it (this should be pretty straightforward, possibly requiring just to implement the swap operation for two items of the list).
  2. Practical one: use the C++ standard library: std::list and std::sort should allow you to quickly provide the required code.
 
Share this answer
 
Comments
Frankie-C 24-May-15 5:35am    
+5 ;D
CPallini 24-May-15 11:03am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900