#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include<conio.h>
#include<iostream>


struct list
{
    int data;
    struct list *next;
}*L;

//function declaration:
void create(int data,struct list **head);
void print(struct list **head);

int main()
{
    //list *L;
    int data =10;
    for(int i=0;i<3;i++)
    {
    //struct list *pList = NULL;
    create(data++,&L);
    print(&L);
    getch();
    }
}
void  create(int data,list **head)
{
    list *pNew = new list;
  if (*head==NULL)
    {
          (*head)->next=NULL;
          (*head)->data=data;
      //(**head).data=info;
        //pNew->data =data;
        //pNew->next=*head;
    }
  else
  {
      pNew =*head;
      while(pNew!=NULL)
      {
          //*head=(**head).next;
          pNew=pNew->next;
      }
      list *temp =new list;
      temp->data=data;
      temp->next=NULL; 
      pNew->next =temp;

      //(**head).next=(**head).next;
  }
    //return 0;
}

void print(list **head)
{
    list *temp1 = new list;
    temp1 =*head;
    while( temp1!=NULL )
{
 std::cout<< temp1->data<<" ";// show the data in the linked list
 temp1 = temp1->next;   // tranfer the address of 'temp->next' to 'temp'
}
}

Hello Guys,

I am a bit rusty on pointers,i actually want to implement linked list ,but my compiler is giving error.

i am getting run time error at line

(head)->next=NULL;
(
head)->data=data;

Would be great if someone can review the short snippet code and point my mistake.
Any other suggestions are always welcome.

Please note that i have also tried using (**head).data=data; but then also i am still getting runtime error .
i am not sure how to proceeed next.

Recommended Answers

All 2 Replies

Member Avatar for Symbiatch

1) you're using C++, but including C headers. At least use the C++ variants <cstdio>, <cstdlib> etc

2) you have a pointer L which is never initialized to anything. So it points to garbage and you naturally will have lots of problems

3) in create you test if the head is null and if it is you use it!

4) in create you create a new list and never use it, so it's a memory leak

This is just a quick check, you should probably really check out a real implementation of a linked list.

Member Avatar for thendrluca

I don't understand what you want to do but a linked list like you did is a bad list
I suggest you to use another structure to save first and last element
this will be easy for you to send parameters
and remember, if you want to send a pointer like parameter
in function use the same range pointer (sorry i have a bad english)
void func(int* x);
in program use like this
int* ptr;
func(ptr); // and that's all no need pointer to pointer

#include<conio.h>
#include<iostream>
using namespace std;

struct knot {
    int data;
    struct knot *next;
};

struct list {
    struct knot* first;
    struct knot* last;
};

//function declaration:
void create(int, list&);
void print(list&);
void free(list&);

int main()
{
    list L;

    L.first = L.last = NULL;

    int data = 10;

    for(int i = 0; i < 3; i++) {
        create(data++, L);
    }

    print(L);
    getch();
}

void create(int data,list& L)
{
    knot* now = new knot;
    now->data = data;
    now->next = NULL;

    if (L.last) {
        L.last->next = now;
    } else {
        L.first = now;
    }
    L.last = now;
}

void print(list& L)
{
    knot* now = L.first;

    while (now) {
        cout << now->data << " ";
        now = now->next;
    }
}

void free(list& L)
{
    knot* now;

    while(L.first) {
        now = L.first;
        L.first = L.first->next;
        delete now;
    }
}
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.