Sunday, October 2, 2016

Link List - Insertion and Print inserted data

#include<bits/stdc++.h>  

using namespace std;

struct node
 {
  int data;
  int key;
  struct node *next;
 };

struct node *head = NULL;
struct node *current = NULL;

void InsertFirst(int key,int data)
 {
  struct node *link = (struct node*) malloc(sizeof(struct node));
  link->key=key;
  link->data=data;
 
  link->next = head;
  head = link;
 
 } 

void printList()
 { 

  struct node *ptr=head;
 
  while(ptr != NULL)
  {
   cout<<"("<<ptr->key<<","<<ptr->data<<")";
   ptr=ptr->next;
  }
   cout<<endl;
 } 


int main()
{
 int data,key;
 for(int i=0; i<4; i++)
 {
  cin>>data>>key;
  
  InsertFirst(data,key);
 }
 
 printList();
 
 return 0;  
}

No comments:

Post a Comment

Thank you for commenting. Please wait for response :)