1

I tried implementing linked list on c++.Below here is my code:

#include<iostream>
using namespace std;
class lnk
{
 struct node
 {
   int data;
   node *next;
 };

node* insert(node *head,int data)
 {
   if(head==NULL)
  {
    node *temp;
    temp->data=data;
    temp->next=NULL;
    head=temp;

  }
   else
   head->next=insert(head->next,data);

   return head;

  }

  node* find(node *head,int data)

 {    while(head!=NULL)
    {
   if(head->data==data)
   return head;
   else
   head=head->next;
 }
 cout<<"sorry";
 return NULL;
 }

 void delete(node *head,int data)
{
  node *temp=find(head,data);
  if(temp==NULL)
   ;
  else
  if(head==temp)
 {
   head=head->next;
 }
  else
 {
   node *temp1=head;
   while(temp1->next!=temp)
  {
    temp1=temp1->next;
  }
   temp1->next=temp->next;
   delete temp;
 }
 }

  void display(node *head)
 {
  while(head!=NULL)
  {
   cout<<head->data;
   head=head->next;
  }
 }
};


 int main()
 {
   lnk o1;
   node *head=NULL;
   head=o1.insert(head,5);
   head=o1.insert(head,8);
   o1.delete(&head,5);
   o1.display(head);
   return 0;
 }

The problem is that I am unable to compile the code correctly.Firstly,while creating a pointer head in main(),it states that node is not declared in scope.I tried switching the definitions to public with no success. I am also getting error of function signature mismatch.Maybe it is due to the head pointer not properly declared. Please evaluate and provide me with a working compiled code for the problem.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
Sarthak Mehra
  • 359
  • 1
  • 2
  • 12
  • Post the compiler errors, indicate the line numbers mentioned in the errors. – BeyelerStudios Jun 06 '16 at 12:25
  • Please use proper variable, class and function names – Khalil Khalaf Jun 06 '16 at 12:25
  • You really need to fix your indentation. As it it makes your code almost unreadable. – NathanOliver Jun 06 '16 at 12:26
  • You've missed the mark on object oriented programming as well as the need for memory allocation. I would suggest going back an reviewing all of your reading material and starting over. – crashmstr Jun 06 '16 at 12:28
  • `lnk` has no data members. Why does the class exist? (It looks like you've taken a C implementation and attempted to make it "object-oriented" by putting it inside a class.) – molbdnilo Jun 06 '16 at 12:29
  • 1
    Replacing your old question with a new question makes the existing answers incomprehensible and pointless. I have rolled back to the original. If you have a follow-up question, post a new one instead of editing one that is already answered. – molbdnilo Jun 06 '16 at 13:28

2 Answers2

1
lnk o1;
node *head=NULL;

node is an inner class in lnk. That's how you declared it. Either stick a

typedef lnk::node node;

at the beginning of main(), or replace all references in main() to node with lnk::node.

EDIT: forgot to notice that node is private. You'll also have to make it a public inner class.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Never going to work. `node` is private, actually all of his class is private. – NathanOliver Jun 06 '16 at 12:27
  • Thank you.I had declared the methods public but maybe I had forgot to copy the new revised code while asking the question in the community.Sorry for any causing any inconvenience. – Sarthak Mehra Jun 06 '16 at 12:56
0

First of all you need to declare them public:

class lnk
{
public:

After that you need to declare the scope of the node:

lnk::node *head=NULL;

Third don't use "delete". this is reserved keyword:

void deleteNode(node *head,int data)

Doing this three things will compile you're code. I don't think it will work. If you want an working example of linked list:

Segmentation fault (core dumped) when I delete pointer

Community
  • 1
  • 1
Heto
  • 590
  • 4
  • 9
  • The code has worked for a link list as desires after fixing the syntax errors.Only the delete function is not working though I have prefixed & in the calling definatin for head. – Sarthak Mehra Jun 06 '16 at 12:52