Skip to content

Instantly share code, notes, and snippets.

@MuhammadJahidHasan
Created December 30, 2020 09:34
Show Gist options
  • Save MuhammadJahidHasan/38477a2ab9c94c845052a3e41a3d1ff3 to your computer and use it in GitHub Desktop.
Save MuhammadJahidHasan/38477a2ab9c94c845052a3e41a3d1ff3 to your computer and use it in GitHub Desktop.
Linked List Implementation with c
#include <bits/stdc++.h>
using namespace std;
typedef struct node Node;
struct node{
int data;
Node *next;
};
Node *create_node(int item, Node *next){
Node *new_node=(Node*) malloc(sizeof(Node));
if(new_node==NULL){
printf("errors");
exit(1);
}
new_node->data=item;
new_node->next=next;
return new_node;
}
Node *remove_node(Node *head, Node *node){
if(node==head){
head=node->next;
free(node);
return head;
}
Node *current_node=head;
while(current_node!=NULL){
if(current_node->next==node){
break;
}
current_node=current_node->next;
}
if(current_node==NULL){return head;}
current_node->next=node->next;
free(node);
return head;
}
Node *prepend(Node *head, int item){
Node *new_node=create_node(item,head);
return new_node;
}
Node *append(Node *head, int item){
Node *new_node=create_node(item,NULL);
if(head==NULL){return new_node;}
Node *current_node=head;
while(current_node->next!=NULL){
current_node=current_node->next;
}
current_node->next=new_node;
return head;
}
void insertt(Node *node, int item){
Node *new_node=create_node(item,node->next);
node->next=new_node;
}
void print_Linked_List(Node *head){
Node *current_node=head;
while(current_node!=NULL){
printf("%d ",current_node->data);
current_node=current_node->next;
}
printf("\n");
}
int main()
{
Node *n,*head;
head=create_node(10,NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment