#include<iostream>
using namespace std;
//initiating a class 'node' that is used to store the data and address of an element
class Node
{
public:
//initialising the data members
int information;
//self referential data members
Node *leftSection;
Node *rightSection;
//default constructor
//initialises the data members when a node is created.
Node()
{
information = 0;
leftSection = NULL;
rightSection = NULL;
}
};
//class that implements Binary Search Tree
class BinarySearchTree
{
public:
//method to insert elements into a binary tree
void insertIntoBinaryTree( int number, Node *root )
{
//checking if tree is empty
if(root == NULL)
{
Node *root = new Node;
root->information = number;
root->leftSection = NULL;
root->rightSection = NULL;
cout<<"Success";
}
else
{
//checking the left section condition
if(number < root->information)
{
insertIntoBinaryTree( number, root->leftSection );
cout<<"Success";
}
else if(number > root->information)
{
insertIntoBinaryTree( number, root->rightSection );
cout<<"Success";
}
else
{
cout<<"\nElement Already Exists !";
}
}
}
void displayBinaryTreeInOrder( Node *root )
{
if(root == NULL)
{
cout<<"\nNo Elements in the Tree ";
return;
}
displayBinaryTreeInOrder(root->leftSection);
cout<<root->information<<" - ";
displayBinaryTreeInOrder(root->rightSection);
}
};
class callAllMethods
{
public:
char menu()
{
char choice;
cout<<endl;
cout<<"\n---MENU---";
cout<<"\n1.Add an Item into Binary Search Tree";
cout<<"\n2.Remove an Item from Binary Search Tree";
cout<<"\n3.Remove an Item (2).";
cout<<"\n4.Remove an Item (3)";
cout<<"\n5.Traverse the Binary Search Tree - In Order";
cout<<"\n\nEnter your Option: ";
cin>>choice;
return choice;
}
void Call()
{
int value;
BinarySearchTree user;
Node *Key = NULL;
char choice;
int number;
int numberToBeChecked;
do{
choice = menu();
switch(choice)
{
case '1': cout<<"\nEnter the Number To Be Inserted into the Tree ";
cin>>value;
user.insertIntoBinaryTree(value,Key);
break;
case '2':user.displayBinaryTreeInOrder(Key);
break;
case '3':
break;
case '4':
break;
case '5': user.displayBinaryTreeInOrder(Key);
break;
default : cout<<"System Exit";
}
}while(choice!='6');
}
};
int main()
{
callAllMethods Object;
Object.Call();
}
My program splits the original linked list into halves after I call function binary search function, any way i can keep the original single linked list and calling the function? i did try to create a 2 extra nodes start2 and start1, and tried to copy all of the data of original start into start2 but again it just splits the original linked list.
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
//declaring
struct node{
int info;
struct node* next;
};
node* start= NULL;
node* start1= NULL;
node* start2= NULL;
int count = 0;
void createlist(int value){
node *q,*tmp;
tmp=new(struct node);
tmp->info=value;
tmp->next=NULL;
if(start==NULL) start=tmp; /*if list is empty*/
else /*element inserted at end*/
{
q=start;
while(q->next!=NULL) q=q->next;
q->next=tmp;
}}
int getCount(node* head)
{
// Initialize count
// Initialize current
node* current = head;
while (current != NULL)
{
count++;
current = current->next;
}
return count;
}
int Split( node *start2, node **start1)
{
struct node *slow, *fast;
if(start2->next==NULL) /*only one element*/
return 0;
slow=fast=start2;
while(fast->next!=NULL && fast->next->next!=NULL)
{
slow = slow->next;
fast = fast->next->next;
}
*start1 = slow->next;
slow->next = NULL;
}//End of Split()//
int binarysearch(int number){
start2= start;
getCount(start2);
int midindex;
midindex = count/2;
node* temp;
temp = start2;
int middle= 1;
while(middle != midindex){
temp = temp->next;
middle++;
}
Split(start2, &start1);
if (temp->info == number){
cout<<"The number is at "<<middle <<"index"<<endl;
}
middle++;
if (temp->info<number){
node* temp1 = start1;
while(temp1->info != number){
temp1= temp1->next;
middle++;}
cout<<"The number exist at the "<< middle <<endl;
}
if (temp->info>number){
node* temp2 = start2;
int newcount =1;
while(temp2->info != number){
temp2= temp2->next;
newcount++;
}
cout<<"The number exist at the "<< newcount <<endl;
}
}
void display()
{
struct node *q;
if(start==NULL)
{
cout<<"List is empty"<<endl;
return;
}
q=start;
cout<<"List is:"<<endl;
while(q!=NULL)
{
cout<<q->info<<" ";
q=q->next;
}
cout<<endl;
}
int main(){
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Search the number "<<endl;
cout<<"3) Display "<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
createlist(val);
break;
}
case 2: {
int n;
cout<<"Enter the value you want to be searched"<<endl;
cin>>n;
binarysearch(n);
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
I have made a program in c++ to delete a node in a singly linked list but it is not working as predicted. I am attaching pictures of the output for better clarity that whats misbehaving.
code:
int del_node(int val_del) //this section is producing error
{
node* temp_del=head;
if(head==nullptr)
{
cout<<"no element to delete.!";
exit(0);
}
else
{
while(temp_del->next!=nullptr)
{
if(temp_del->next->data==val_del)
{
temp_del->next=temp_del->next->next;
delete temp_del->next->next;
}
temp_del=temp_del->next;
}
}
return 0;
}
This a function of a class.
Here is the complete code if it helps:
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
class linked_list
{
node *head,*tail;
public:
linked_list()
{
head=nullptr;
tail=nullptr;
}
int create_last(int val_last)
{
node *temp=new node; if(!temp){cout<<"memory not allocated"; exit(1);}
temp->data=val_last;
temp->next=nullptr;
if(head==nullptr)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
return 0;
}
int create_beg(int val_beg)
{
node *temp_head=nullptr;
node *temp=new node; if(!temp){cout<<"memory not allocated"; exit(1);}
temp->data=val_beg;
temp->next=nullptr;
if(head==nullptr)
{
head=temp;
tail=temp;
}
else
{
temp_head=head;
head=temp;
temp->next=temp_head;
}
return 0;
}
int del_node(int val_del) //this section is producing error
{
node* temp_del=head;
if(head==nullptr)
{
cout<<"no element to delete.!";
exit(0);
}
else
{
while(temp_del->next!=nullptr)
{
if(temp_del->next->data==val_del)
{
temp_del->next=temp_del->next->next;
delete temp_del->next->next;
}
temp_del=temp_del->next;
}
}
return 0;
}
int show()
{
node* temp_show=head;
while(temp_show!=nullptr)
{
cout<<temp_show->data<<"\n";
temp_show=temp_show->next;
}
return 0;
}
}info;
int main()
{
int choice,ele; char cont;
rep:
cout<<"1. Insert node at the end\n";
cout<<"2. Insert node at beg\n";
cout<<"3. Delete node\n";
cout<<"4. Show nodes\n";
cout<<"5. Exit\n";
cout<<"enter your choice: ";
cin>>choice;
switch(choice)
{
case 1: cout<<"Enter element: ";
cin>>ele;
info.create_last(ele);
break;
case 2: cout<<"Enter element: ";
cin>>ele;
info.create_beg(ele);
break;
case 3: cout<<"Enter element: ";
cin>>ele;
info.del_node(ele);
break;
case 4: info.show();
break;
case 5: exit(0);
break;
default: cout<<"Wrong choice, Bye.!!";
exit(0);
}
cout<<"Do you want to continue(y/n): ";
cin>>cont;
if(cont=='y'||cont=='Y')
{
goto rep;
}
else
{
cout<<"thank you";
exit(0);
}
return 0;
}
int del_node(int val_del) {
node* temp_del = head;
if(head == nullptr) {
cout<<"no element to delete.!";
exit(0);
} else if(head->data == val_del) {
// If head is to be deleted
head = head->next;
} else {
while(temp_del->next != nullptr) {
if(temp_del->next->data == val_del) {
temp_del->next=temp_del->next->next;
// delete temp_del->next->next; This is wrong deletion
}
temp_del = temp_del->next;
}
}
// delete the node if one found else not
if (temp_del != nullptr)
delete temp_del;
return 0; // This should return true or false, do check what you want as return type
}
The del_node function has two problems:
1) It can't delete the node when the list has exactly 1 element
2) It deletes the wrong element
So let's start with number 1 and look at the code:
if(head==nullptr)
{
cout<<"no element to delete.!";
exit(0);
}
else
{
while(temp_del->next!=nullptr)
Assume that the list contains exactly one element. That means:
a) head is not NULL
b) head->next and therefore also temp_del->next is NULL
so while(temp_del->next!=nullptr) will result in false, i.e. the loop will no be executed. The overall result is that the code does nothing.
Now for number 2. The code is:
if(temp_del->next->data==val_del)
{
temp_del->next=temp_del->next->next; // You want to delete temp_del->next
// but here you change it
delete temp_del->next->next; // so here you delete the wrong node
// there is also an extra ->next
}
You need a temp variable to hold a pointer to the node you want to delete.
You probably want the code to be:
int del_node(int val_del)
{
// Delete elements in the front
while (head!=nullptr && head->data==val_del)
{
node* node_to_delete = head;
head = head->next;
delete node_to_delete;
// return 0; Uncomment if you only want one delete per function call
}
if(head==nullptr) return 0;
// Delete elements not in the front
node* temp_del=head;
while(temp_del->next!=nullptr)
{
if (temp_del->next->data==val_del)
{
node* node_to_delete = temp_del->next;
temp_del->next = temp_del->next->next;
delete node_to_delete;
// return 0; Uncomment if you only want one delete per function call
}
temp_del=temp_del->next;
}
return 0;
}
This is my first time in building a custom deque(Double Ended Queue) class in c++ .This code is giving me Multiple definition error for head and tail.What should i do? what changes should be made?
// this is the header file(dequeue.h)
#ifndef DEQUEUE_H
#define DEQUEUE_H
#include <iostream>
#include <cstdlib>
using namespace std;
/*
* Node Declaration
*/
struct node
{
int info;
node *next;
node *prev;
}*head, *tail;
class dequeue
{
public:
int top1, top2;
void insert();
void del();
void display();
dequeue()
{
top1 = 0;
top2 = 0;
head = NULL;
tail = NULL;
}
};
#endif // DEQUEUE_H
//This is the dequeue.cpp file
#include "dequeue.h"
#include <iostream>
using namespace std;
/*
* Insert Element in Doubly Ended Queue
*/
void dequeue::insert()
{
struct node *temp;
int ch, value;
if (top1 + top2 >= 50)
{
cout<<"Dequeue Overflow"<<endl;
return;
}
if (top1 + top2 == 0)
{
cout<<"Enter the value to be inserted: ";
cin>>value;
head = new (struct node);
head->info = value;
head->next = NULL;
head->prev = NULL;
tail = head;
top1++;
cout<<"Element Inserted into empty deque"<<endl;
}
else
{
while (1)
{
cout<<endl;
cout<<"1.Insert Element at first"<<endl;
cout<<"2.Insert Element at last"<<endl;
cout<<"3.Exit"<<endl;
cout<<endl;
cout<<"Enter Your Choice: ";
cin>>ch;
cout<<endl;
switch(ch)
{
case 1:
cout<<"Enter the value to be inserted: ";
cin>>value;
temp = new (struct node);
temp->info = value;
temp->next = head;
temp->prev = NULL;
head->prev = temp;
head = temp;
top1++;
break;
case 2:
cout<<"Enter the value to be inserted: ";
cin>>value;
temp = new (struct node);
temp->info = value;
temp->next = NULL;
temp->prev = tail;
tail->next = temp;
tail = temp;
top2++;
break;
case 3:
return;
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
}
}
/*
* Delete Element in Doubly Ended Queue
*/
void dequeue::del()
{
if (top1 + top2 <= 0)
{
cout<<"Deque Underflow"<<endl;
return;
}
int ch;
while (1)
{
cout<<endl;
cout<<"1.Delete Element at first"<<endl;
cout<<"2.Delete Element at last"<<endl;
cout<<"3.Exit"<<endl;
cout<<endl;
cout<<"Enter Your Choice: ";
cin>>ch;
cout<<endl;
switch(ch)
{
case 1:
head = head->next;
head->prev = NULL;
top1--;
break;
case 2:
tail = tail->prev;
tail->next = NULL;
top2--;
break;
case 3:
return;
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
}
/*
* Display Doubly Ended Queue
*/
void dequeue::display()
{
struct node *temp;
int ch;
if (top1 + top2 <= 0)
{
cout<<"Deque Underflow"<<endl;
return;
}
while (1)
{
cout<<endl;
cout<<"1.Display Deque from Beginning"<<endl;
cout<<"2.Display Deque from End"<<endl;
cout<<"3.Exit"<<endl;
cout<<endl;
cout<<"Enter Your Choice: ";
cin>>ch;
cout<<endl;
switch (ch)
{
case 1:
temp = head;
cout<<"Deque from Beginning:"<<endl;
while (temp != NULL)
{
cout<<temp->info<<" ";
temp = temp->next;
}
cout<<endl;
break;
case 2:
cout<<"Deque from End:"<<endl;
temp = tail;
while (temp != NULL)
{
cout<<temp->info<<" ";
temp = temp->prev;
}
temp = tail;
cout<<endl;
break;
case 3:
return;
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
}
//This the main.cpp`#include
#include "dequeue.h"
using namespace std;
int main()
{
int choice;
dequeue dl;
while (1)
{
cout<<"\n-------------"<<endl;
cout<<"Operations on Deque"<<endl;
cout<<"\n-------------"<<endl;
cout<<"1.Insert Element into the Deque"<<endl;
cout<<"2.Delete Element from the Deque"<<endl;
cout<<"3.Traverse the Deque"<<endl;
cout<<"4.Quit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
cout<<endl;
switch(choice)
{
case 1:
dl.insert();
break;
case 2:
dl.del();
break;
case 3:
dl.display();
break;
case 4:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
`
The head and tail variables will be defined everywhere you include the dequeue.h file.
Did you mean for them to be class members of dequeue?
If you really to share them, move them to the cpp file and declare them extern in the h file.
I have made a Student record system in c++ using the concept of linked list. The program will take the student id, name and marks for 3 different exams. Then it will calculate the aggregate percentage based on those exams marks and display the relevant message according to the aggregate percentage. The program is running without any errors.
But I want student id to be unique. For example, if a user inputs a student id which has been already assigned to previous student than it will show a message that "please enter a different id."
How would I do that. Any help will be appreciated.
struct student
{
int id;
char name[MAX];
string status;
double aggr;
int matric_marks, inter_marks, entryTest_marks;
};
struct node
{
struct student *data;
struct node *next;
node()
{
data = 0;
next = NULL;
}
};
struct student *readStudent()
{
clearWindow();
struct student *stdnt = new student;
gotoxy(33,8);cout<<"Student ID: ";
while(!(cin >> stdnt->id) || cin.peek() != '\n')
{
char ch;
cin.clear();
cout << "sorry";
while(cin.get(ch) && ch != '\n');
}
cin.ignore();
gotoxy(33,10);cout<<"Student Name: ";
cin.getline(stdnt->name, 50);
gotoxy(33,12);cout<<"Enter Matriculation Marks: ";
cin>>(stdnt->matric_marks);
gotoxy(33,14);cout<<"Enter Intermediate Marks: ";
cin>>(stdnt->inter_marks);
gotoxy(33,16);cout<<"Enter Entry Test Marks: ";
cin>>(stdnt->entryTest_marks);
stdnt->aggr = calculate_aggregiate(stdnt);
gotoxy(33,18);cout<<"Student Aggregate Marks: "<< stdnt->aggr;
if (stdnt->aggr >= 70)
{
gotoxy(33,20);cout<<"Student Registered In Electrical Engg";
}
else if (stdnt->aggr >= 60)
{
gotoxy(33,22);cout<<"Student Registered In Mechanical Engg";
}
else if (stdnt->aggr >=50)
{
gotoxy(33,24);cout<<"Student Registered In Computer Science";
}
else
{
gotoxy(33,20);cout<<"Sorry! The Student Doesnt Qualify";
}
return stdnt;
}
struct node *createDatabase(int size)
{
int i = 0;
struct node *head = NULL;
struct node *last = NULL;
for(i = 0; i < size; i++)
{
struct student *stdnt = readStudent();
struct node * current = new node;
current->data = stdnt;
current->next = NULL;
if(last)
last->next = current;
else
head = current;
last = current;
}
return head;
}
struct node *InsertRecord(struct node *head)
{
struct node *record = new node;
struct student *stdnt = readStudent();
record->data=stdnt;
record->next=head;
return record;
}
double calculate_aggregiate(struct student *stud)
{
student *stdnt = stud;
double aggr;
aggr = stdnt->matric_marks * 10/100 + stdnt->inter_marks * 50/100 +
stdnt->entryTest_marks * 40/100;
return aggr;
}
struct node *DeleteRecord(struct node *head)
{
clearWindow();
struct node *curr,*prev, *temp;
int tdata;
if(head==NULL)
{
gotoxy(33,10);cout<<"NO RECORDS TO DELETE......!";
}
else
{
gotoxy(33,10);cout<<"Enter Student ID To Be Deleted: ";
cin>>tdata;
prev=curr=head;
while((curr!=NULL)&&(curr->data->id!=tdata))
{
prev=curr;
curr=curr->next;
}
if(curr==NULL)
{
gotoxy(33,12);cout<<"The Requested ID Is Not Found...!";
}
else if(curr==head)
{
head=head->next;
gotoxy(33,12);cout<<"DATA DELETED....!";
}
else
{
prev->next=curr->next;
if(curr->next==NULL)
{
temp=prev;
}
gotoxy(33,12);cout<<"DATA DELETED....!"<<tdata;
}
delete(curr);
}
return head;
}
void ModifyRecord(struct node *head)
{
clearWindow();
int ch, sid;
struct node *current;
struct student *stdnt;
if (head==NULL)
{
gotoxy(33,8);cout<<"NO RECORD TO MODIFY..!";
}
else
{
gotoxy(33,8);cout<<"Enter Student ID To Modify: ";
cin>>sid;
current=head;
while((current!=NULL) && (current->data->id!=sid))
{
current=current->next;
}
if (current==NULL)
{
gotoxy(33,10);cout<<"The Requested ID is Not Found";
}
else if(current->data->id==sid)
{
gotoxy(33,10);cout<<"What Do You Want To Modify";
gotoxy(33,12);cout<<"1. Student's Name";
gotoxy(33,14);cout<<"2. Student's Matric Marks";
gotoxy(33,16);cout<<"3. Student's Intermediate Marks";
gotoxy(33,18);cout<<"4. Student's Entry Test Marks";
gotoxy(33,20);cout<<"Enter Your Choice: ";
cin>>ch;
switch(ch)
{
case 1 : gotoxy(33,22);cout<<"Enter New Name: ";
cin.getline(current->data->name, 50);break;
case 2 : gotoxy(33,22);cout<<"Enter New Matric Marks: ";
cin>>(current->data->matric_marks);break;
case 3 : gotoxy(33,22);cout<<"Enter New Intermediate Marks: ";
cin>>(current->data->inter_marks);break;
case 4 : gotoxy(33,22);cout<<"Enter New Entry Test Marks: ";
cin>>(current->data->entryTest_marks);break;
current->data->aggr = current->data->matric_marks * 10/100 + current->data- >inter_marks * 50/100 +
current->data->entryTest_marks * 40/100;
}
gotoxy(33,24);cout<<"RECORD MODIFIED....!";
}
}
}
void SearchRecord(struct node *head)
{
clearWindow();
int s_id;
struct node *current;
if (head==NULL)
{
gotoxy(33,8);cout<<"NO RECORD TO SEARCH..!";
}
else
{
gotoxy(33,8);cout<<"Enter Student ID To Be Searched: ";
cin>>s_id;
current=head;
while ((current!=NULL) && (current->data->id!=s_id))
{
current=current->next;
}
if (current==NULL)
{
gotoxy(33,10);cout<<"The Requested ID is Not Found";
}
else if (current->data->id==s_id)
{
gotoxy(33,10);cout<<"Student ID: "<<current->data->id;
gotoxy(33,12);cout<<"Student Name: "<<current->data->name;
gotoxy(33,14);cout<<"Student Matric Marks: "<<current->data->matric_marks;
gotoxy(33,16);cout<<"Student Intermediate Marks: "<<current->data - >inter_marks;
gotoxy(33,18);cout<<"Student Entry Test Marks: "<<current->data- >entryTest_marks;
gotoxy(33,20);cout<<"Student Aggregate Marks: "<<current->data->aggr;
if (current->data->aggr >= 70)
{
gotoxy(33,22);cout<<"Student Registered In Electrical Engg";
}
else if (current->data->aggr >= 60)
{
gotoxy(33,22);cout<<"Student Registered In Mechanical Engg";
}
else if (current->data->aggr >=50)
{
gotoxy(33,22);cout<<"Student Registered In Computer Science";
}
else
{
gotoxy(33,22);cout<<"Sorry! The Student Doesnt Qualify";
}
}
}
}
void print(struct node *head)
{
clearWindow_p();
struct node *current = head;
if (head == NULL)
{
gotoxy(33,8);cout<<"No Student Registered Yet......!";
}
else
{
cout<<"\n\t\t\t\t\tSTUDENTS STATISTICS";
while(current)
{
struct student *stdnt = current->data;
cout<<"\n\t\t\t\t\t-------------------------------- ";
cout<<"\n\t\t\t\t\tStudent ID :"<<stdnt->id;
cout<<"\n\t\t\t\t\tStudent Name :"<<stdnt->name;
cout<<"\n\t\t\t\t\tMatric Marks :"<<stdnt->matric_marks;
cout<<"\n\t\t\t\t\tIntermediate Marks :"<<stdnt->inter_marks;
cout<<"\n\t\t\t\t\tEntry Test Marks: "<<stdnt->entryTest_marks;
cout<<"\n\t\t\t\t\tAggregate: "<<stdnt->aggr;
if (stdnt->aggr >= 70)
{
cout<<"\n\t\t\t\t\tStudent Registered In Electrical Engg";
}
else if (stdnt->aggr >= 60)
{
cout<<"\n\t\t\t\t\tStudent Registered In Mechanical Engg";
}
else if (stdnt->aggr >=50)
{
cout<<"\n\t\t\t\t\tStudent Registered In Computer Science";
}
else
{
cout<<"\n\t\t\t\t\tSorry! The Student Doesnt Qualify";
}
current=current->next;
}
cout<<"\n\t\t\t\t\t--------------------------------";
}
}
int compareStudents(struct student *left, struct student *right)
{
return strcmp(left->name, right->name);
}
struct node *sort(struct node *head)
{
//using bubble sort
int swapped = 0;
do
{
swapped = 0;
struct node *current = head;
struct node *previous = NULL;
while(current && current->next)
{
if(compareStudents(current->data, current->next->data) > 0)
{
//swap here
struct node *next = current->next;
if(previous)
{
previous->next = next;
}
else
{
head = next;
}
current->next = next->next;
previous = next;
previous->next = current;
swapped = 1;
}
else
{
previous = current;
current = current->next;
}
}
} while(swapped);
return head;
}
int main()
{
system("color f0");
window();
SetColor(28);
int total,i,choice;
int x = 2;
struct node *head;
head=NULL;
do
{
menu:
gotoxy(x, 8);cout<<"1.Create a Record File";
gotoxy(x, 10);cout<<"2.Insert Student's Record";
gotoxy(x, 12);cout<<"3.Modify Student's Record";
gotoxy(x, 14);cout<<"4.Delete Student's Record";
gotoxy(x, 16);cout<<"5.Search Student's Record";
gotoxy(x, 18);cout<<"6.Print All Records";
gotoxy(x, 20);cout<<"7.Sort According To Names";
gotoxy(x, 22);cout<<"8.Clear The Screen";
gotoxy(x, 24);cout<<"9.Exit";
gotoxy(x, 26);cout<<"Enter your choice";
cout<<" [ ]\b\b";
cin>>choice;
switch(choice)
{
case 1:
clearWindow();
gotoxy(33, 8);cout<<"How Many Students Do You Want To Register: ";
cin>>total;
head=createDatabase(total);
break;
case 2:
head=InsertRecord(head);
break;
case 3:
ModifyRecord(head);
break;
case 4:
DeleteRecord(head);
break;
case 5:
SearchRecord(head);
break;
case 6:
print(head);
break;
case 7:
sort(head);
print(head);
break;
case 8:
main();
break;
default: gotoxy(33,8);cout<<"INVALID OPTION";
}
}while(choice!=9);
getch();
return 0;
}
Since you are using C++, you should take advantage of the built-in types. If there is only one student with a given ID, use the std::map<> template. For example:
struct student
{
char name[MAX];
string status;
double aggr;
int matric_marks, inter_marks, entryTest_marks;
};
map<int, student> students;
then, to traverse students:
for (auto i = students.begin(); i != students.end(); ++i) {
int studentId = i->first;
student& student = i->second;
student.aggr = ...
}
(auto is C++11, not all compilers enable it by default IIRC. See here.)
If You know the range of student ids , and it is within a feasible range of memory , you can use a UsedId[] array.
If the range is too big too be held , use a Hashmap ,(MAP in c++) .
I would probably implement something called FindRecordByID.
struct student *FindRecordByID(struct node *head, int id) {
for(; head != NULL; head = head->next)
if (head->data->id == id)
return head->data;
return NULL;
}
Then when you do your create_new_student routine, you could use that to verify that the given ID is not already used.