Earn without any Investment!

Saturday, November 13, 2010

Student Details Using Single Linked List

#include
#include
#include
#include

class node
{
public:
int rollnumber;
char name[20];
node *next;
};
class list
{
node *start;
public:
list()
{
start=NULL;
}
void addnode()
{
int rollno;
char nm[20];
cout<<"\nEnter the roll number of the student:"; cin>>rollno;
cout<<"\nEnter the name of the student:"; cin>>nm;
node *newnode=new node;
newnode->rollnumber=rollno;
strcpy(newnode->name,nm);
if(start==NULL || rollno<=start->rollnumber)
/* If the node to be inserted is the first node */
{
if(start!=NULL && (rollno == start->rollnumber))
{
cout<<"\nDuplicate Roll Numbers not allowed."; return; } newnode->next=start;
start=newnode;
return;
}
node *previous,*current;
previous=start;
current=start;
while((current!=NULL) && (rollno>=current->rollnumber))
{
if(rollno==current->rollnumber)
{
cout<<"\nDuplicate Roll Numbers not allowed."; return; } previous=current; current=current->next;
}
newnode->next=current;
previous->next=newnode;
}
int listempty()
{
if(start==NULL)
return(1);
else
return(0);
}
int delnode(int rollno)
{
node *current,*previous;
if(search(rollno,&previous,¤t)==0)
return 0;
previous->next=current->next;
if(current==start)
start=start->next;
delete current;
return 1;
}
/* int search(int rollno,node **previous,node **current)
{
*previous=start;
*current=start;
while((*current!=NULL)&&((rollno!=current->rollnumber)))
{
*previous=*current;
*current=(*current)->next;
}
if(*current!=NULL)
return 1;
else
return 0;
}
*/ void traverse()
{
if(listempty())
cout<<"\nList is empty"; else { cout<<"The records in the list are:\n"; node *currentnode; for(currentnode=start;currentnode!=NULL;currentnode=currentnode->next)
{
cout<rollnumber<<"\t"<name<<"\n"; } cout<>ch;
switch(ch)
{
case '1':
obj.addnode();
break;
case '2':
if(obj.listempty()==0)
{
cout<<"List Is Empty:"; break; } cout<<"\nEnter Rollno. to delete record:"; cin>>rollno;
if(obj.delnode(rollno)==0)
cout<<"Record not found"; else cout<<"Record deleted."; break; case '3': obj.traverse(); break; case '4': if(obj.listempty()==0) { cout<<"\nList Empty"; break; } node *previous,*current; cout<<"\nEnter rollno. to search:"; cin>>rollno;
if(obj.search(rollno,&previous,¤t)==0)
{
cout<<"\n Record not Found"; break; } else { cout<<"\n Record found\n"; cout<<"Roll Number: "<rollnumber;
cout<<"\nName: "<name;
}
break;
case '5':
exit(0);
default:
cout<<"Enter correct choice:";
break;
}
}
}


No comments:

Post a Comment