1

Whats wrong with this code? I am writing this code to implement singly linked list using arrays but its not working. Im using code::blocks and its crashing on run time. Please help. I must have missed out on something when it was taught in the class. xD

#include<iostream>
#include<stdio.h>
using namespace std;
class Node
{
 int data;
 Node *next;
public:
 Node(int n)
 {
 data=n;
 next=NULL;
 }
 friend class List;
};
class List
{
 Node *listptr;
public:
void create();
void display();
};
void List::create()
{
 Node *temp;
 int n, num;
 cout << "Enter number of nodes:" << endl;
 cin >> n;
 cout << "/nEnter the data" << endl;
 for(int i=0; i<n; i++)
 {
 cin >> num;
 Node *new_node=new Node(num);
 if(listptr==NULL)
 listptr=temp=new_node;
 else
 {
 temp->next=new_node;
 temp=temp->next;
 }
 }
}
void List::display()
{
 Node *temp=listptr;
 while(temp!=NULL)
 {
 cout << temp->data << "->";
 temp=temp->next;
 }
}
 main()
{
 List l1;
 l1.create();
 l1.display();
}
afic
5204 silver badges13 bronze badges
asked Jan 15, 2018 at 15:00
6
  • 6
    I recommend you learn how to debug your programs. Especially how to use a debugger to catch crashes and location where they happen. Commented Jan 15, 2018 at 15:04
  • Looking at the code I'm confused if this is supposed to be C or C++ too. Commented Jan 15, 2018 at 15:04
  • 1
    Node *temp; is never initialized. Commented Jan 15, 2018 at 15:05
  • @Someprogrammerdude okay I'll learn it! thanks a ton! :) Commented Jan 15, 2018 at 15:11
  • You haven't used any array. If you're supposed to do that, you probably should. Commented Jan 15, 2018 at 15:17

2 Answers 2

1

listptr not initialized, you can initialize in constructor.

 List() {
 listptr = 0;
 }

Class List should be

class List
{
 Node *listptr;
 public:
 List() {
 listptr = 0;
 }
 void create();
 void display();
};
answered Jan 15, 2018 at 15:06

1 Comment

OMG you are a life saver! Thanks a lot!
0

Try following piece of code -

First Creating Node

class ListElement
{
 int data;
 ListElement* next;
public:
 void set_element(int item) { data = item; }
 int get_value() { return data; }
 friend class List;
};

Another class for further operation

class List
{
 ListElement *Start, *Tail, *New;
public:
 List() { Start = Tail = New = NULL; } // initialise all pointer value to NULL
 void add_element(int element) {
 // Create a new Node
 New = new ListElement;
 New->set_element(element);
 New->next = NULL;
 // adding value or linkig each node to each other
 (Start == NULL) ? Start = New : Tail->next = New;
 Tail = New;
 }
 // print the whole linked list
 void print()
 {
 ListElement* Current = Start;
 while (Current != NULL)
 {
 cout << Current->get_value() << endl;
 Current = Current->next;
 }
 }
};

Main Function

int main()
{
 List L;
 int num_of_element, element;
 cin >> num_of_element;
 for (int i(0); i < num_of_element; i++) {
 cin >> element;
 L.add_element(element);
 }
 L.print();
}

Hope it'll work.

answered Jan 15, 2018 at 15:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.