Added assignment-19 (singly linked list for PRN and name) which takes PRN and names for n students and displays it
This commit is contained in:
parent
5049898263
commit
dc66a90d9b
60
assignment-19.cpp
Normal file
60
assignment-19.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
// This code creates a linked list to store PRN and names of n number of students.
|
||||
// We're not sure if there's anything more to it. For now, it only stores and displays.
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class node {
|
||||
int roll;
|
||||
string name;
|
||||
node *next;
|
||||
|
||||
public:
|
||||
static node* create();
|
||||
void display(node* head);
|
||||
};
|
||||
|
||||
node* node::create() {
|
||||
int total;
|
||||
node *head=NULL;
|
||||
node* point=NULL;
|
||||
cout<<"Total number of students are:\t";
|
||||
cin>>total;
|
||||
|
||||
for (int i=0; i<total; i++) {
|
||||
if (head==NULL) {
|
||||
head = new node;
|
||||
cout<<"Enter PRN number for student "<<i+1<<":\t";
|
||||
cin>>head->roll;
|
||||
cout<<"Enter name for student "<<i+1<<":\t";
|
||||
cin>>head->name;
|
||||
head->next=NULL;
|
||||
point=head;
|
||||
}
|
||||
else {
|
||||
point->next=new node;
|
||||
point=point->next;
|
||||
cout << "Enter PRN number for student "<<i+1<<":\t";
|
||||
cin>>point->roll;
|
||||
cout << "Enter name for student "<<i+1<<":\t";
|
||||
cin>>point->name;
|
||||
point->next=NULL;
|
||||
}
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
void node::display(node* head) {
|
||||
node* point=this;
|
||||
cout<<endl<<"PRNs and names are:"<<endl;
|
||||
for (point=head; point!=NULL; point=point->next) {
|
||||
cout<<point->roll<<" -> "<<point->name<<endl;
|
||||
}
|
||||
cout<<endl<<"## DESIGNED AND ENGINEERED BY KSHITIJ\n## END OF CODE"<<endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
node* head = node::create();
|
||||
head->display(head);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user