In: Computer Science
C++
void add(double x);
boolean isMember(double x);
LinkedList( );
The add function adds a new node containing x to the front (head) of the list, while the isMember function tests to see if the list contains a node with the value x. Test your linked list class by adding various numbers to the list and then testing for membership.
void remove(double x);
~LinkedList();
Test the class by adding a sequence of instructions that mixes operations for adding items, removing items, and printing the list.
void reverse();
The member function rearranges the nodes in the list so that their order is reversed. You should do this without creating or destroying nodes.
Code:
#include <iostream>
using namespace std;
typedef struct ListNode //ListNode structure
{
int n;
struct ListNode* next; //pointer to next node
}ListNode; //renaming struct ListNode to simply ListNode using typedef keyword
class LinkedList //LinkedList class
{
public: //the data member and all the two methods have public access i.e any other class methods or functions can use them
ListNode* head; //head node pointer
LinkedList() //null constructor
{
head = NULL;
}
void add(double x) //add method appends a ListNode to the front of LinkedList
{
ListNode* tmp = head; //storing current head in temporary variable
head = new ListNode; //declaring dynamic memory using new keyword
head->n = x; //assigning the new value to new node
head->next = tmp; //adding next pointer to the previous head
}
bool isMember(double x) //isMember method returns status of presence of an element in the LinkedList
{
ListNode* itr = head;
while(itr!=NULL) //iterating through the LinkedList until NULL is attained i.e exiting after last element
{
if(itr->n==x) //if the current node has the given value then returning true
return true;
itr = itr->next;
}
return false; //if no node contains the given value then returning false
}
};
int main() //main funtion to test the LinkedList class
{
LinkedList l; //declaring static LinkedList variable l
l.add(4); //adding 4 to front
l.add(3); //adding 3 to front
l.add(2); //adding 2 to front
l.add(1); //adding 1 to front
for(ListNode* itr = l.head; itr!=NULL; itr = itr->next) //printing all LinkedList node elements to screen in order of list
printf("%d -> ", itr->n);
printf("null\n");
printf("Is 3 a member of linked list or not(True[1]/False[0]): %d\n", l.isMember(3)); //testing if 3 is present in LinkedList l
printf("Is 5 a member of linked list or not(True[1]/False[0]): %d\n", l.isMember(5)); //testing if 5 is present in LinkedList l
return 0;
}
Output:
#include <iostream> using namespace std; struct node { double data; node *next; node(int data) { this->data = data; } }; class LinkedList { node *head; public: LinkedList() { head = NULL; } LinkedList(LinkedList ©) { copy.head = this->head; } void add(double data) { //create new node node *m = new node(data); //if list is empty then node is the first element if (head == NULL) { head = m; m->next = NULL; } //else add input as first element else { m->next = head; head = m; } } void remove(double key) { // Store head node node *temp = head, *prev; // If head node itself holds the key to be deleted if (temp != NULL && temp->data == key) { head = temp->next; // Changed head return; } // Search for the key to be deleted, keep track of the // previous node as we need to change 'prev->next' while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } // If key was not present in linked list if (temp == NULL) return; // Unlink the node from linked list prev->next = temp->next; } bool isMember(double data) { node *c = head; while (c != NULL) { if (c->data == data) { break; } c = c->next; } if (c == NULL) { return false; } else return true; } bool checkMember(double x) { return check(head, x); } //helper function bool check(node *elem, double x) { if (elem != NULL) { if (elem->data == x) return true; else return check(elem->next, x); } else return false; } void reverse() { // Initialize current, previous and // next pointers node *current = head; node *prev = NULL, *next = NULL; while (current != NULL) { // Store next next = current->next; // Reverse current node's pointer current->next = prev; // Move pointers one position ahead. prev = current; current = next; } head = prev; } void Print() { node *m = head; while (m != NULL) { cout << m->data << " "; m = m->next; } } ~LinkedList() { cout << "Destroyed"; }; }; int main() { LinkedList list; list.add(20); list.add(10); list.add(5); list.add(3); list.Print(); list.reverse(); list.remove(3); list.Print(); LinkedList copy; copy = list; cout << "copy:"; copy.Print(); cout<<endl<<list.isMember(5)<<endl; cout<<list.checkMember(3)<<endl; }
Sample output: