Hi! everyone ,
I have created a little code to implement the linked list ,everything works fine but it is creating one exta node everytime a run this program ,so I'am attaching the code below ,help for fixing this bug . Thanks in advance .
Code :
#include<iostream>
#include<stdio.h>
using namespace std;
struct node{ //defining ADT
int data;
node* link;
};
typedef node* nodeptr; //giving name to node
nodeptr head=new node; //creating the head pointer
bool insertatfront(int n,nodeptr &head) //function to inserting a element
{
//i think the bug exist in this function
nodeptr newnode=new node;
if(!newnode) return false;
newnode->data=n;
newnode->link=head;
head=newnode; //it updating the local copy of the head only
The extra node you're seeing is the initial empty node you create when you initialise head. But head should initially be null, and insert should deal with an empty head.