0% found this document useful (0 votes)
17 views6 pages

Data Assignment 1

The document provides a comprehensive overview of various operations that can be performed on a linked list in C++. It includes code snippets for traversing the list, inserting nodes at the beginning, specific indices, and the end, as well as deleting nodes from the beginning, specific indices, and the end. Each operation is explained with corresponding code examples to illustrate the implementation.

Uploaded by

Annaya Ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Data Assignment 1

The document provides a comprehensive overview of various operations that can be performed on a linked list in C++. It includes code snippets for traversing the list, inserting nodes at the beginning, specific indices, and the end, as well as deleting nodes from the beginning, specific indices, and the end. Each operation is explained with corresponding code examples to illustrate the implementation.

Uploaded by

Annaya Ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Different Operations in Linked list

1-Traversal
// A simple C++ program for traversal of a linked list
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* next;
};
// This function prints contents of linked list
// starting from the given node
void printList(Node* n)
{
while (n != NULL) {
cout << n->data << " ";
n = n->next;
}
}

// Driver code
int main()
{
Node* head = NULL;
Node* second = NULL;
Node* third = NULL;
// allocate 3 nodes in the heap
head = new Node();
second = new Node();
third = new Node();

head->data = 1; // assign data in first node


head->next = second; // Link first node with second

second->data = 2; // assign data to second node


second->next = third;

third->data = 3; // assign data to third node


third->next = NULL;
printList(head);
return 0;}

2-insert node at the beginning


void push(Node** head_ref, int new_data)
{
/* 1. allocate node */
Node* new_node = new Node();
/* 2. put in the data */
new_node->data = new_data;

/* 3. Make next of new node as head */


new_node->next = (*head_ref);

/* 4. move the head to point to the new node */


(*head_ref) = new_node;
}

3-insert node at the specific index


void insertAfter(Node* prev_node, int new_data)
{
/*1. check if the given prev_node is NULL */
if (prev_node == NULL)
{
cout<<"The given previous node cannot be NULL";
return;
}

/* 2. allocate new node */


Node* new_node = new Node();

/* 3. put in the data */


new_node->data = new_data;

/* 4. Make next of new node as next of prev_node */


new_node->next = prev_node->next;

/* 5. move the next of prev_node as new_node */


prev_node->next = new_node;
}

4-insert node at the end


* 1. allocate node */
Node* new_node = new Node();

Node *last = *head_ref; /* used in step 5*/

/* 2. put in the data */


new_node->data = new_data;

/* 3. This new node is going to be


the last node, so make next of
it as NULL*/
new_node->next = NULL;

/* 4. If the Linked List is empty,


then make the new node as head */
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}

/* 5. Else traverse till the last node */


while (last->next != NULL)
{
last = last->next;
}

/* 6. Change the next of last node */


last->next = new_node;
return;

5-delete node at the beginning


oid pop_front() {
if(head != NULL) {

//1. if head is not null, create a


// temp node pointing to head
Node* temp = head;

//2. move head to next of head


head = head->next;

//3. delete temp node


free(temp);
}
}

6-delete node at the specific index


void pop_at(int position) {

//1. check if the position is > 0


if(position < 1) {
cout<<"\nposition should be >= 1.";
} else if (position == 1 && head != NULL) {

//2. if the position is 1 and head is not null, make


// head next as head and delete previous head
Node* nodeToDelete = head;
head = head->next;
free(nodeToDelete);
} else {

//3. Else, make a temp node and traverse to the


// node previous to the position
Node* temp = head;
for(int i = 1; i < position-1; i++) {
if(temp != NULL) {
temp = temp->next;
}
}

//4. If the previous node and next of the previous


// is not null, adjust links
if(temp != NULL && temp->next != NULL) {
Node* nodeToDelete = temp->next;
temp->next = temp->next->next;
free(nodeToDelete);
} else {

//5. Else the given node will be empty.


cout<<"\nThe node is already null.";
}
}
}

7-delete node at the end


void pop_back() {
if(head != NULL) {

//1. if head in not null and next of head


// is null, release the head
if(head->next == NULL) {
head = NULL;
} else {

//2. Else, traverse to the second last


// element of the list
Node* temp = head;
while(temp->next->next != NULL)
temp = temp->next;

//3. Change the next of the second


// last node to null and delete the
// last node
Node* lastNode = temp->next;
temp->next = NULL;
free(lastNode);
}
}
}

You might also like