0% found this document useful (0 votes)
2 views7 pages

Assignment

The document contains a C++ implementation of a custom linked list class called XList, which supports adding and deleting elements from both the front and back. It uses a unique method of combining pointers for node linking, enhancing memory efficiency. The main function demonstrates the functionality of the XList class by adding, finding, and deleting elements, and displaying the list contents.

Uploaded by

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

Assignment

The document contains a C++ implementation of a custom linked list class called XList, which supports adding and deleting elements from both the front and back. It uses a unique method of combining pointers for node linking, enhancing memory efficiency. The main function demonstrates the functionality of the XList class by adding, finding, and deleting elements, and displaying the list contents.

Uploaded by

waniyasaeed2129
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment

submitted by Waniya saeed


Roll number Bsef24m057

code

#include <iostream>

#include <cstdint>

using namespace std;

class Node {

public:

int value;

Node* link;

Node(int v) {

value = v;

link = nullptr;

};

class XList {

private:

Node* start;

Node* end;
Node* combine(Node* a, Node* b)

return (Node*)((uintptr_t)(a) ^ (uintptr_t)(b));

public:

XList()

start = end = nullptr;

void addFront(int val) {

Node* temp = new Node(val);

temp->link = combine(nullptr, start);

if (start != nullptr) {

Node* next = combine(start->link, nullptr);

start->link = combine(temp, next);

else

end = temp;

start = temp;

}
void addBack(int val) {

Node* temp = new Node(val);

if (end == nullptr) {

start = end = temp;

return;

temp->link = combine(end, nullptr);

Node* prev = combine(end->link, nullptr);

end->link = combine(prev, temp);

end = temp;

int deleteFront() {

if (start == nullptr) {

cout << "Empty list\n";

return -1;

Node* temp = start;

int val = temp->value;


Node* next = combine(nullptr, temp->link);

if (next != nullptr) {

Node* nextNext = combine(temp, next->link);

next->link = combine(nullptr, nextNext);

start = next;

else {

start = end = nullptr;

delete temp;

return val;

int deleteBack() {

if (end == nullptr) {

cout << "Empty";

return -1;

Node* temp = end;

int val = temp->value;

Node* prev = combine(temp->link, nullptr);


if (prev != nullptr) {

Node* prevPrev = combine(prev->link, temp);

prev->link = combine(prevPrev, nullptr);

end = prev;

else {

start = end = nullptr;

delete temp;

return val;

bool find(int key) {

Node* curr = start;

Node* prev = nullptr;

while (curr != nullptr) {

if (curr->value == key)

return true;

Node* next = combine(prev, curr->link);

prev = curr;

curr = next;

}
return false;

void display() {

Node* curr = start;

Node* prev = nullptr;

while (curr != nullptr) {

cout << curr->value << " ";

Node* next = combine(prev, curr->link);

prev = curr;

curr = next;

cout << endl;

};

int main() {

XList l;

[Link](5);

[Link](15);

[Link](25);

[Link](35);

cout << "Elements ";


[Link]();

cout << "Find 25 ";

if ([Link](25))

cout << "Yes";

else

cout << "No";

cout << endl;

cout << "Deleted from front " << [Link]() << "\n";

cout << "Deleted from back" << [Link]() ;

[Link]();

return 0;

You might also like