Hey guys Im having problem with this question language is c

Hey guys, I\'m having problem with this question.

language is c++ .

2) Write an OOP program to store integers into a linked list.

    Add code so that the linked list sorted

    Add a function so you can search for an integer

    Have a function to ask the user for input.

    Add a function to delete and integer

    Have the ability to know/print out the count of the number of integers

    Have a function to print out the array.

Solution

Here is the program to create a sorted list with other functions as requested. A menu is displayed to user to choose what operation needs to be performed on the list. The list remains sorted.. A sample runof the proram is attached. Please do rate the answer if you are happy with program. Thanks.

=====================

#include<iostream>
using namespace std;
//node in list with data and next pointer
class node
{
   int num;
   node *next;
   public:
       node(int n,node *link)
       {
           num=n;
           next=link;
       }
       int data()
       {
           return num;
       }
       node *link()
       {
           return next;
       }
       void set_link(node *n)
       {
           next=n;
       }
};
//Class to create and maintain a sorted list
class sorted_list
{
   node *start;
   public:
       sorted_list()
       {
           start=NULL;
       }

//adds a new integer into list with the starting node, keeps the list sorted.
//finds the place where to insert the integer to that list remains sorted.
//if the start node was null, then it gets updated
   void add(int n)
   {
       if(start==NULL)
           start=new node(n,NULL);
       else
       {
           node *p=start,*prev=NULL;
           while(p!=NULL && p->data()<n)
           {
               prev=p;
               p=p->link();
              
           }
           //insertion before the first node
           if(prev==NULL)
           {
               start=new node(n,p);
             
           }
           else
           {
               prev->set_link(new node(n,p));
           }
       }  
   }
   //returns the count of integers in the list
   int size() const
   {
       node *p=start;
       int count=0;
       while(p!=NULL)
       {
           p=p->link();
           count++;
       }
       return count;
   }
  
   //function to find if an integer is in the list. returns true if found and false otherwise
   bool contains(int n)
   {
       node *p=start;
         
       while(p!=NULL)
       {
           if(p->data()==n)
               return true;
           p=p->link();
          
       }
       //already reached end of list and not found number
       return false;
   }
   //removes the first occurence of the number in the sorted list. returns true if element
   //was found and removed. false otherwise
   bool remove(int n)
   {
       node *p=start,*prev=NULL;
       while(p!=NULL && p->data()!=n)
       {
           prev=p;
           p=p->link();
       }
       if(p==NULL) // no such element exists
           return false;
       if(prev==NULL)//means we are removing the first element
       {
           start=p->link();
           delete p;
       }
       else
       {
           prev->set_link(p->link());
           delete p;
       }
       return true;
   }
   //displays list contents
   void display() const
   {

       node *p=start;
       cout<<\"\ [\";
       while(p!=NULL)
       {
           cout<<p->data()<<\",\";
           p=p->link();
             
       }
        cout<<\"]\";
   }
};
int main()
{
   sorted_list list;
   int choice,num;
   //display a menu to user and depending on user\'s choce , perform appropriate function calls on the list
   while(true)
   {
       cout<<\"\ \ 1. Add \ 2.Delete \ 3.Search \ 4.Display \ 5.Exit\";
       cout<<\"\ Enter choice:\";
       cin>>choice;
       switch(choice)
       {
           case 1:
               cout<<\"\ Enter number to be added:\";
               cin>>num;
               list.add(num);
              
               break;
           case 2:
               cout<<\"\ Enter number to be deleted:\";
               cin>>num;
               if(list.remove(num))
                   cout<<\"\ First occurence of number \"<<num<<\" deleted.\";
               else
                   cout<<\"Number \"<<num<<\" was not found in list.\";
               break;
           case 3:
              
               cout<<\"\ Enter number to be searched:\";
               cin>>num;
               if(list.contains(num))
                   cout<<\"\ Number \"<<num<<\" found in list.\";
               else
                   cout<<\"\ Number \"<<num<<\" was not found in list.\";
              
               break;
           case 4:
               cout<<\"\ List contains \"<<list.size()<<\" elements:\";
               list.display();
               break;
           case 5:
               return 0;
           default:
               cout<<\"\ Invalid choice\";
       }
   }

     
}

=====================

sample output

================

1. Add
2.Delete
3.Search
4.Display
5.Exit
Enter choice:1

Enter number to be added:5


1. Add
2.Delete
3.Search
4.Display
5.Exit
Enter choice:1

Enter number to be added:3


1. Add
2.Delete
3.Search
4.Display
5.Exit
Enter choice:1

Enter number to be added:7


1. Add
2.Delete
3.Search
4.Display
5.Exit
Enter choice:4

List contains 3 elements:
[3,5,7,]

1. Add
2.Delete
3.Search
4.Display
5.Exit
Enter choice:2

Enter number to be deleted:9
Number 9 was not found in list.

1. Add
2.Delete
3.Search
4.Display
5.Exit
Enter choice:2

Enter number to be deleted:5

First occurence of number 5 deleted.

1. Add
2.Delete
3.Search
4.Display
5.Exit
Enter choice:3

Enter number to be searched:5

Number 5 was not found in list.

1. Add
2.Delete
3.Search
4.Display
5.Exit
Enter choice:4

List contains 2 elements:
[3,7,]

1. Add
2.Delete
3.Search
4.Display
5.Exit
Enter choice:3

Enter number to be searched:7

Number 7 found in list.

1. Add
2.Delete
3.Search
4.Display
5.Exit
Enter choice:5

Hey guys, I\'m having problem with this question. language is c++ . 2) Write an OOP program to store integers into a linked list. Add code so that the linked li
Hey guys, I\'m having problem with this question. language is c++ . 2) Write an OOP program to store integers into a linked list. Add code so that the linked li
Hey guys, I\'m having problem with this question. language is c++ . 2) Write an OOP program to store integers into a linked list. Add code so that the linked li
Hey guys, I\'m having problem with this question. language is c++ . 2) Write an OOP program to store integers into a linked list. Add code so that the linked li
Hey guys, I\'m having problem with this question. language is c++ . 2) Write an OOP program to store integers into a linked list. Add code so that the linked li

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site