Skip to content

Instantly share code, notes, and snippets.

@tanaytoshniwal
Created August 18, 2019 20:37
Show Gist options
  • Save tanaytoshniwal/890b66fa11c8570e07cee2de04078ab3 to your computer and use it in GitHub Desktop.
Save tanaytoshniwal/890b66fa11c8570e07cee2de04078ab3 to your computer and use it in GitHub Desktop.
Linked List CPP
#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