Singly Linked List

#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
class Node
 {
public:
  int rollnumber;
  char name[20];
  Node *next;
 };
class List
 {
Node *START;
public:
  List();
  void addNode();
  int Search(int rollno, Node **current, Node **previous);
  int listEmpty();
  int delNode(int element);
  void traverse();
 };
List::List()
 {
START=NULL;
 }
void List::addNode()
 {
int rollno;
char nm[20];
cout<<"\n Enter the roll number of the student:";
cin>>rollno;
cout<<"\n Enter 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((START!=NULL)&&(rollno==START->rollnumber))
          {
 cout<<"\n Duplicate roll numbers not allowed\n";
 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<<"\n Duplicate roll numbers not allowed\n";
return;
    }
         previous=current;
   current=current->next;
       }
newnode->next=current;
previous->next=newnode;
 }
int List::listEmpty()
 {
if(START==NULL)
return 1;
else
return 0;
 }
int List::delNode(int rollno)
 {
Node *current,*previous;
if(Search(rollno,&previous,&current)==0)
return 0;
previous->next=current->next;
if(current==START)
START=START->next;
delete current;
return 1;
 }
int List:: Search(int rollno,Node **previous,Node **current)
 {
*previous=START;
*current=START;
while((*current!=NULL)&&(rollno!=(*current)->rollnumber))
 {
*previous=*current;
*current=(*current)->next;
      }
return(*current!=NULL);
 }
void List::traverse()
 {
if(listEmpty())
cout<<"\n List is enpty\n";
else
 {
  cout<<endl<<"The record in the list are:"<<endl;
  Node *currentNode;
 
for(currentNode=START;currentNode!=NULL;currentNode=currentNode->next)
 {
cout<<currentNode->rollnumber<<" "<<currentNode->name<<"\n";
 }
cout<<endl;
}
 }
void main()
 {
clrscr();
List obj;
int rollno;
char ch;
do
  {
   cout<<"Main Menu";
   cout<<endl<<"1.Add a record to the list"<<endl;
   cout<<"2.Delete a record from the list"<<endl;
   cout<<"3.View all the records in the list"<<endl;
   cout<<"4.Search for a record in the list"<<endl;
   cout<<"5.Exit"<<endl;
   cout<<"Enter the choice:";
   cin>>ch;
   switch(ch)
{
 case '1':
                     obj.addNode();
                     break;
 case '2':
                     if(obj.listEmpty())
                      {
                       cout<<endl<<"List is empty"<<endl;
                       break;
                      }
                     cout<<endl<<"\n Enter the roll number of the student  
                                           whose record is to be delete:";
                     cin>>rollno;
                     if(obj.delNode(rollno)==0)
                          cout<<endl<<"Record not found"<<endl;
                     else
                      {
                          cout<<endl<<"Record with roll
                                           number"<<rollno<<"delete"<<endl;
                      }
                     break;
 case '3':
                     obj.traverse();
                     break;




 case'4':
                     if(obj.listEmpty()==1)
                      {
                        cout<<"\n List is empty\n";
                        break;
                      }
                     Node *previous,*current;
                     cout<<endl<<"Enter the roll number of the student whose
                                                  record is to be searched:";
                     cin>>rollno;
                     if(obj.Search(rollno, &previous, &current)==0)
                          cout<<endl<<"Record not found"<<endl;
                     else
                      {
                        cout<<endl<<"Record found"<<endl;
                        cout<<"\nRoll number:"<<current->rollnumber;
                        cout<<"\n\nName:"<<current->name;
                        cout<<"\n";
                      }
                     break;
     case '5':
                      exit(0);
                      break;
   }
       }while(ch!=5);
getch();
 }

No comments:

Related Posts with Thumbnails