Created
October 5, 2021 17:29
-
-
Save devil-cyber/b00dbb1d1f67cb05cf2cb767344ea2af to your computer and use it in GitHub Desktop.
LinkedList implementation in C
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<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