Skip to content

Instantly share code, notes, and snippets.

@chankruze
Created June 2, 2019 18:26
Show Gist options
  • Save chankruze/ad22192e964a5afb12efcbaf836f47b6 to your computer and use it in GitHub Desktop.
Save chankruze/ad22192e964a5afb12efcbaf836f47b6 to your computer and use it in GitHub Desktop.
Linked List Implementation
// Linked List: Insert a node at beginning
/*
* @Author: chankruze (Chandan Kumar Mandal)
* @Date: 2019-06-02 21:40:46
* @Last Modified by: chankruze (Chandan Kumar Mandal)
* @Last Modified time: 2019-06-02 23:54:36
*/
#include <stdlib.h>
#include <stdio.h>
typedef struct Node
{
int data;
struct Node *next;
} Node;
void Insert(Node **head, int x)
{
Node *temp = (Node *)malloc(sizeof(struct Node));
temp->data = x;
temp->next = *head;
*head = temp;
}
void Print(Node *head)
{
printf("List is : ");
while (head != NULL)
{
printf(" %d", head->data);
head = head->next;
}
printf("\n");
}
int main()
{
Node *head = NULL; //EMPTY LIST
printf("How many numbers to add ?\n");
int n, i, x;
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter the number\n");
scanf("%d", &x);
Insert(&head, x);
Print(head);
}
printf("\nYeh ! I learned inserting node at the beginning of a linked list ...\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment