You are on page 1of 13

LAB REPORT 4

NAME : AHTASHAM SAJAWAL


REG NO : 211356 BSSE A
INSTRUCTOR : SIR SAQIB RAZA
/
******************************************************************
************

HEADER FILE
******************************************************************
*************/

#include <iostream>
#include <string>

using namespace std;


class Node
{
public:
int data;
Node *next;
};

void insert(int x);


int getcount(Node * head);
bool search(Node* head, int x);
Node* reverse(Node* node);
int getNth(Node * head, int indx);
Node* newNode(int data);
Node* addTwoLists(Node* head, Node* second);
Node* Rev(Node* head);
void printList(Node* node);
void pushFront(Node** head_ref, int new_data);

/
******************************************************************
************

IMPLEMENTATION FILE
******************************************************************
*************/
Node *head;
void insert(int x)
{
Node *temp = new Node();
temp->data = x;
temp->next = head;
head = temp;
}

int getcount(Node * head)


{ int count=0;
Node* current=head;
while(current!= NULL)
{

current=current->next;
count++;
}
return count;

bool search(Node* head, int x)


{
Node* current = head;
while (current != NULL) {
if (current->data == x)

cout<<"\nSearch Data exists \n";

current = current->next;
return true;
}
else
cout<<"\n Search Data do not exists \n";

return false;
}
}

Node* reverse(Node* node)


{
if (node == NULL)

return NULL;
if (node->next == NULL) {

head = node;

return node;
}
Node* node1 = reverse(node->next);

node1->next = node;
node->next = NULL;

return node;
}

int getNth(Node * head, int indx)


{

Node* current = head;

int count = 0;
while (current != NULL) {
if (count == indx)

return (current->data);

count++;

current = current->next;
}
}
Node* newNode(int data)
{
Node* new_node = new Node();

new_node->data = data;

new_node->next = NULL;

return new_node;
}

Node* addTwoLists(Node* head, Node* second)


{
Node* res = NULL;
Node *temp, *prev = NULL;
int carry = 0, sum;

while (head != NULL || second != NULL) {

sum = carry + (head ? head->data : 0) + (second ? second->data : 0);


carry = (sum >= 10) ? 1 : 0;
sum = sum % 10;
temp = newNode(sum);
if (res == NULL)
res = temp;
else
prev->next = temp;

prev = temp;

if (head)
head = head->next;
if (second)
second = second->next;
}
if (carry > 0)
temp->next = newNode(carry);
return res;
}

Node* Rev(Node* head)


{
if (head == NULL || head->next == NULL)
return head;
Node* rest = Rev(head->next);
head->next->next = head;
head->next = NULL;
return rest;
}

void printList(Node* node)


{
while (node != NULL) {
cout << node->data << " ";
node = node->next;
}
cout << endl;
}

void pushFront(Node** head_ref, int new_data)


{
Node* new_node = new Node();

new_node->data = new_data;

new_node->next = (*head_ref);

(*head_ref) = new_node;
}
/
******************************************************************
************

DRIVER CPP FILE


******************************************************************
*************/
#include"Node.h"
#include"List.cpp"

#include <bits/stdc++.h>
using namespace std;

int main()
{

Node list;
Node* res = NULL;
Node* second = NULL;

head = NULL;
int Size,size, number;
cout<<"What is the size of 1st list: ";
cin>>size;
for(int i = 1; i<=size; i++)
{
cout<<"\nEnter Number: ";
cin>>number;
insert(number);
cout<<"First Linked List = ";

printList(head);
}

search(head, 11);
cout << "COUNT OF NODES = " << getcount(head)<<endl<<endl;
cout << "Data at index 1 = " << getNth(head, 1)<<endl<<endl;
cout << "\nReversed Linked list = ";
reverse(head);
printList(head);

pushFront(&second, 10);
pushFront(&second, 6);
pushFront(&second, 7);
cout<<"Second Linked List is = ";

printList(second);
head = Rev(head);
second = Rev(second);
res = addTwoLists(head, second);
res = Rev(res);
cout << "Resultant list is = ";
printList(res);

return 0;
}

/
******************************************************************
************

OUTPUT
******************************************************************
*************/

You might also like