#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);
	}

Dani AI

Generated

This thread mixes simple syntax slips with deeper design and ownership problems. originally had several compile/run issues; fixed many of the syntax problems and and raised correct points about allocation behavior. Below are focused, practical recommendations that stay useful over time.

Keep the list head as a class member, not as a local variable in insert. Initialize it in the constructor and ensure nodes are destroyed in a destructor — or better, use RAII. Modern C++ makes this easy and far safer via std::unique_ptr, which avoids manual delete and most leaks (see unique_ptr). Handle allocation strategy explicitly: by default new throws std::bad_alloc; only use the nothrow variant if you intend to check for a null result (see operator new). Use the standard form of main as documented (see main function) and prefer nullptr over old null macros (see nullptr).

Minimal pattern using smart pointers:

#include <memory>

struct Node {
  int value;
  std::unique_ptr<Node> next;
  Node(int v): value(v) {}
};

class LinkedList {
  std::unique_ptr<Node> head;
public:
  void insert_front(int v) {
    auto n = std::make_unique<Node>(v);
    n->next = std::move(head);
    head = std::move(n);
  }
};

Compile with warnings enabled (for example -Wall -Wextra) and run a leak checker (Valgrind or similar) to catch lingering issues.

Recommended Answers

All 5 Replies

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

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?

thxxxxxxxxxx alote for all :)

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";
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.