C++ Template Implementation of Singly Linked List for Student Data, Exercises of Computer Science

The implementation of a singly linked list in c++ using templates for managing student data. The linked list includes methods for adding nodes, deleting nodes, sorting nodes based on roll number and alphabetical order, and searching for a node based on roll number.

Typology: Exercises

2015/2016

Uploaded on 10/13/2016

hammad_iqbal
hammad_iqbal 🇵🇰

1 document

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Header.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
template<class t>
class student
{
private:
t name, roll, descip;
public:
student();
~student();
void setter(t, t, t);
t getname();
t getroll();
t getdesci();
};
template<class c>
class dlist
{
private:
static dlist<c> *head;
static dlist<c> *tail;
dlist *next;
dlist *pre;
public:
dlist();
~dlist();
student<c> s;
void addnode(c, c, c);
bool deleteele(c);
void rollsort();
void alphasort();
void descisort();
int search(c);
c getroll(int);
c getname(int);
c getdesci(int);
int getcount();
};
template <class c>
dlist<c> *dlist<c>::head = NULL;
template <class c>
dlist<c> *dlist<c>::tail = NULL;
template<class c>
dlist<c>::dlist()
{
}
template<class c>
dlist<c>::~dlist()
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download C++ Template Implementation of Singly Linked List for Student Data and more Exercises Computer Science in PDF only on Docsity!

Header.h

#pragma once #include #include using namespace std; template class student { private: t name, roll, descip; public: student(); ~student(); void setter(t, t, t); t getname(); t getroll(); t getdesci(); };

template class dlist { private: static dlist *head; static dlist *tail; dlist *next; dlist *pre; public: dlist(); ~dlist(); student s; void addnode(c, c, c); bool deleteele(c); void rollsort(); void alphasort(); void descisort(); int search(c); c getroll(int); c getname(int); c getdesci(int); int getcount(); };

template dlist *dlist::head = NULL;

template dlist *dlist::tail = NULL;

template dlist::dlist() { }

template dlist::~dlist()

