Implement insertion of node in the beginning of the list in singly linked list in C++
Code
#include <iostream>using namespace std;class Node{public:int data;Node *next;};void insertbeg(Node **head, int data){Node *newnode = new Node();newnode->data = data;newnode->next = *head;*head = newnode;}void printlist(Node *head){while (head != NULL){cout << head->data << " ";head = head->next;}}int main(){insertbeg(10);insertbeg(20);insertbeg(30);insertbeg(40);insertbeg(50);display();return 0;}
Output
50 40 30 20 10
0 Comments
Post a Comment