0% found this document useful (0 votes)
6 views

Task 4

dsa code2

Uploaded by

Hassaan Awais
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Task 4

dsa code2

Uploaded by

Hassaan Awais
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

#include <iostream>

using namespace std;


class Node {
public:
Node (int data, Node* next = NULL) {
setData (data);
setNext (next);
}
void setData (int data) {
this->data = data;
}
void setNext (Node* next) {
this->next = next;
}
int getData () {
return data;
}
Node* getNext () {
return next;
}
private:
int data;
Node* next;
};
class LinkedList {
private:
void setHead (Node* head) {
this->head = head;
}
Node* getHead () {
return head;
}
Node* head;

public:
LinkedList () {
setHead (NULL);
}
bool isEmpty () {
return getHead () == NULL;
}
void addOnHead (int data) {
/*
Steps
1. if the list is empty
1.1. insert/add a new node at the head
2. if the list is not empty
2.1. Create a temporary pointer of Node type and store the reference of
the created node with data in it
2.2. Next part of temp node point/refer to the node which head is
referring to
2.3. Head node will start referring to the node which temp node is
referring to
*/

if (isEmpty ()) { // Step 1.


setHead (new Node (data)); // Step 1.1.
}
else { // Step 2.
Node* temp = new Node (data); // Step 2.1.
temp -> setNext (getHead ()); // Step 2.2.
setHead (temp); // Step 2.3.
// setHead (new Node (data, getHead ()));
}
}

void printList () {
/*
Steps
1. if the list is not empty
1.1. Create a pointer named temp and store the address of node head is
referring to
1.2. Check if temp is not NULL
1.2.1. print the data part of temp
1.2.2. move temp to the next part
1.2.3. Repeat 1.2.
*/

if (!isEmpty ()) { // Step 1.


Node* temp = getHead (); // Step 1.1.
while (temp != NULL) { // Step 1.2., Step
1.2.3.
cout << temp -> getData () << " "; // Step 1.2.1
temp = temp -> getNext (); // Step 1.2.2.
}
}
cout<<endl;
}
bool search (int key) {
return searchData (key) != NULL;
}
bool searchbefore(int key) {
return searchData(key) != NULL;
}

Node* searchData (int key) {


/*
Steps
1. if the list is not empty
1.1. Create a pointer named temp and store the address of node head is
referring to
1.2. Check if temp is not NULL and data part of temp should not be
equals to key
1.2.1. move temp to the next part
1.2.2. Repeat 1.2.
1.3. return temp
2. if the list is empty
2.1. return NULL
*/

if (!isEmpty ()) { // Step 1.


Node* temp = getHead (); // Step 1.1.
while (temp != NULL && temp -> getData () != key) { // Step 1.2., Step 1.2.2.
temp = temp -> getNext (); // Step 1.2.1.
}
return temp; // Step 1.3.
}
else { // Step 2.
return NULL; // Step 2.1.
}
}

bool updateData(int key, int data){


/*
steps
1.if the list is not empty
1.1. search the node where data would be changed and store it in a temp
node
1.2. if the temp node is not null
1.2.1. set data to the temp node in data part
1.2.2.when data is successfully change return true
1.3. if the temp node is null then return false
2. if the list is empty
2.1. print link is empty.
*/

if (!isEmpty ()){ //step 1


Node* temp = searchData(key); //step 1.1
if (temp != NULL) { //step 1.2
temp->setData(data); //step 1.2.1
return true; //step 1.2.2
}
return false; //step 1.3
}
else { //step 2
cout<<"linked list is empty"; //step 2.1
}
}

void insertUnique(int data) {


/*
steps
1. search the node where data would be changed and store it in a temp
node
1.2. if the temp node is null
1.2.1. add data on the head.
1.3.if the temp node is not null
1.3.1. data is not unique
*/
Node* temp = searchData(data); //step
1
if(temp==NULL)
//step 1.2
{
addOnHead(data);
//step 1.2.1
}
else {
//step 1.3
cout<<"data you entered is not unique"; //step
1.3.1
}
}

void insertAfter(int key, int data) {


/*
Steps
1. if the list is not empty
1.1 search the node where want to add data after thet node and
store it as current node
2. if the current node is not null
2.1. Create a New pointer of Node type and store the reference of
the new node with data in it
2.2. Next part of New node point/refer to the node which current
node is referring to
2.3. Current node will start referring to the New node which is
created now
3. if the current node is null
3.1. print there is no specific node you want to insert after
*/
if(!isEmpty()){ //step
1
Node* current = searchData(key); //step 1.1
if (current != NULL) { //step
2.
Node* NewNode = new Node(data); //step 2.1
NewNode->setNext(current->getNext()); //step 2.2
current->setNext(NewNode); //step 2.3
}
else {
//step 3
cout<<"there is no specific node you want to insert after "<<endl;
//step 3.1
}
}
}

Node* searchDataBefore(int key) {


/*
Steps
1. if the list is not empty OR key is not equal to head data
1.1. Create a pointer named previous equal to null
1.2. Create a pointer named temp and store the address of node
head is referring to
1.3. Check if temp is not NULL and data part of temp should not
be equals to key
1.3.1. make previous is equal to current node
1.3.2. move temp to the next part
1.3.3. Repeat 1.3
1.4. if the next part of previous node is not null
1.4.1. return previous
1.5. if the next part of previous node is null
1.5.1. return null
2. if the list is empty OR key is equal to head data
2.1. return NULL
*/

if (!isEmpty() || getHead()->getData() != key) { //step


1
Node* previous = NULL;
//step 1.1
Node* current = getHead();
//step 1.2
while (current != NULL && current->getData() != key) { //step
1.3 and 1.3.3
previous = current;
//step 1.3.1
current = current->getNext();
//step 1.3.2
}
if(previous->getNext()!=NULL){
//step 1.4
return previous;}
//step 1.4.1
else
//step 1.5
return NULL;
//step 1.5.1
}
else {
//step 2
return NULL;
//step 2.1
}
}

void insertBefore(int key, int data) {


/*
Steps
1. if the list is not empty
1.1 search the node where want to add data before thet node and
store it as previous node
2. if the key is equal to head
2.1 add value on head
3. if the current node is not null
3.1. Create a New pointer of Node type and store the reference of
the new node with data in it
3.2. Next part of New node point/refer to the node which current
node is referring to
3.3. Current node will start referring to the New node which is
created now
4. if the current node is null
4.1. print there is no specific node you want to insert before
*/

if (!isEmpty()){ //step 1
Node* prev = searchDataBefore(key); //step 1.1
if (getHead()->getData() == key) { //step 2
addOnHead(data); //step 2.1
}
if (prev != NULL) { //step 3
Node* newNode = new Node(data); //step 3.1
newNode->setNext(prev->getNext()); //step 3.2
prev->setNext(newNode); //step 3.3
}
else {
//step 4
cout<<"there is no specific node you want to insert before "<<endl;
//step 4.1
}
}
}
bool removeData(int key) {
if (!isEmpty()) {
Node* temp = getHead();
Node* prev = NULL;
while (temp != NULL && temp->getData() != key) {
prev = temp;
temp = temp->getNext();
}
if (temp != NULL) {
if (prev == NULL) {
setHead(temp->getNext());
} else {
prev->setNext(temp->getNext());
}
delete temp;
return true;
}
}
return false;
}
};

