Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- struct Node{
- int val;
- Node * next;
- };
- Node * insertAtHead(int data,Node* head){
- Node * a = new Node();
- a->val = data;
- a->next = NULL;
- if(head==NULL){
- head= a;
- }
- else{
- a->next = head;
- head= a;
- }
- return head;
- }
- void printList(Node * head){
- while(head!=NULL){
- cout<<head->val<<" ";
- head= head->next;
- }
- return;
- }
- int main(){
- int n;
- cout<<"How many values you want in list : "<<endl;
- cin>>n;
- Node *head = NULL;
- //insertAtHead
- //5
- //6->5
- //9->6->5
- for(int i = 0; i<n ; i++){
- int data;
- cout<<"Enter an integer : "<<endl;
- cin>>data;
- head = insertAtHead(data,head);
- }
- printList(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement