Created
August 18, 2019 20:37
-
-
Save tanaytoshniwal/890b66fa11c8570e07cee2de04078ab3 to your computer and use it in GitHub Desktop.
Linked List CPP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
typedef struct Node Node; | |
struct Node{ | |
int data; | |
Node* next; | |
}; | |
Node* insert(Node* head, int data){ | |
Node *node = (struct Node*)malloc(sizeof(struct Node*)); | |
node->data = data; | |
node->next = NULL; | |
if(head == NULL){ | |
head = node; | |
return head; | |
} | |
Node* temp = head; | |
while(temp->next!=NULL){ | |
temp = temp->next; | |
} | |
temp->next = node; | |
return head; | |
} | |
void print(Node* head){ | |
if(head==NULL){ | |
return; | |
} | |
Node* temp = head; | |
while(temp != NULL){ | |
cout << temp->data << " "; | |
temp = temp->next; | |
} | |
} | |
int main(){ | |
Node *head = NULL; | |
int n; | |
cin >> n; | |
while(n--){ | |
int t; | |
cin >> t; | |
head = insert(head, t); | |
} | |
print(head); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment