I need to verify with some one if my code is right i have to submit it in 3 hours from now
and any help will be welcome

The instructions are:

Using an appropriate definition of listnode, design a simple linked list class with only two member function and a defult constructor:

void add(double x)
boolean isMumber(double x)
LinkedList();

The add function adds a new node containing x to the front (head) of the list,
while the isMember function tests to see if the list contains a node with the value x. Test your linked list class by adding various numbers to the list and then testing for membership

second part of the question :

Modify your list class of programming challenge 1 to add a copy constructor. Test your class by making a copy of a list and then testing membership on the copy.

third part :

Modify the list class you created in the previous programming challenge to add a print member function. Test the class by starting with an empty list, adding some elements, and then printing the resulting list out.

The header file

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
class linkedlist
{
private:
struct listNode
{
double x;
struct listNode *next;
};
listNode *head;
public:
linkedlist()
{
head = NULL;
}
// copy constructor
linkedlist( const linkedlist& otherList );
//add member fuunction
void add(double );
//isMumber member function
bool isMember(double );
//Print memberfunction
void print() const;
};
#endif

and the cpp file

#include<iostream>
#include "linkedlist.h"
using namespace std;
linkedlist::linkedlist( const linkedlist& otherList )
{
head = NULL;
listNode *newNode;
listNode *nodePtr;
listNode *tempPtr;
if ( !otherList.head )
return;
nodePtr = otherList.head;
head = new listNode;
head->x = nodePtr->x;
head->next = NULL;
nodePtr = nodePtr->next;
tempPtr = head;
while ( nodePtr != NULL )
{
newNode = new listNode;
newNode->x = nodePtr->x;
newNode->next = NULL;
tempPtr->next = newNode;
tempPtr = newNode;
nodePtr = nodePtr->next;
}
}
void linkedlist::add( double x )
{
listNode *newNode;
listNode *nodePtr;
listNode *previousNode = NULL;
newNode = new listNode;
newNode->x = x;
newNode->next = NULL;
if ( !head )
head = newNode;
else
{
nodePtr = head;
previousNode = NULL;
while ( nodePtr != NULL && nodePtr->x < x )
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if ( previousNode == NULL )
{
head = newNode;
newNode->next = nodePtr;
}
else
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
bool linkedlist::isMember(double x)
{
listNode *nodePtr;
double pos=-1;
if ( !head )
{
cout << "The list is empty";
return-1;
}
nodePtr = head;
while (nodePtr)
{
pos++;
if(nodePtr->x== x)
return pos;
else
nodePtr=nodePtr->next;
}
return -1;
}
void linkedlist::print() const
{
listNode *nodePtr;
if ( !head )
{
cout << "The list is empty.";
return;
}
nodePtr = head;
cout << "\n The elements in the list are:";
while (nodePtr)
{
cout << nodePtr->x << " -> ";
nodePtr = nodePtr->next;
}
cout << "Null";
}
#include<iostream>
using std::cout;
using std::cin;
#include "linkedlist.h"
int main()
{
linkedlist myList;
char choice;
int n;
do {
cout << "\nInsert a node : I";
cout << "\nQuit : Q";
cout << "\nEnter your choice: ";
cin >> choice;
switch ( choice )
{
case 'I' :
case 'i' :
cout<<"\tEnter an integer: ";
cin >> n;
myList.add( n );
break;
case 'Q' :
case 'q' :
choice = 'q';
}
} while ( choice != 'q' );
return 0;
}

Recommended Answers

All 2 Replies

1) Not going to even look at code that is that poorly formatted. Learn to format your code if you want help.
2) Why do you assume we have time to test your program? If you ran it with a good set of data and it didn't have a problem, it's probably good enough.

#include <iostream.h>
#include <conio.h>
struct link    {
int no;
char stname[30];
char dep;
link* next;
               };
           link *first;
   //void additem(int no, char name , int dep);
   void additem(int no);
   void display();
              void  main()  {
       int x;
      char stname[30];
      char dep;
      char a;

       first= NULL;
       for(int i=0;i<3;i++)
       {
       cout <<"enter the no. of the student x:"<<(i+1)<<" ";
       cin>>x;
       cout<<x;
       cin.get(a);
       cout <<" enter the name of the student :";
       cin.get(stname,29);
       cin.get(a);
       cout<< "enter the student department:";
       cin>> dep;
       cin.get(a);
       additem(x);
       }
       cout<< " the linked list are:";
       display();
       getch();
       }
       void additem(int x)
                    {
                    link* node= new link;
                    node->no=x;
                    node->next=first ;
                    first=node;
                    }
       void display()
                    {
                    link* current =first;
                    while(current!=NULL)
                    {
                    cout<<endl<<current->no;
                             // cout<<endl<<current->no<<current->stname<<current->dep;
                      current=current->next;

                                         }
                     }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.