Thread: Explaining the Data Structure

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    34

    Explaining the Data Structure

    Hi i'm trying to read out the some code that i search in google. And i try to differentiate the code whether its using what kind of data structure.. Its using binary tree, queue, tree or vector or others?
    Code:
    #include<iostream.h>
    #include<time.h>
    #include<math.h>
    #include<iomanip>
    
    
    
    class studentrecord
    {
    public:
        studentrecord();
        char fname[10];
        char lname[10];
        int ID;
        char programme[10];
        int year_of_study;
        studentrecord *nxt;
        void register_student();
        void display_all();
        void displayStudentInformation();
        void Swap();
        void SearchStudent(); 
        void DeleteStudent();
        void ConditionalDisplay();
    };
    studentrecord::studentrecord()
    {
    }
    studentrecord *start_ptr=NULL;
    int khetho=0;
    
    void studentrecord::register_student()
    {
        studentrecord *temp,*temp2;
        temp=new studentrecord;
        cout<<"enter the student fist name:"<<endl;
        cin>>temp->fname;
        cout<<"enter student last name:"<<endl;
        cin>>temp->lname;
        cout<<"enter student id:"<<endl;
        cin>>temp->ID;
        cout<<"Enter the Programme of the student:"<<endl;
        cin>>temp->programme;
        cout<<"Enter student's year:"<<endl;
        cin>>temp->year_of_study;
        temp->nxt=NULL;
    
        
        if(start_ptr==NULL)
        {
            start_ptr=temp;
        }
        else
        {
            temp2=start_ptr;
    
      while(temp2->nxt!=NULL)
      {
          temp2=temp2->nxt;
      }
      temp2->nxt=temp;
        }
    }
    void studentrecord::display_all()
    {
        studentrecord *temp;
       temp=start_ptr;
    if(start_ptr==NULL)
    {
        cout<<"the record is empty"<<endl;
    }
    else
    {
    
    while(temp!=NULL)
    {
        cout<<"last name is:"<<temp->lname<<endl;
        cout<<"ID:"<<temp->ID<<endl;
        cout<<"first name is:"<<temp->fname<<endl;
        cout<<"the programme is:"<<temp->programme<<endl;
        cout<<endl;
        cout<<endl;
    
        temp=temp->nxt;
    }
    }
    }
    
    void studentrecord::Swap()
    {   studentrecord *temp,*temp2,*temp3,*temp4; 
           temp= start_ptr;
           cout<<endl;
           int id,id1; //count;
           cout<<"enter ID u want to swap "<<endl;
           cin>>id;
           cout<<"enter 2nd ID u want to swap "<<endl;
           cin>>id1; 
           if (start_ptr == NULL)
           {
             cout << "The list is empty!" << endl;
           }
           while(temp!=NULL)
           {
           if(temp->ID=id1)
           { 
               temp2=temp;
               temp=temp->nxt;
           }
           }
    
           while(temp3!=NULL)
           {
            if(temp3->ID=id1)
            {
                temp4=temp3;
               temp3=temp3->nxt;
    
            }
           }
    
         temp->nxt=temp4;
         temp3->nxt=temp2;
         temp->nxt=temp3->nxt;
         temp3->nxt=temp->nxt;
         //temp4->nxt=temp2->nxt;
         //temp2->nxt=temp4->nxt;
         //temp3->nxt=temp2;
        //    temp2->nxt=temp->nxt;
        //    delete temp;
           cout<<endl;
    //       cout<<count;
          }
    
        
    void studentrecord:: SearchStudent()
    {    studentrecord *temp; 
           temp= start_ptr;
        cout<<endl;
           int id,count=0;
           cout<<"enter student ID u want to search for";
           cin>>id; 
           if (start_ptr == NULL)
             cout << "The list is empty!" << endl;
          else
          {
             while(temp!=NULL )
             {
               if(temp->ID==id)
               {
                 cout<<"ID :"<<temp->ID<<endl;
                 cout<<"First Name: "<<temp->fname<<endl;
                 cout<<"Last Name: "<<temp->lname<<endl;
                 cout<<"Programme: "<<temp->programme<<endl;
                 cout<<"Year of Study: "<<temp->year_of_study<<endl;
                 count++;
               }
           //temp->display_list();
               
                temp = temp->nxt;
             }
           cout<<endl;
           cout<<count;
          }
    }
    
    
    void studentrecord:: DeleteStudent()
    {      studentrecord *temp,*temp2; 
           temp= start_ptr;
           cout<<endl;
           int id, count;
           cout<<"enter id u wana delete";
           cin>>id; 
           if (start_ptr == NULL)
           {
             cout << "The list is empty!" << endl;
           }
           if(start_ptr->ID = id)
           {
               temp=start_ptr;
               start_ptr=start_ptr->nxt;
           delete temp;
           }
          else
          {
            while(temp->ID!=id)
               {
                   temp2=temp;
                 cout<<temp->ID;
                 count++;
                 temp = temp->nxt;
            }
            temp2->nxt=temp->nxt;
            delete temp;
           cout<<endl;
           cout<<count;
          }
    }
    
    void studentrecord::ConditionalDisplay()
    {    studentrecord *temp; 
          temp= start_ptr;
          cout<<endl;
          int count=0;
           char lastname[20];
           cout<<"Enter the Last Name to search:   ";
           cin>>lastname; 
           if (temp == NULL)
             cout << "The list is empty!" << endl;
          else
          {
             while(temp!=NULL )
             {
                 if(temp->lname==lastname)
                 {          
                 cout<<endl;
                 cout<<temp->lname<<endl;
                 cout<<temp->fname<<endl;
                 cout<<temp->ID<<endl;
                 cout<<temp->ID<<endl;
                 cout<<temp->year_of_study<<endl;
                temp=temp->nxt;
               }
           //temp->display_list();
               
                temp = temp->nxt;
             }
           cout<<endl;
           cout<<"There are "<<count<<"maches";
          }
    }
    
    void studentrecord:: displayStudentInformation()
    {      studentrecord *temp; 
           temp= start_ptr;
        cout<<endl;
           int id,count=0;
           cout<<"enter id to get student information";
           cin>>id;
           if (temp == NULL)
             cout << "The list is empty!" << endl;
          else
       {
             while(temp!=NULL )
       {
        if(temp->ID==id)
        {
          cout<<temp->fname<<endl;
          cout<<temp->lname<<endl;
          cout<<temp->ID<<endl;
          cout<<temp->year_of_study<<endl;
          cout<<endl;
          
          count++; 
        }
           //temp->display_list();
               
          temp = temp->nxt;
       }
       cout<<endl;
      cout<<count;
          }
    }
    void main()
    
    {  
        //start_ptr = NULL;
        studentrecord l;
    
         do
        {
         
          cout << "             ______________________________"<<endl;
          cout << "             Please select a choice : " << endl;
          cout << "             ______________________________"<<endl;
          cout << "             0. Exit the program." << endl;
          cout << "             1. register student." << endl;
          cout << "             2. search student." << endl;
          cout << "             3. Swap student postion." << endl;
          cout << "             4. display student information." << endl;
          cout << "             5. display all." << endl;
          cout << "             6. conditional display."<<endl;
          cout << "             7. delete."<<endl;
          cout << "             _______________________________" <<endl << "";
         cin >> khetho;
    
          switch (khetho)
          {   
              case 1 : l.register_student();
        cout<<" ";system("pause");
                  system("cls");
    
                  break;
              case 2 : l.SearchStudent(); break;
              case 3 : l.Swap(); break;
              case 4 : l.displayStudentInformation(); break;
              case 5 : l.display_all();break;
              case 6 : l.ConditionalDisplay();break;
              case 7 : l.DeleteStudent();break;
            }
        }
         while (khetho != 0);
      }
    Last edited by aquilina; 05-01-2013 at 07:24 AM. Reason: wrong code

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also posted here.

    Jim

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    Quote Originally Posted by jimblumberg View Post
    Also posted here.

    Jim
    actually i didnt get what he saying btw..

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Then why didn't you ask him to explain himself better? Why post to another forum when you're already getting answers?

    Myself, if I were to try to answer the question, I would tell you to tell me what kind of structure you think the program is using. But since I don't care to answer your question I don't expect any answer.

    Jim

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    Yea i already asked.. Since hes taking long time to answer me and i tried to move other forum to get different way of explaination. Different ppl have his own way to explain. Thats why i try to posted it at here instead waiting for his reply. Ah ok since you dont want to answer it. Maybe i should tried other forum then. Thank.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The only thing I am going to say is that the code is garbage, an eyesore. In fact, I'd call it C code, and not C++ code.
    What do you want to do with it, anyway?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Looks like it was copied from here.

    EDIT: Ah, it's a linked list.
    Last edited by rags_to_riches; 05-01-2013 at 02:41 PM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh, so a professor who writes garbage code that students need to understand and explain? Great...
    Maybe this professor should just stick with C since he/she is so familiar with it >_<
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Wow, is that really a teacher's code?

    Get out of that class. Seriously, this code is awful. There are no less than four absolute showstopper errors in one method alone (two infinite loops, a dereference of an uninitialized pointer, and a sequence of operations that leaves what's left of the list in an invalid state).

    It looks like a linked list implementation done by someone who doesn't know how a linked list works.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by aquilina View Post
    Its using binary tree, queue, tree or vector or others?
    As already posted it's a single linked list (the structures have a "next" pointer"). A doubly linked list would have a previous and a next pointer. A binary tree is made of structures that include "node" structures that have two pointers, "left" and "right" that link to "sub-trees". A queue normally mean just about any structure or method that is first in first out, such as a circular buffer, but it can also be a linked list. A tree is a general form of a binary tree. A vector is essentially the same as an array, it's accessed via indexes or pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2013, 05:19 AM
  2. difference between data object and data structure?
    By c_lady in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2011, 12:30 PM
  3. Data structure for storing serial port data in firmware
    By james457 in forum C Programming
    Replies: 4
    Last Post: 06-15-2009, 09:28 AM
  4. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  5. Data Structure
    By sunnypalsingh in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2005, 08:27 PM