// File : Suitors.h
#ifndef SUITORSLIST_H   
#define SUITORSLIST_H
#include <iostream>
using std::cout;
using std::endl;

class SuitorsList
{
private:
    struct ListNode
    {
        int value;
        ListNode * next;
    };
    ListNode * head;

public:
    SuitorsList()
    {
        head = NULL;
    }
    void appendNode(int num);
    void deleteNode(int num); 
    void displayList() const;
};

#endif;

// File : suitors.cpp
#include "Suitors.h"

void SuitorsList::appendNode(int num)
{
    ListNode * newNode;

    newNode = new ListNode;
    newNode->value = num;
    newNode->next = NULL;


    if(!head)
    {
        head = newNode;
    }
    else 
    {
        ListNode * nodePtr;

        nodePtr = head;
        while(nodePtr->next)
        {
            nodePtr = nodePtr->next;
        }
        nodePtr->next = newNode;

    }

}

void SuitorsList::deleteNode(int num)
{
    ListNode * nodePtr;
    ListNode * prevPtr;

    if(!head)
        return;
    if(head->value == num)
    {
        nodePtr = head->next;
        delete head;
        head = nodePtr;
    }
    else
    {
        nodePtr = head;
        while(nodePtr && nodePtr->value != num)
        {
            prevPtr = nodePtr;
            nodePtr = nodePtr->next;
        }
        if(nodePtr)
        {
            prevPtr->next = nodePtr->next;
            delete nodePtr;
        }
        else 
        {
            cout<< "value"<< num << "not found"<<endl;
        }
    }
}

void SuitorsList::displayList() const
{
    ListNode * cur;

    cur = head;
    while(cur)
    {
        cout<< cur->value << " ";
        cur = cur->next;
    }
    cout<<endl;
}

Recommended Answers

All 7 Replies

First: please use the code-tags around your code
Second: What is the question?

iam trying to write circular linked list but iam not able run this program
may i know what is problem regarding circular linked list in appendnode and delte node

#ifndef SUITORSLIST_H
#define SUITORSLIST_H
#include <iostream>
using std::cout;
using std::endl;

class SuitorsList
{
private:
struct ListNode
{
int value;
ListNode * next;
};
ListNode * head;

public:
SuitorsList()
{
head = NULL;
}
void appendNode(int num);
void deleteNode(int num);
void displayList() const;
};

#endif;

#include "Suitors.h"

void SuitorsList::appendNode(int num)
{
ListNode * newNode;

newNode = new ListNode;
newNode->value = num;
newNode->next = NULL;


if(!head)
{
head = newNode;
}
else 
{
ListNode * nodePtr;

nodePtr = head;
while(nodePtr->next)
{
nodePtr = nodePtr->next;
}
nodePtr->next = newNode;

}

}

void SuitorsList::deleteNode(int num)
{
ListNode * nodePtr;
ListNode * prevPtr;

if(!head)
return;
if(head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
nodePtr = head;
while(nodePtr && nodePtr->value != num)
{
prevPtr = nodePtr;
nodePtr = nodePtr->next;
}
if(nodePtr)
{
prevPtr->next = nodePtr->next;
delete nodePtr;
}
else 
{
cout<< "value"<< num << "not found"<<endl;
}
}
}

void SuitorsList::displayList() const
{
ListNode * cur;

cur = head;
while(cur)
{
cout<< cur->value << " ";
cur = cur->next;
}
cout<<endl;
}

There is no main() in the code you posted: So there is nothing to run.

The rule for handling linked lists: When appending, create the new node, then point it at the list, then point the list at it. When deleting, point the list around it, then remove it (somehow, depends on whether nodes are re-used or deleted)

i tried to main fucntion also but iam not able to run
can u tell me what is code mistake in append node for circular linked list

// File : Suitors.h 
#ifndef SUITORSLIST_H	
#define SUITORSLIST_H
#include <iostream>
using std::cout;
using std::endl;

class SuitorsList
{
private:
	struct ListNode
	{
		int value;
		ListNode * next;
	};
	ListNode * head;

public:
	SuitorsList()
	{
		head = NULL;
	}
	void appendNode(int num);
	void deleteNode(int num); 
	void displayList() const;
};

#endif;
// File : suitors.cpp
#include "Suitors.h"

void SuitorsList::appendNode(int num)
{
	ListNode * newNode;

	newNode = new ListNode;
	newNode->value = num;
	newNode->next = NULL;
	

	if(!head)
	{
		head = newNode;
	}
	else 
	{
		ListNode * nodePtr;

		nodePtr = head;
		while(nodePtr->next)
		{
			nodePtr = nodePtr->next;
		}
		nodePtr->next = newNode;
		
	}

}

void SuitorsList::deleteNode(int num)
{
	ListNode * nodePtr;
	ListNode * prevPtr;

	if(!head)
		return;
	if(head->value == num)
	{
		nodePtr = head->next;
		delete head;
		head = nodePtr;
	}
	else
	{
		nodePtr = head;
		while(nodePtr && nodePtr->value != num)
		{
			prevPtr = nodePtr;
			nodePtr = nodePtr->next;
		}
		if(nodePtr)
		{
			prevPtr->next = nodePtr->next;
			delete nodePtr;
		}
		else 
		{
			cout<< "value"<< num << "not found"<<endl;
		}
	}
}

void SuitorsList::displayList() const
{
	ListNode * cur;

	cur = head;
	while(cur)
	{
		cout<< cur->value << " ";
		cur = cur->next;
	}
	cout<<endl;
}
// File : main.cpp
#include "Suitors.h"
using namespace std;
int main()
{
	int i,n;
	SuitorsList List;
	cout<<"enter number of suitors"<<endl;
	cin >> n;
	for(i=1;i<n+1;i++)
	{
		List.appendNode(i);
	}
	List.displayList();
/*	for(i=3;i<n+1;i+3)
	{
		List.deleteNode(i);
		List.displayList();
	}*/
return 0;

}

When you append to a linked list:

  1. Create the node You did it
  2. Link the node to the list Where is this code?
  3. Link the list to the node You did only part of it

You need to think "Why is it called a circular linked list?" or "What are the properties of a circular linked list and how can I make my structure act that way?". At this point, you are working very hard to insert the node at the end of a singly linked list that is not circular. I think this is the essence of the problem you have been set, not just any linked list.

can u give the correct code example of the circular linked list

I will not do your homework for you. YOU need to understand this stuff. It is not particularly difficult if you have been doing the work that leads up to it.

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.