Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
C#
#include<iostream>
using namespace std;
class A
{
typedef struct node
{
public:
int data;
node *next;
};
node *head;
head=(node*)malloc(sizeof(node));
head=NULL;
public void createNode()
{
for(int i=1;i<4;i++)
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->data=i;
temp->next=head;
head=temp;
}
}
public void display()
{
for(int i=1;i<4;i++)
{
node *temp1;
temp1=(node*)malloc(sizeof(node));
temp1=head;
cout<<temp1->data<<"->";
temp1=temp1->next;
}
}
};
int main()
{
for(int i=1;i<=3;i++)
{
A ob[i];
ob[i].createNode();
}
A ob1;
ob1.display();
}
Posted
Comments
nv3 18-Jul-14 3:28am    
Your code does not compile. The line "node *head;" is outside any member function in class A. Give it a little more attention before you turn in your homework for others to do.
AbinayaThiyagarajan 18-Jul-14 9:20am    
Thank you.Btw this is not homework,I'm just practising.

Well, your main makes little sense. Try:
C++
int main()
{
  A ob[3];
  // create 3 identical lists of 4 nodes
  for(int i = 1; i <= 3; i++)
  {
    ob[i-1].createNode();
  }

  ob[0].display();
}
 
Share this answer
 
Comments
AbinayaThiyagarajan 17-Jul-14 16:18pm    
Still its showing error at the place where head node is initially created ! :/
Matt T Heffron 17-Jul-14 16:26pm    
The main was so bad I didn't look at the rest!
Your head initialization code isn't even in a function!
Don't create a head node.
The head should be NULL until there's something in the list.
display() should NEVER be malloc-ing a new node.
The first 2 lines (after removing the line with malloc) of the body of the for loop there should be before the for statement.
And the for statement should be while (temp1 != NULL)
AbinayaThiyagarajan 17-Jul-14 16:34pm    
Its very confusing.
To display the nodes ,i need to traverse.For traversing ,i need a new pointer of type node in my display function.
Without malloc-ing how will i allocate memory?? O_o
will you please edit the code and show me?
In the meanwhile, I will also try to debug.
Matt T Heffron 17-Jul-14 17:11pm    
You've declared the node* (temp1).
Just assign to it.
You don't need to create an object of the target of the pointer before you can use the pointer.
In fact, what you have leaked the memory of the malloc-ed nodes in display()!

This looks very much like class-related work, so I'm not going to do it for you, but I'll try to point you in the right direction.
AbinayaThiyagarajan 17-Jul-14 22:05pm    
Errr,Thank you anyways. But this is not class related work,I'm preparing for my placements.Basically I'm allergic to pointers and linked lists but this was the first time i tried on my own after reading the article.
C#
#include<iostream>
using namespace std;
 struct node{
    int data;
    struct node* next;
};

struct node* insertFront(struct node *head,int k){
    node* newnode=new node;
    newnode->data=k;
    newnode->next=head;
    head=newnode;
    return head;
}
void display(struct node *head1){
    node *temp;
    temp=head1;
    while(temp!=NULL){
    cout<<temp->data<<"->";
    temp=temp->next;
    }
}
int main(){
     struct node *head=new node;
     head=NULL;
     head=insertFront(head,5);
     head=insertFront(head,6);
     display(head);
    }
 
Share this answer
 
Comments
nv3 20-Jul-14 17:25pm    
Fine that you finally found the solution to your question. But why did you post it in the first place if it was so easy to solve it by yourself? And then even post two solutions? Are you kidding us?
AbinayaThiyagarajan 21-Jul-14 8:40am    
If u don't want to help ,just leave.
AbinayaThiyagarajan 21-Jul-14 8:42am    
Go read the terms and policy of the site where u created the account.Its my right to ask doubts,to post solution when i find the answer.did i ask u specifically ?? or did u spend too much time to point out the errors?? DON'T WRITE COMMENTS JUST FOR THE SAKE OF WRITING.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900