#define SIZE 10

class Hashing {
public:
Hashing() {
for (int index = 0; index < SIZE; index += 1) {
array[index] = new LinkedList();
}
}

void insertData(int data) {


int index = hashFunction(data);
array[index]->addOnHead(data);
}

int searchData(int key) {


int index = hashFunction(key);
Node* node = array[index]->searchData(key);
if (node != NULL) {
return index;
} else {
return -1;
}
}

bool removeData(int key) {


int index = searchData(key);
if (index > -1) {
return array[index]->removeData(key);
} else {
return false;
}
}

void updateData(int key, int data) {


int index = searchData(key);
if (index > -1){
removeData(key) ;
insertData(data);
}
}

void printHashTable() {
for (int index = 0; index < SIZE; index += 1) {
cout << "Index " << index << ": ";
array[index]->printList();
}
}

private:
int hashFunction(int key) {
return ((key + 3) * 5) % SIZE;
}

LinkedList* array[SIZE];
};

int main() {
Hashing obj;
obj.insertData(2);
obj.insertData(3);
obj.insertData(4);
obj.insertData(5);
obj.insertData(6);
obj.insertData(32);
obj.insertData(12);
obj.insertData(22);
obj.updateData(5, 33);
obj.removeData(12);
obj.printHashTable();
cout << "Value index: " << obj.searchData(4) << endl;
obj.removeData(4);
cout << "Value index: " << obj.searchData(4) << endl;
return 0;
}

You might also like