template void dlist::addnode(c rol, c nam, c descipl) { dlist *newnode = new dlist; newnode->s.setter(rol, nam, descipl); if (head == NULL)//if first element { head = tail = newnode; tail->next = NULL; head->pre = NULL; } else if (newnode->s.getroll() < head->s.getroll())//if less than head { head->pre = newnode; newnode->next = head; head = newnode; newnode->pre = NULL; } else if (newnode->s.getroll() > tail->s.getroll())//if greater than tail { tail->next = newnode; newnode->pre = tail; newnode->next = NULL; tail = newnode; } else//in mid { dlist *temp; temp = head; while (temp != NULL&&temp->s.getroll() < newnode->s.getroll()) { temp = temp->next; } temp->pre->next = newnode; newnode->next = temp; newnode->pre = temp->pre; temp->pre = newnode; } }

template bool dlist::deleteele(c rol) { dlist *temp; temp = head; if (temp == NULL) { return false; } if (temp->s.getroll() == rol) { head = head->next; head->pre = NULL; temp->next = NULL; delete temp;

temp = temp->next; } }

template void dlist::alphasort() { dlist *temp, *temp1; temp = head; string s, s1, s2, s3, s4, s5; while (temp != NULL) { temp1 = head; while (temp1 != NULL) { if (temp->s.getname() < temp1->s.getname()) { s = temp->s.getroll(); s1 = temp->s.getname(); s2 = temp->s.getdesci(); s3 = temp1->s.getroll(); s4 = temp1->s.getname(); s5 = temp1->s.getdesci(); temp->s.setter(s3, s4, s5); temp1->s.setter(s, s1, s2); } temp1 = temp1->next; } temp = temp->next; } }

template void dlist::descisort() { dlist *temp, *temp1; temp = head; string s, s1, s2, s3, s4, s5; while (temp != NULL) { temp1 = head; while (temp1 != NULL) { if (temp->s.getdesci() < temp1->s.getdesci()) { s = temp->s.getroll(); s1 = temp->s.getname(); s2 = temp->s.getdesci(); s3 = temp1->s.getroll(); s4 = temp1->s.getname(); s5 = temp1->s.getdesci(); temp->s.setter(s3, s4, s5); temp1->s.setter(s, s1, s2); } temp1 = temp1->next; } temp = temp->next; }

template int dlist::search(c val) { dlist *temp; temp = head; int count=0; while (temp != NULL&&temp->s.getroll()!=val) { temp = temp->next; count++; } if (temp == NULL) { return -1; } else { return count; } }

template c dlist::getroll(int index) { dlist *temp; temp = head; for (int i = 0; i < index; i++) { temp = temp->next; } return temp->s.getroll(); }

template c dlist::getname(int index) { dlist *temp; temp = head; for (int i = 0; i < index; i++) { temp = temp->next; } return temp->s.getname(); }

template c dlist::getdesci(int index) { dlist *temp; temp = head; for (int i = 0; i < index; i++) { temp = temp->next; } return temp->s.getdesci();

namespace Project2 {

using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing;

///

/// Summary for MyForm /// dlist l; public ref class MyForm : public System::Windows::Forms::Form { public: MyForm(void) { InitializeComponent(); // //TODO: Add the constructor code here // }

protected: ///

/// Clean up any resources being used. /// ~MyForm() { if (components) { delete components; } } private: System::Windows::Forms::Label^ label1; protected: private: System::Windows::Forms::TextBox^ textBox1; private: System::Windows::Forms::TabControl^ tabControl1; private: System::Windows::Forms::TabPage^ tabPage1; private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::TabPage^ tabPage2; private: System::Windows::Forms::Label^ label3; private: System::Windows::Forms::Label^ label2; private: System::Windows::Forms::TextBox^ textBox3; private: System::Windows::Forms::TextBox^ textBox2; private: System::Windows::Forms::Button^ button2; private: System::Windows::Forms::Label^ label4; private: System::Windows::Forms::TextBox^ textBox4; private: System::Windows::Forms::ListView^ listView1; private: System::Windows::Forms::ColumnHeader^ columnHeader1; private: System::Windows::Forms::ColumnHeader^ columnHeader2; private: System::Windows::Forms::ColumnHeader^ columnHeader3; private: System::Windows::Forms::TabPage^ tabPage3; private: System::Windows::Forms::Button^ button3; private: System::Windows::Forms::TextBox^ textBox5; private: System::Windows::Forms::Label^ label5; private: System::Windows::Forms::TabPage^ tabPage4;

private: System::Windows::Forms::ListView^ listView2; private: System::Windows::Forms::ColumnHeader^ columnHeader4; private: System::Windows::Forms::ColumnHeader^ columnHeader5; private: System::Windows::Forms::ColumnHeader^ columnHeader6; private: System::Windows::Forms::Button^ button6; private: System::Windows::Forms::Button^ button5; private: System::Windows::Forms::Button^ button4; private: System::Windows::Forms::Label^ label6; private: System::Windows::Forms::Button^ button7;

private: ///

/// Required designer variable. /// System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code ///

/// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// void InitializeComponent(void) { this->label1 = (gcnew System::Windows::Forms::Label()); this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->tabControl1 = (gcnew System::Windows::Forms::TabControl()); this->tabPage1 = (gcnew System::Windows::Forms::TabPage()); this->label3 = (gcnew System::Windows::Forms::Label()); this->label2 = (gcnew System::Windows::Forms::Label()); this->textBox3 = (gcnew System::Windows::Forms::TextBox()); this->textBox2 = (gcnew System::Windows::Forms::TextBox()); this->button1 = (gcnew System::Windows::Forms::Button()); this->tabPage2 = (gcnew System::Windows::Forms::TabPage()); this->button2 = (gcnew System::Windows::Forms::Button()); this->label4 = (gcnew System::Windows::Forms::Label()); this->textBox4 = (gcnew System::Windows::Forms::TextBox()); this->listView1 = (gcnew System::Windows::Forms::ListView()); this->columnHeader1 = (gcnew System::Windows::Forms::ColumnHeader ()); this->columnHeader2 = (gcnew System::Windows::Forms::ColumnHeader ()); this->columnHeader3 = (gcnew System::Windows::Forms::ColumnHeader ()); this->tabPage3 = (gcnew System::Windows::Forms::TabPage()); this->button3 = (gcnew System::Windows::Forms::Button()); this->textBox5 = (gcnew System::Windows::Forms::TextBox()); this->label5 = (gcnew System::Windows::Forms::Label()); this->tabPage4 = (gcnew System::Windows::Forms::TabPage()); this->button6 = (gcnew System::Windows::Forms::Button()); this->button5 = (gcnew System::Windows::Forms::Button()); this->button4 = (gcnew System::Windows::Forms::Button()); this->label6 = (gcnew System::Windows::Forms::Label()); this->listView2 = (gcnew System::Windows::Forms::ListView()); this->columnHeader4 = (gcnew System::Windows::Forms::ColumnHeader ()); this->columnHeader5 = (gcnew System::Windows::Forms::ColumnHeader ());

this->label3->AutoSize = true; this->label3->Location = System::Drawing::Point(21, 141); this->label3->Name = L"label3"; this->label3->Size = System::Drawing::Size(102, 13); this->label3->TabIndex = 6; this->label3->Text = L"Enter the Descipline"; // // label // this->label2->AutoSize = true; this->label2->Location = System::Drawing::Point(21, 93); this->label2->Name = L"label2"; this->label2->Size = System::Drawing::Size(81, 13); this->label2->TabIndex = 5; this->label2->Text = L"Enter the Name"; // // textBox // this->textBox3->Location = System::Drawing::Point(187, 138); this->textBox3->Name = L"textBox3"; this->textBox3->Size = System::Drawing::Size(129, 20); this->textBox3->TabIndex = 4; // // textBox // this->textBox2->Location = System::Drawing::Point(187, 90); this->textBox2->Name = L"textBox2"; this->textBox2->Size = System::Drawing::Size(129, 20); this->textBox2->TabIndex = 3; // // button // this->button1->Location = System::Drawing::Point(202, 193); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(95, 34); this->button1->TabIndex = 2; this->button1->Text = L"Enter"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click); // // tabPage // this->tabPage2->Controls->Add(this->button2); this->tabPage2->Controls->Add(this->label4); this->tabPage2->Controls->Add(this->textBox4); this->tabPage2->Controls->Add(this->listView1); this->tabPage2->Location = System::Drawing::Point(4, 22); this->tabPage2->Name = L"tabPage2"; this->tabPage2->Padding = System::Windows::Forms::Padding(3); this->tabPage2->Size = System::Drawing::Size(463, 290); this->tabPage2->TabIndex = 1; this->tabPage2->Text = L"Search"; this->tabPage2->UseVisualStyleBackColor = true; // // button // this->button2->Location = System::Drawing::Point(95, 146);

this->button2->Name = L"button2"; this->button2->Size = System::Drawing::Size(100, 30); this->button2->TabIndex = 3; this->button2->Text = L"Enter"; this->button2->UseVisualStyleBackColor = true; this->button2->Click += gcnew System::EventHandler(this, &MyForm::button2_Click); // // label // this->label4->AutoSize = true; this->label4->Location = System::Drawing::Point(18, 83); this->label4->Name = L"label4"; this->label4->Size = System::Drawing::Size(60, 13); this->label4->TabIndex = 2; this->label4->Text = L"Enter Roll#"; // // textBox // this->textBox4->Location = System::Drawing::Point(95, 80); this->textBox4->Name = L"textBox4"; this->textBox4->Size = System::Drawing::Size(100, 20); this->textBox4->TabIndex = 1; // // listView // this->listView1->Columns->AddRange(gcnew cli::array< System::Windows::Forms::ColumnHeader^ >(3) { this->columnHeader1, this->columnHeader2, this->columnHeader }); this->listView1->Location = System::Drawing::Point(223, 38); this->listView1->Name = L"listView1"; this->listView1->Size = System::Drawing::Size(234, 246); this->listView1->TabIndex = 0; this->listView1->UseCompatibleStateImageBehavior = false; this->listView1->View = System::Windows::Forms::View::Details; // // columnHeader // this->columnHeader1->Text = L"Roll No"; this->columnHeader1->Width = 74; // // columnHeader // this->columnHeader2->Text = L"Name"; this->columnHeader2->Width = 86; // // columnHeader // this->columnHeader3->Text = L"Descipline"; this->columnHeader3->Width = 66; // // tabPage // this->tabPage3->Controls->Add(this->button3); this->tabPage3->Controls->Add(this->textBox5); this->tabPage3->Controls->Add(this->label5);

this->button6->Click += gcnew System::EventHandler(this, &MyForm::button6_Click); // // button // this->button5->Location = System::Drawing::Point(322, 120); this->button5->Name = L"button5"; this->button5->Size = System::Drawing::Size(122, 32); this->button5->TabIndex = 3; this->button5->Text = L"By Descipline"; this->button5->UseVisualStyleBackColor = true; this->button5->Click += gcnew System::EventHandler(this, &MyForm::button5_Click); // // button // this->button4->Location = System::Drawing::Point(322, 51); this->button4->Name = L"button4"; this->button4->Size = System::Drawing::Size(122, 32); this->button4->TabIndex = 2; this->button4->Text = L"By Roll #"; this->button4->UseVisualStyleBackColor = true; this->button4->Click += gcnew System::EventHandler(this, &MyForm::button4_Click); // // label // this->label6->AutoSize = true; this->label6->Location = System::Drawing::Point(319, 9); this->label6->Name = L"label6"; this->label6->Size = System::Drawing::Size(125, 13); this->label6->TabIndex = 1; this->label6->Text = L"Choose data sort method"; // // listView // this->listView2->Columns->AddRange(gcnew cli::array< System::Windows::Forms::ColumnHeader^ >(3) { this->columnHeader4, this->columnHeader5, this->columnHeader }); this->listView2->Location = System::Drawing::Point(7, 9); this->listView2->Name = L"listView2"; this->listView2->Size = System::Drawing::Size(270, 278); this->listView2->TabIndex = 0; this->listView2->UseCompatibleStateImageBehavior = false; this->listView2->View = System::Windows::Forms::View::Details; // // columnHeader // this->columnHeader4->Text = L"Roll #"; this->columnHeader4->Width = 66; // // columnHeader // this->columnHeader5->Text = L"Name"; this->columnHeader5->Width = 118; //

// columnHeader // this->columnHeader6->Text = L"Descipline"; this->columnHeader6->Width = 83; // // button // this->button7->Location = System::Drawing::Point(353, 134); this->button7->Name = L"button7"; this->button7->Size = System::Drawing::Size(88, 26); this->button7->TabIndex = 7; this->button7->Text = L"Examples"; this->button7->UseVisualStyleBackColor = true; this->button7->Click += gcnew System::EventHandler(this, &MyForm::button7_Click); // // MyForm // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(475, 319); this->Controls->Add(this->tabControl1); this->Name = L"MyForm"; this->Text = L"MyForm"; this->tabControl1->ResumeLayout(false); this->tabPage1->ResumeLayout(false); this->tabPage1->PerformLayout(); this->tabPage2->ResumeLayout(false); this->tabPage2->PerformLayout(); this->tabPage3->ResumeLayout(false); this->tabPage3->PerformLayout(); this->tabPage4->ResumeLayout(false); this->tabPage4->PerformLayout(); this->ResumeLayout(false);

#pragma endregion private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { string roll, des, nam; msclr::interop::marshal_context context; if (textBox1->Text != ""&&textBox2->Text != ""&&textBox3->Text != "") { roll = context.marshal_as(textBox1->Text); nam = context.marshal_as(textBox2->Text); if (islower(nam[0])) { nam[0] = toupper(nam[0]); } des = context.marshal_as(textBox3->Text); if (roll.length() < 7 || roll.length() > 7) { MessageBox::Show("The roll number should be of 7 letters"); } else if(des.length()<3 || des.length()>4) { MessageBox::Show("The roll number should be of 7 letters"); }

string roll; bool flag; msclr::interop::marshal_context context; if (textBox5->Text != "") { roll = context.marshal_as(textBox5->Text); flag = l.deleteele(roll); if (flag == true) { MessageBox::Show("The Student data is deleted"); } else { MessageBox::Show("The Student data is not found"); } } else { MessageBox::Show("Enter the roll # correctly"); } textBox5->Clear(); } private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) { listView2->Items->Clear(); msclr::interop::marshal_context context; int x, count; count = l.getcount(); for (x = 0; x < count; x++) { String^s = context.marshal_as<String^>(l.getroll(x)); String^s1 = context.marshal_as<String^>(l.getname(x)); String^s2 = context.marshal_as<String^>(l.getdesci(x)); listView2->Items->Add(gcnew String("" + s)); listView2->Items[x]->SubItems->Add(gcnew String("" + s1)); listView2->Items[x]->SubItems->Add(gcnew String("" + s2)); } } private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e) { l.descisort(); listView2->Items->Clear(); msclr::interop::marshal_context context; int x, count; count = l.getcount(); for (x = 0; x < count; x++) { String^s = context.marshal_as<String^>(l.getroll(x)); String^s1 = context.marshal_as<String^>(l.getname(x)); String^s2 = context.marshal_as<String^>(l.getdesci(x)); listView2->Items->Add(gcnew String("" + s)); listView2->Items[x]->SubItems->Add(gcnew String("" + s1)); listView2->Items[x]->SubItems->Add(gcnew String("" + s2)); } } private: System::Void button6_Click(System::Object^ sender, System::EventArgs^ e) { l.alphasort();

listView2->Items->Clear(); msclr::interop::marshal_context context; int x, count; count = l.getcount(); for (x = 0; x < count; x++) { String^s = context.marshal_as<String^>(l.getroll(x)); String^s1 = context.marshal_as<String^>(l.getname(x)); String^s2 = context.marshal_as<String^>(l.getdesci(x)); listView2->Items->Add(gcnew String("" + s)); listView2->Items[x]->SubItems->Add(gcnew String("" + s1)); listView2->Items[x]->SubItems->Add(gcnew String("" + s2)); } } private: System::Void button7_Click(System::Object^ sender, System::EventArgs^ e) { this->Hide(); MyForm1 ^f = gcnew MyForm1(this); f->ShowDialog(); } }; }

