Datastrucure

You might also like

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

1.

// Write a C /C++ program to implement linked list and also print all the
elements of the same

#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
};
void LinkListTraversal(struct Node *ptr)
{
while (ptr != NULL)
{
cout << "Element:" << ptr->data << endl;
ptr = ptr->next;
}
}
struct Node *CreateNode(int data)
{
struct Node *ptr = (struct Node *)malloc(sizeof(struct Node));
ptr->data = data;
return ptr;
}
int main()
{
struct Node *head = CreateNode(9);
struct Node *second = CreateNode(11);
struct Node *third = CreateNode(34);
struct Node *fourth = CreateNode(50);
struct Node *fifth = CreateNode(76);
head->next = second;
second->next = third;
third->next = fourth;
fourth->next = fifth;
fifth->next = NULL;

LinkListTraversal(head);
}
OUTPUT-:Element:9
Element:11
Element:34
Element:50
Element:76
// 2. Write a C/C++ program to perform insertion at the beginning of a linked
list
// Write a C /C++ program to implement linked list and also print all the
elements of the same
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
};
void LinkListTraversal(struct Node *ptr)
{
while (ptr != NULL)
{
cout << "Element:" << ptr->data << endl;
ptr = ptr->next;
}
}
struct Node *CreateNode(int data)
{
struct Node *ptr = (struct Node *)malloc(sizeof(struct Node));
ptr->data = data;
return ptr;
}
struct Node *Insert_at_First(struct Node *head, int data)
{
struct Node *newNode = CreateNode(data);
newNode->next = head;
head = newNode;
return head;
}
int main()
{
struct Node *head = CreateNode(12);
struct Node *second = CreateNode(29);
struct Node *third = CreateNode(34);
struct Node *fourth = CreateNode(50);
struct Node *fifth = CreateNode(76);
head->next = second;
second->next = third;
third->next = fourth;
fourth->next = fifth;
fifth->next = NULL;
cout << "Before Insertion:\n";
LinkListTraversal(head);
cout << "\nAfter Insertion at First Position\n";
head = Insert_at_First(head, 8);
LinkListTraversal(head);
}

OUTPUT-:Before Insertion:
Element:12
Element:29
Element:34
Element:50
Element:76

After Insertion at First Position


Element:8
Element:12
Element:29
Element:34
Element:50
Element:76
// write a C /C++ program to perform insertion at a specific position in a
created linked list.
// Write a C /C++ program to implement linked list and also print all the
elements of the same
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
};
void LinkListTraversal(struct Node *ptr)
{
while (ptr != NULL)
{
cout << "Element:" << ptr->data << endl;
ptr = ptr->next;
}
}
struct Node *CreateNode(int data)
{
struct Node *ptr = (struct Node *)malloc(sizeof(struct Node));
ptr->data = data;
return ptr;
}
struct Node *Insert_at_Position(struct Node *head, int pos, int data)
{
struct Node *newNode = CreateNode(data);
struct Node *ptr = head;
for (int i = 1; i < pos; i++)
{
ptr = ptr->next;
}
newNode->next = ptr->next;
ptr->next = newNode;
return head;
}
int main()
{
struct Node *head = CreateNode(9);
struct Node *second = CreateNode(11);
struct Node *third = CreateNode(34);
struct Node *fourth = CreateNode(50);
struct Node *fifth = CreateNode(76);
head->next = second;
second->next = third;
third->next = fourth;
fourth->next = fifth;
fifth->next = NULL;
cout << "Before Insertion\n";
LinkListTraversal(head);
head = Insert_at_Position(head, 3, 45);
cout << "After Insertion\n";
LinkListTraversal(head);
return 0;
}
OUTPUT-Before Insertion
Element:9
Element:11
Element:34
Element:50
Element:76
After Insertion
Element:9
Element:11
Element:34
Element:45
Element:50
Element:76

You might also like