Lab.14.oop - Syed Mujtaba Ali Shah.22i-6176.c

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

LAB 14 OOP:

NAME: SYED MUJTABA ALI SHAH


ROLL NO: 22I-6176
SECTION: C
INSTRUCTOR: NIMRA FATIMA
CODE:
MAIN CODE:

#include <iostream>
using namespace std;

struct nodeType{
int info;
nodeType *link;
};

class linklist{
nodeType *head;
public:
linklist() { head = NULL; }
~linklist();
void displayList();
void AddatFront(int);
void AddatEnd(int);
void deleteValue(int val);
void deleteFirst();
bool operator==(const linklist& otherList);
void operator+=(const linklist& otherList); // declaration of the
overloaded operator+=
};
void linklist::deleteFirst(){
nodeType *d = head;
head = head->link;
delete d;
}

void linklist::deleteValue(int x){


nodeType *prev, *d;
d = head;
while(d->info != x){
prev = d;
d = d->link;
}
prev->link = d->link;
delete d;
}

void linklist::AddatFront(int x){


nodeType *ptr;
ptr = new nodeType;
ptr->info = x;
ptr->link = head;
head = ptr;
}

void linklist::displayList(){
nodeType *curr = head;
while(curr!=NULL){
cout<<curr->info<<" ";
curr=curr->link;
}
cout<<endl;
}

void linklist::AddatEnd(int x){

nodeType *ptr = head;

while(ptr->link!=NULL)
{

ptr=ptr->link;
}
nodeType* ptr1 = new nodeType;
ptr1->info=x;
ptr1->link=NULL;
ptr->link=ptr1;

//Add your own code


}
bool linklist::operator==(const linklist& otherList){
nodeType *curr1 = head;
nodeType *curr2 = otherList.head;
while(curr1 != NULL && curr2 != NULL){
if(curr1->info != curr2->info){
return false;
}
curr1 = curr1->link;
curr2 = curr2->link;
}
if(curr1 != NULL || curr2 != NULL){
return false;
}
return true;
}

void linklist::operator+=(const linklist& otherList) {


if (head == NULL) { // if the first list is empty
head = otherList.head;
}
else { // if the first list is not empty
nodeType* ptr = head;
while (ptr->link != NULL) {
ptr = ptr->link;
}
ptr->link = otherList.head;
}
}

linklist::~linklist() {
nodeType* del = head;
while (del != NULL) {
nodeType* next = del->link;
delete del;
del = next;
}
head = NULL;
}

MAIN BODY + HEADER FILE:

#include "LAB_14.h"

int main() {
linklist list1;
list1.AddatFront(44);
list1.AddatFront(23);
list1.AddatFront(65);
list1.AddatFront(10);
list1.displayList();
list1.deleteValue(44);
list1.displayList();
list1.deleteFirst();
list1.displayList();

linklist list2;
list2.AddatFront(500);
list2.AddatFront(400);
list2.AddatEnd(300);
list2.displayList();

if (list1 == list2) {
cout << "The two linked lists are identical." << endl;
} else {
cout << "The two linked lists are not identical." << endl;
}

(list1 += list2) += list3;


cout << "Concatenated List: ";
list1.displayList();

return 0;
}

You might also like