MyForm1.h

#pragma once #include #include <msclr\marshal_cppstd.h> using namespace std; namespace Project2 {

using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing;

///

/// Summary for MyForm /// public ref class MyForm1 : public System::Windows::Forms::Form { public: Form ^b; public: MyForm1(void) { InitializeComponent(); // //TODO: Add the constructor code here // }

MyForm1(Form ^a) {

this->listView1->Name = L"listView1"; this->listView1->Size = System::Drawing::Size(344, 226); this->listView1->TabIndex = 0; this->listView1->UseCompatibleStateImageBehavior = false; this->listView1->View = System::Windows::Forms::View::Details; // // columnHeader // this->columnHeader1->Text = L"Index"; // // columnHeader // this->columnHeader2->Text = L"Program Code"; this->columnHeader2->Width = 86; // // columnHeader // this->columnHeader3->Text = L"Program Name"; this->columnHeader3->Width = 193; // // button // this->button1->Location = System::Drawing::Point(12, 237); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(75, 23); this->button1->TabIndex = 1; this->button1->Text = L"Back"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &MyForm1::button1_Click); // // button // this->button2->Location = System::Drawing::Point(258, 237); this->button2->Name = L"button2"; this->button2->Size = System::Drawing::Size(75, 23); this->button2->TabIndex = 2; this->button2->Text = L"Show List"; this->button2->UseVisualStyleBackColor = true; this->button2->Click += gcnew System::EventHandler(this, &MyForm1::button2_Click); // // MyForm // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(345, 272); this->Controls->Add(this->button2); this->Controls->Add(this->button1); this->Controls->Add(this->listView1); this->Name = L"MyForm1"; this->Text = L"MyForm1"; this->ResumeLayout(false);

#pragma endregion private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {

msclr::interop::marshal_context context; fstream file; string *str, *str1,s; int size = 0,temp,len,count=0,x=0; file.open("text_check.TXT"); if (file.is_open()) { while (!file.eof()) { getline(file, s); size++; } file.close(); str = new string[size]; str1 = new string[size]; file.open("text_check.TXT"); while (!file.eof()) { count = 0; temp = count; len = 0; getline(file, s); while (s[count] != ' ') { count++; len++; } str[x] = s.substr(temp, len); count++; temp = count; len = 0; while (s[count] != '\0') { count++; len++; } str1[x] = s.substr(temp, len); x++; } } for (x = 0; x < size; x++) { String^s = context.marshal_as<String^>(str[x]); String^s1 = context.marshal_as<String^>(str1[x]); listView1->Items->Add(gcnew String("" + (x+1))); listView1->Items[x]->SubItems->Add(gcnew String("" + s)); listView1->Items[x]->SubItems->Add(gcnew String("" + s1)); } } private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { this->Hide(); b->Show(); } }; }