Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions Linkedlist/C/doubly_linked_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
struct Node* head;
struct Node* GetNode(int x){
struct Node* newNode= (struct Node*)malloc(sizeof(struct Node));
newNode->data = x;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
void InsertAtHead(int x){
struct Node* newNode = GetNode(x);
if(head==NULL){
head = newNode;
return ;
}
head->prev = newNode;
newNode->next = head;
head = newNode;

}
void InsertAtTail(int x){
struct Node* temp = head;
struct Node* newNode = GetNode(x);
if(head == NULL){
head = newNode;
return ;
}
while(temp->next != NULL) temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
void Print(){
struct Node* temp = head;
printf("Forward: ");
while(temp !=NULL){
printf("%d", temp->data);
temp = temp->next;
}
printf("\n");
}
void ReversePrint() {
struct Node* temp = head;
if(temp == NULL) return; // empty list, exit
// Going to last Node
while(temp->next != NULL) {
temp = temp->next;
}
// Traversing backward using prev pointer
printf("Reverse: ");
while(temp != NULL) {
printf("%d ",temp->data);
temp = temp->prev;
}
printf("\n");
}
int main(){
head = NULL;
InsertAtTail(2); Print(); ReversePrint();
InsertAtTail(4); Print(); ReversePrint();
InsertAtHead(6); Print(); ReversePrint();
InsertAtTail(8); Print(); ReversePrint();

}
32 changes: 32 additions & 0 deletions Linkedlist/C/linked_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node* next;
};
struct Node* head;
void Insertn(int x){
struct Node* temp= (struct Node*)malloc(sizeof(struct Node));
temp->data = x;
temp->next = head;
head = temp ;
return temp;
}
void print(){
struct Node* temp = head;
while(temp != NULL){
printf("%d", temp->data);
temp = temp->next;
}
printf("\n");
}
int main(){
head = NULL;
int i , n ,x;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&x);
Insertn(x);
}
print();
}