"linked list in c++ using class insert delete display in array" Code Answer's
You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "linked list in c++ using class insert delete display in array" answered properly. Developers are finding an appropriate answer about linked list in c++ using class insert delete display in array related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like linked list in c++ using class insert delete display in array. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on linked list in c++ using class insert delete display in array.
linked list in c++ using class insert delete display in array
#include <iostream>
using namespace std;
// A linked list node
struct Node
{
int data;
struct Node *next;
};
//insert a new node in front of the list
void push(struct Node** head, int node_data)
{
/* 1. create and allocate node */
struct Node* newNode = new Node;
/* 2. assign data to node */
newNode->data = node_data;
/* 3. set next of new node as head */
newNode->next = (*head);
/* 4. move the head to point to the new node */
(*head) = newNode;
}
//insert new node after a given node
void insertAfter(struct Node* prev_node, int node_data)
{
/*1. check if the given prev_node is NULL */
if (prev_node == NULL)
{
cout<<"the given previous node is required,cannot be NULL"; return; }
/* 2. create and allocate new node */
struct Node* newNode =new Node;
/* 3. assign data to the node */
newNode->data = node_data;
/* 4. Make next of new node as next of prev_node */
newNode->next = prev_node->next;
/* 5. move the next of prev_node as new_node */
prev_node->next = newNode;
}
/* insert new node at the end of the linked list */
void append(struct Node** head, int node_data)
{
/* 1. create and allocate node */
struct Node* newNode = new Node;
struct Node *last = *head; /* used in step 5*/
/* 2. assign data to the node */
newNode->data = node_data;
/* 3. set next pointer of new node to null as its the last node*/
newNode->next = NULL;
/* 4. if list is empty, new node becomes first node */
if (*head == NULL)
{
*head = newNode;
return;
}
/* 5. Else traverse till the last node */
while (last->next != NULL)
last = last->next;
/* 6. Change the next of last node */
last->next = newNode;
return;
}
// display linked list contents
void displayList(struct Node *node)
{
//traverse the list to display each node
while (node != NULL)
{
cout<<node->data<<"-->";
node = node->next;
}
if(node== NULL)
cout<<"null";
}
/* main program for linked list*/
int main()
{
/* empty list */
struct Node* head = NULL;
// Insert 10.
append(&head, 10);
// Insert 20 at the beginning.
push(&head, 20);
// Insert 30 at the beginning.
push(&head, 30);
// Insert 40 at the end.
append(&head, 40); //
Insert 50, after 20.
insertAfter(head->next, 50);
cout<<"Final linked list: "<<endl;
displayList(head);
return 0;
}
Source: www.softwaretestinghelp.com
All those coders who are working on the C++ based application and are stuck on linked list in c++ using class insert delete display in array can get a collection of related answers to their query. Programmers need to enter their query on linked list in c++ using class insert delete display in array related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about linked list in c++ using class insert delete display in array for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of linked list in c++ using class insert delete display in array while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "linked list in c++ using class insert delete display in array". Visit this developer's friendly online web community, CodeProZone, and get your queries like linked list in c++ using class insert delete display in array resolved professionally and stay updated to the latest C++ updates.