I have written code to append, prepend and update a linked list using c++. But I am unable to figure out the problem. Can anyone help me to run it. I will be grateful for the favor.
The code is pasted below:

#include<iostream>
using namespace std;

class node {
    public:
        int data;
        node *Next;
        int key;

    node() {
        data = 0;
        Next = NULL;
        key = 0;
    }
    node(int d, int k) {
        data=d;
        key=k;
    }
//  constructor overloading used        
};
//create single link list class
class linkedList : public node 
{
    public:
        node *head;

    linkedList() {
        head = NULL;
    }
    linkedList( node *nn ) {   //new node = nn (stores the address of next node in head)
        head = nn;
    }
//  first operation to check wether node already exist or not (node duplication)
    node *nodeExist( int k)
    {
        node *temp = head; //temporary pointer(stores head node address)
        node *ptr = head; //used to jump from one node to next during comparison; starts form head

        while( ptr != NULL) {
            if(ptr -> key == k) {
                temp = ptr;
            }

            ptr= ptr -> Next; //statement to jump to next node
        }
        return temp;  //temp value may be null(if null not match with any node) or address of a node(matching occur)
    }
// function to add new node 
    void appendNode(node *nn) 
    {
        //check uniqueness of node
        if( nodeExist(nn -> key) != NULL ) {
            cout<<"key is matching with existing node(try again with different address)"<<endl;
        }
        //check if linked list is empty or have nodes
        else if(head == NULL) {
            head = nn;
        }
        else {
            node *ptr = head; //pointer starts jumping from head

            while( ptr -> Next != NULL) 
            {
                ptr = ptr -> Next; //statement to jump to next node or update ptr
            }
            ptr -> Next = nn; //stores the address of new node
        }
    }

    void display()
    {
        if(head == NULL) {
            cout<<"list is empty";
        }
        else {
            node *ptr = head;

            while( ptr != NULL ) 
            {
                cout<< ptr -> key << "," <<ptr-> data; //print key and data of node

                ptr = ptr -> Next;
            }
        }
    }
// add node at start of linked list
    void prepend(node *nn)
    {
        if (nodeExist ( nn -> key) != NULL ) {
            cout<<"key is matching with existing node";
        }
        else {
            nn -> Next = head; //store new node reference in head
            head = nn; //head stores new node address / update head
        }
    }
// update node value
    void updateNode(int k, int d) // k stores key of node to find and d stores new data value
    {
//       int k, d;
        node *ptr = nodeExist(k);

        if( ptr = NULL ) {
            ptr -> data = d; //update existing node data with new data
        }
        else {
            cout<<"node does not exist";
        }
    }
};

int main() 
{
    linkedList l;
    node n(85,7);
    int data;
    int key;
    int d;
    int k;
    int oper;

    do {
    cout<<"\nPress"<<endl;
    cout<<"[0] to exit"<<endl;
    cout<<"[1] to append a node"<<endl;
    cout<<"[2] to prepend a node"<<endl;
    cout<<"[3] to update a node"<<endl;
    cout<<"[4] to display"<<endl;
    cin>>oper;

    switch(oper)
    {
    case 0:
        cout<<"PROGRAM ENDED!!!!";
        break;
    case 1:
        cout<<"ADD DATA VALUE OF NODE: ";
        cin>>data;
        cout<<"ADD KEY OF NODE: ";
        cin>>key;
        l.appendNode();
        break;
    case 2:
        cout<<"ADD DATA VALUE OF NODE: ";
        cin>>data;
        cout<<"ADD KEY OF NODE: ";
        cin>>key;
        l.prepend();
        break;
    case 3:
        cout<<"enter key of node to update: ";
        cin>>k;
        cout<<"enter new data value: ";
        cin>>d;
        l.updateNode();
        break;
    case 4:
        cout<<"nodes are: "<<endl;
        l.display();
        break;
    }

        }while (oper != 0);
}

Recommended Answers

All 4 Replies

What happens when you run it?

commented: similarly, no matching function for call to 'linkedlist::prepend() and updateNode() +0

Looks like l is never initialized. i.e. l = new linklist(n);

You're missing a few things in your code:

Your <code>appendNode</code> function doesn't take any arguments, but you're trying to pass it one. Your <code>prepend</code> function doesn't take any arguments, but you're trying to pass it one. Your <code>updateNode</code> function doesn't take any arguments, but you're trying to pass it two.

These should be pretty easy to fix. Good luck!

For future reference, when a new post is made in a thread, that thread bubbles to the top of the thread list, assuming that, like me, people sort the thread list from newest to oldest. Even if not sorted that way, threads with new posts get displayed in blue so that they stand out.

When you reply using a comment, no changes are made to the way the thread is displayed in the thread list. Thus nobody can tell there is something new to read, and the thread gets ignored.

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.