adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C and C++ to insert node at the beginning of Singly linked List

Program in C and C++ to insert node at the beginning of Singly linked List

C Program
#include<stdio.h>
#include<stdlib.h>
     struct Node {
         int data;
         struct Node *next;
     };
    typedef struct Node Node;
    Node *head = NULL, *tail = NULL;
    void insert(int data){
        Node *newN;
        newN = (Node*)malloc(sizeof(Node));
        newN->data = data;
        newN->next = NULL;
        if(head == NULL){
            head = tail = newN;
        }
        else{
           tail->next = newN;
            tail = newN;
        }
    }
    void show(){
        Node *temp;
        if(head == 0){
           printf("List is empty.\n");
        }
        else{
            temp = head;
            while(temp != 0){
                printf("%4d",temp->data);
                temp = temp->next;
            }
        }
     }
    void insertBeg(int data){
        Node *newN;
        newN = (Node*)malloc(sizeof(Node));
        newN->data = data;
        newN->next = head;
        head = newN;
    }
    int main(){
      Node l1;
      insert(19);
      insert(56);
      insert(13);
      printf("Elements of Linked list is ");
      show();
      insertBeg(90); // call insertBeg function to insert Node at Beginning
      printf("\nElements of Linked list is After insert Node at beginning ");
      show();
    return 0;
 }
C++ Program
#include<iostream>
 using namespace std;
 class List {
        struct Node{
              int data;
              Node *next;
              //constructors
              Node(){
                  data = 0;
                  next = 0;
              }
              Node(int data){
                  this->data = data;
                  next = 0;
              }
              //Destructor
              ~Node(){
                  delete next;
              }
        };
        Node *head, *tail;
        public:
    	  int size;
        //Constructors of List class
          List(){
           size = 0;
           head = NULL;
		   tail = NULL;
         }
         ~List(){
           delete head, tail;
        }
        void insert(int data){
            Node *newN;
            newN = new Node(data);
            if(head == NULL){
                head = tail = newN;
            }
            else{
                tail->next = newN;
                tail = newN;
            }
            size++;
        }
        void show(){
           Node *temp;
           if(head == 0){
               cout<<"List is empty.\n";
           }
           else{
               temp = head;
               while(temp != 0){
                   cout<<"  "<<temp->data;
                   temp = temp->next;
               }
           }
        }
        //Insert Node at beginning of SLL
        void insertBeg(int data){
            Node *newN = new Node(data);
            newN->next = head;
            head = newN;
        }
 };
 int main(){
     List l1;
     l1.insert(30);
     l1.insert(40);
     cout<<"Elements of List is ";
     l1.show();
     l1.insertBeg(10); //insertBeg function call
     cout<<endl<<"Elements of List is After insert Node at beginning ";
     l1.show();
     return 0;
 }
Output :-






Post a Comment

0 Comments