0% found this document useful (0 votes)
9 views3 pages

Using Namespace Class Public Int Int Int Return: #Include #Include

This C++ program defines a Node class to represent nodes in a linked list and a List class to manage the linked list. The List class includes methods to add nodes, delete nodes by value, and print the list. The main function tests the List class by adding and deleting nodes and printing the list after each operation.

Uploaded by

Dragu Stelian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Using Namespace Class Public Int Int Int Return: #Include #Include

This C++ program defines a Node class to represent nodes in a linked list and a List class to manage the linked list. The List class includes methods to add nodes, delete nodes by value, and print the list. The main function tests the List class by adding and deleting nodes and printing the list after each operation.

Uploaded by

Dragu Stelian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

#include <stdlib.h>
using namespace std;
class Node
{
public:
int info;
Node *next;
Node(int x, Node *pnext):info(x), next(pnext) {};
int getInfo()
{
return info;
}
Node *Next()
{
return next;
}
void setNext(Node *pnext)
{
next = pnext;
}
};
class List
{
public:
Node *head;
List():head(NULL) {};
void Add(int info)
{
Node *new_node = new Node(info, NULL);
Node *temp = head;
if (temp != NULL)
{
while (temp->Next() != NULL)
temp = temp->Next();
temp->setNext(new_node);
}
else
head = new_node;
}
void Delete(int info)
{
Node *temp = head;
if (temp == NULL)
exit(0);
if (temp->Next() == NULL)
{
delete temp;
head = NULL;
}
else
{
Node *prev;
do

{
if (temp->getInfo() == info)
break;
prev = temp;
temp = temp->Next();

}
while (temp != NULL);
prev->setNext(temp->Next());
delete temp;

}
}
void Print()
{
Node *temp = head;
if (temp == NULL)
{
cout << "\n Empty list!" << endl;
exit(0);
}
if (temp->Next() == NULL)
{
cout << " " << temp->getInfo();
cout << " ->";
cout << " NULL" << endl;
}
else
{
do
{
cout << " " << temp->getInfo();
cout << " ->";
temp = temp->Next();
}
while (temp != NULL);
cout << " NULL" << endl;
}
}
};
int main()
{
List obj;
cout << endl;
obj.Add(1);
obj.Print();
obj.Add(2);
obj.Print();
obj.Add(3);
obj.Print();
obj.Add(4);
obj.Print();
obj.Delete(4);
obj.Print();
obj.Delete(3);
obj.Print();

obj.Delete(2);
obj.Print();
return 0;

You might also like