[go: up one dir, main page]

0% found this document useful (0 votes)
22 views14 pages

Question

Uploaded by

Samra Nawabi
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)
22 views14 pages

Question

Uploaded by

Samra Nawabi
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/ 14

Question #1:

Write a c++ code to perform insertion at start in linked list.


#include<iostream>
using namespace std;

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

void insertFront(Node** head, int data){

Node* new_node = new Node();

// assign data
new_node->data = data;

// change the next node of this newNode


// to current head of Linked List
new_node->next = *head;

// new_node should become the new head of Linked List


*head = new_node;

cout << "Inserted Item: " << new_node->data << endl;


}

void printList(Node* node){


cout << "\nLinked List : " ;

//as linked list will end when Node is Null


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

int main(){

Node* head = NULL;

insertFront(&head,4);
insertFront(&head,5);
insertFront(&head,6);
insertFront(&head,7);
insertFront(&head,8);
insertFront(&head,9);

printList(head);

return 0;
}

Output:
Question #2:
Write a code in c++ to perform insertion at end in linked list.
#include<iostream>
using namespace std;

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

void insertEnd(Node** head, int data)


{

Node* freshNode = new Node();

freshNode->data = data;
// since this will be the last node so it will point to NULL
freshNode->next = NULL;
//need this if there is no node present in linked list at all
if(*head==NULL)
{
*head = freshNode;
cout << freshNode->data << " inserted" << endl;
return;
}

struct Node* temp = *head;

// traverse to the last node of Linked List


while(temp->next!=NULL)
temp = temp->next;

// assign last node's next to this freshNode


temp->next = freshNode;
cout << freshNode->data << " inserted" << endl;
}

void display(Node* node)


{

//as linked list will end when Node is Null


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

int main()
{

Node* head = NULL;

insertEnd(&head,7);
insertEnd(&head,8);
insertEnd(&head,9);
insertEnd(&head,10);

display(head);
return 0;
}

Output:

Question #3:
Insertion at specific point:
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};

// function to create a new node in heap


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

// function to insert a new node at a specific position in the linked list


void insertAt(Node** head_ref, int new_data, int position) {
// create a new node
Node* new_node = newNode(new_data);

// if the linked list is empty, make the new node the head
if (*head_ref == NULL) {
*head_ref = new_node;
return;
}

// if the position is at the head of the linked list


if (position == 0) {
new_node->next = *head_ref;
*head_ref = new_node;
return;
}

// else, traverse to the specified position and insert the new node
Node* temp = *head_ref;
for (int i = 0; i < position - 1; i++) {
temp = temp->next;
}

new_node->next = temp->next;
temp->next = new_node;
}

// function to print the linked list


void printList(Node* node) {
while (node != NULL) {
cout << node->data << " ";
node = node->next;
}
}

int main() {
Node* head = NULL;

// insert node at specific position


insertAt(&head, 10, 0);
insertAt(&head, 20, 1);
insertAt(&head, 30, 2);
insertAt(&head, 40, 3);

// print the list


printList(head);

return 0;
}

Output:

Question # 4
Deletion at start
#include<iostream>
using namespace std;
class node{
public:
int data;
node*next;
node(int d){
data=d;
node*next=NULL;
}
};
void insertAtFirstNode (node*&head, int data){
node*n= new node(data);
n->next= head;
head=n;
}
void print(node*head){
while(head!=NULL){
cout<<head->data<<"->";
head=head->next;
}
cout<<endl;
}
void deleteAtFirst(node*&head){
if(head==NULL){
return;
}
node*temp=head;
head= head->next;
delete temp;
return;
}
int main(){
node*head= NULL;
insertAtFirstNode(head,1);
insertAtFirstNode(head,2);
insertAtFirstNode(head,3);
insertAtFirstNode(head,4);
deleteAtFirst(head);
print(head);
}
Question #5
Deletion at end
#include<iostream>
using namespace std;

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

void deleteEnd (Node ** head)


{
Node *tempNode = *head;
Node *previous;

// Can't delete from empty Linked List


if (*head == NULL)
{
cout << ("\nEmpty List, can't delete");
return;
}

// if Linked List has only 1 node


if (tempNode->next == NULL)
{
cout << "\nValue Deleted: " << (*head)->data;
*head = NULL;
return;
}

// else traverse to the last node


while (tempNode->next != NULL)
{
// store previous link node as we need to change its next val
previous = tempNode;
tempNode = tempNode->next;
}

// Curr assign 2nd last node's next to Null


previous->next = NULL;

cout << "\nValue Deleted: " << tempNode->data;


// delete the last node
//free (tempNode);
// 2nd last now becomes the last node
}
void insert (Node ** head, int data){
Node *newNode = new Node ();

newNode->data = data;
newNode->next = *head;

// assigned head to newNode


*head = newNode;
}

void display (Node * temp){


cout << "\nLinked List: ";

//as linked list will end when Node is Null


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

int main ()
{
Node *head = NULL;

insert (&head, 4);


insert (&head, 5);
insert (&head, 6);
insert (&head, 7);
insert (&head, 8);
insert (&head, 9);
insert (&head, 10);

display (head);
deleteEnd (&head);
deleteEnd (&head);
deleteEnd (&head);
display (head);

return 0;
}

Output:

You might also like