#include <iostream>
#include <string>
#include <cmath>
#include <cstring>
#include <cctype>

using namespace std;

class link
{
	struct employee
	{
		int x;
		employee *next;
	}
	

	void insert()
		{
		employee *temp;
		temp=new employee;
		employee *first;
		first=new employee;

		if(first=NULL)
		{
		employee *tmep;
		temp=new employee;
		temp=first;
		cin>>first->x;
		first->next=NULL;
		}
		else
		{
			temp=first;
			first=temp;
			cin>>temp->x;
			temp->next=NULL;
		}
	
	   }
}

void main()
{
	int y;
do
	{
		cout<<endl;
		cout<<"Select one of the following"<<endl;
		cout<<"1- Insert "<<endl;
		cout<<"2- exit "<<endl;
	cin>>y;
	switch(y)
	{
	case 1:
		{
			void insert();
			break;
		};

	}
	}while(y!=2);
	}

Edited 7 Years Ago by Nick Evan: Added code-tags

1-Use code tags please..

2-

if(first=NULL)

Are you assigning or defining a condition?
I think it should be

if(first==NULL)

3-

void main()

in c++ is not advisable,

main()

should return a value.
Please,If this helped,mark this thread as solved and add to my reputation points...Thanks!!

"Show me your code and I will tell you who you are.."-Tkud

Edited 7 Years Ago by tkud: n/a

Hi again...I just went through your code and I found a LOT of bugs and c++ 'no-no's. However, I have modified your code...

#include <iostream>
#include <string>
#include <cmath>
#include <cstring>
#include <cctype>
using namespace std;
class link
{
   public:
   struct employee
  {
   int x;
   employee *next;
  };
     link(){cout<<"Link's constructor.."<<endl;}
     ~link(){cout<<"Link's destructor"<<endl;}
     void insert()
   {
    employee *temp;
    temp=new employee;
    employee *first=NULL;
    first=new employee;
    if(first==NULL){
      cout<<"Enter a number..";
      cin>>first->x;
      first->next=NULL;
     }
     else
      {
        cout<<"Enter a number..";
        cin>>temp->x;
        first->next=temp;
        temp->next=NULL;
      }
    }
};
int main()
{
 link x;
 int y;
 do
 {
  cout<<endl;
  cout<<"Select one of the following"<<endl;
  cout<<"1- Insert "<<endl;
  cout<<"2- exit "<<endl;
  cin>>y;
  switch(y)
   {
    case 1:
    x.insert();
    break;
    case 2:
    break;
   }
 }while(y!=2);
 return 0;
}

Please, If this helped, mark thsi thread as solved and add to my reputation points..Thanks!!!

"Show me your code and I will tell you who you are.."-Tkud

I have a question. Lines #21, #22 and #23:

employee *first=NULL;
first=new employee;
if(first==NULL){
//...

If you allocate memory for pointer 'first', is that possible for 'first' to be NULL unless you get some exception?

I have a question. Lines #21, #22 and #23:

employee *first=NULL;
first=new employee;
if(first==NULL){
//...

If you allocate memory for pointer 'first', is that possible for 'first' to be NULL unless you get some exception?

Yes, but what he is doing is wrong. If you use nothrow
when allocating for memory, then first could be null, if it failed to allocate memory.

like this :

int *p = new(nothrow) int[100000000];
if(! p ){
cout<<"Error allocating memory\n";
}
else cout<<"good\n";
This article has been dead for over six months. Start a new discussion instead.