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

Lab 5 Dsa

Uploaded by

anilpython7877
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)
9 views

Lab 5 Dsa

Uploaded by

anilpython7877
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
You are on page 1/ 8

#include <iostream>

using namespace std;

struct Node {
int data;
Node* next;
};

Node* createLL() {
Node* head = nullptr;
Node* ptr = nullptr;
int numNodes, data;

cout << "Enter the number of nodes: ";


cin >> numNodes;

for (int i = 0; i < numNodes; i++) {


cout << "Enter data for node " << i + 1 << ": ";
cin >> data;

Node* newNode = new Node();


newNode->data = data;
newNode->next = nullptr;

if (head == nullptr) {
head = newNode;
} else {
ptr = head;
while (ptr->next != nullptr) {
ptr = ptr->next;
}
ptr->next = newNode;
}
}

return head;
}

void display(Node* head) {


if (head == nullptr) {
cout << "The list is empty." << endl;
return;
}

Node* ptr = head;


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

int main() {
Node* head = createLL();

cout << "Linked list after insertion: " << endl;


display(head);

return 0;
}
#include <iostream>
using namespace std;

struct Node
{
int data;
Node *next;

Node(int val)
{
data = val;
next = NULL;
}
};

class Linked_List
{
Node *head;

public:
Linked_List()
{
head = NULL;
}
int odd_sum = 0;

void insert_at_End(int val)


{

for (int i = 0; i < val; i++)


{
cout << "Enter the value :";
int j;
cin >> j;
Node *newNode = new Node(j);
if (head == NULL)
{
head = newNode;
}
else
{
Node *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
if ((i+1) % 2 != 0){
odd_sum = odd_sum + j;
}
}
}

void display()
{
Node *temp = head;
while (temp != NULL)
{
cout << temp->data << "->";
temp = temp->next;
}
cout << "Null";
}

void oddsumprint(){
cout<<endl<<"odd sum is : "<<odd_sum;
}
};
int main()
{
Linked_List list;
cout << "Enter the no. of node : ";
int n;
cin >> n;

list.insert_at_End(n);

cout << "Linked List: ";


list.display();
list.oddsumprint();

return 0;
}

#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
};

Node* createLL() {
Node* head = nullptr;
Node* ptr = nullptr;
int numNodes, data;

cout << "Enter the number of nodes: ";


cin >> numNodes;

for (int i = 0; i < numNodes; i++) {


cout << "Enter data for node " << i + 1 << ": ";
cin >> data;

Node* newNode = new Node();


newNode->data = data;
newNode->next = nullptr;

if (head == nullptr) {
head = newNode;
} else {
ptr = head;
while (ptr->next != nullptr) {
ptr = ptr->next;
}
ptr->next = newNode;
}
}

return head;
}

void reverseData(Node* head) {


if (head == nullptr) {
return;
}

int count = 0;
Node* ptr = head;
while (ptr != nullptr) {
count++;
ptr = ptr->next;
}

int* dataArr = new int[count];


ptr = head;

for (int i = 0; i < count; i++) {


dataArr[i] = ptr->data;
ptr = ptr->next;
}

ptr = head;
for (int i = count - 1; i >= 0; i--) {
ptr->data = dataArr[i];
ptr = ptr->next;
}

delete[] dataArr;
}

void display(Node* head) {


if (head == nullptr) {
cout << "The list is empty." << endl;
return;
}

Node* ptr = head;


while (ptr != nullptr) {
cout << ptr->data << " -> ";
ptr = ptr->next;
}
cout << "NULL" << endl;
}
int main() {
Node* head = createLL();

cout << "Original linked list: " << endl;


display(head);

reverseData(head);

cout << "Linked list after reversing the data: " << endl;
display(head);

return 0;
}

You might also like