Skip to content

Instantly share code, notes, and snippets.

@devil-cyber
Created October 5, 2021 17:29
Show Gist options
  • Save devil-cyber/b00dbb1d1f67cb05cf2cb767344ea2af to your computer and use it in GitHub Desktop.
Save devil-cyber/b00dbb1d1f67cb05cf2cb767344ea2af to your computer and use it in GitHub Desktop.
LinkedList implementation in C
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *next;
};
struct Node *NewNode(int data){
struct Node *node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->next = NULL;
return node;
};
struct Node *head = NULL;
void push(int data){
struct Node *node;
if(head==NULL){
head = NewNode(data);
}
else{
node = NewNode(data);
node->next = head;
head = node;
}
}
void print()
{
struct Node *temp = head;
while(temp!=NULL){
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
void reverse(){
struct Node *prev, *temp, *next=NULL;
prev = NULL;
temp = head;
while(temp!=NULL){
next = temp->next;
temp->next = prev;
prev = temp;
temp = next;
}
if(prev!=NULL)
head = prev;
}
int main()
{
push(10);
push(20);
push(30);
push(40);
reverse();
print();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment