Skip to content

Commit

Permalink
Added implementation for LinkedList in C
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanjeet4567 committed Oct 31, 2024
1 parent 74d2e14 commit 1a24cd9
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions C/LinkedList.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <stdio.h>
#include<stdlib.h>
typedef struct Node LL;
struct Node{
int val;
LL* next;
};
LL* insertBegin(LL* head,int val){
LL* node =(LL*)malloc(sizeof(LL));
if(node==NULL){
printf("No memory\n");
return node;
}
node->val=val;
node->next=head;
return node;
}
LL* deleteBegin(LL * head){
if(head==NULL){
printf("No node present to delete\n");
return head;
}
LL* temp=head;
head=head->next;
free(temp);
return head;
}
LL* search(LL* head,int val){
if(head==NULL){
printf("No node present\n");
return head;
}
LL* temp = head;
while(temp->next!=NULL){
if(temp->val==val){
return temp;
}
temp=temp->next;
}
return NULL;
}
LL* insertAfter(LL* head,int s,int val){
if(head==NULL){
printf("No Node found\n");
return NULL;
}
LL* temp=search(head,s);
if(temp==NULL){
printf("NO Node found\n");
return head;
}
LL* node =(LL*)malloc(sizeof(LL));
if(node==NULL){
printf("No memory\n");
return node;
}
node->val=val;
node->next=temp->next;
temp->next=node;
return head;


}
void printLL(LL* head){
while(head!=NULL){
printf("%d -> ",head->val);
head=head->next;

}
printf("NULL\n");
}

LL* deleteAfter(LL* head,int val){
if(head==NULL){
printf("No Node found\n");
return NULL;
}
LL* temp=search(head,val);
if(temp==NULL){
printf("NO Node found\n");
return head;
}
LL* node=temp->next;
temp->next=node->next;
free(node);
return head;
}
int main(){
//testing
LL* head=NULL;
int arr[]={1,2,3,4,5};
for(int i=0;i<5;i++){
head=insertBegin(head,arr[i]);
}
head=insertAfter(head,4,6);
head=deleteAfter(head,3);
head=deleteBegin(head);
printLL(head);
}

0 comments on commit 1a24cd9

Please sign in to comment.