Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
Closed
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
210 changes: 210 additions & 0 deletions data structures/linked list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
#include <stdio.h>
#include <stdlib.h>

struct SLLNode{
int data;
struct SLLNode *next;
};

struct DLLNode{
int data;
struct DLLNode *prev;
struct DLLNode *next;
};

typedef struct DLLNode DLL_NODE;
typedef struct SLLNode SLL_NODE;

SLL_NODE* makeNewSLLNode(int data){
SLL_NODE *newSLLNode = (SLL_NODE *) calloc(1, sizeof(SLL_NODE));
newSLLNode->data = data;
return newSLLNode;
}

void SLLInsert(SLL_NODE **head, int data, int position){
SLL_NODE *current = *head;
int k = 1;

SLL_NODE *newSLLNode = makeNewSLLNode(data);
if(position == 1){
newSLLNode->next = current;
(*head) = newSLLNode;
return;
}

while((k < position - 1) && (current->next !=NULL)){
current = current->next;
k++;
}

newSLLNode->next = current->next;
current->next = newSLLNode;

}

void SLLDelete(SLL_NODE **head, int position){
SLL_NODE *temp = *head;
int k = 1;

SLL_NODE *temp2;

if(position == 1){
(*head) = (*head)->next;
free(temp);
return;
}

while((k < position) && (temp != NULL)){
temp2 = temp;
temp = temp->next;
k++;
}

temp2->next = temp->next;
free(temp);

}

void deleteSingleLinkedList(SLL_NODE **head){
SLL_NODE *current = *head;
while(current != NULL){
(*head) = (*head)->next;
free(current);
current = *head;
}
}

void displaySLL(SLL_NODE *head){
SLL_NODE* curr = head;
printf("\nPrinting the list : ");
while(curr != NULL){
printf("%d -> ", curr->data);
curr = curr->next;
}
}

DLL_NODE* makeNewDLLNode(int data){
DLL_NODE *newNode = (DLL_NODE*) calloc(1, sizeof(DLL_NODE*));
newNode->data = data;
return newNode;
}

void DLLInsert(DLL_NODE **head, int data , int position){
DLL_NODE* newNode = makeNewDLLNode(data);
DLL_NODE* temp = *head;

int k = 1;
if(position == 1){
newNode->prev = NULL;
newNode->next = *head;
*head = newNode;
return;
}

while((k < position - 1) && (temp->next != NULL)){
temp = temp->next;
k++;
}

newNode->next = temp->next;
newNode->prev = temp;
if(temp->next){
temp->next->prev = newNode;
}

temp->next = newNode;

}

void DLLDelete(DLL_NODE **head, int position){
DLL_NODE *temp = *head;
DLL_NODE *temp2;

int k = 1;

if(*head == NULL){
printf("Empty List");
return;
}

if(position == 1){
temp = *head;
(*head) = (*head)->next;
free(temp);
return;
}

while((k < position) && (temp->next != NULL)){
temp = temp->next;
k++;
}

temp2 = temp->prev;
temp->next->prev = temp2;
temp2->next = temp->next;
free(temp);
return;

}

void displayDLL(DLL_NODE* head){
DLL_NODE* current = head;
printf("\n");
while(current != NULL){
printf("%d -> ", current->data);
current = current->next;
}
}

void singlyLinkedList() {
SLL_NODE *head = NULL;
SLLInsert(&head, 7, 1);
SLLInsert(&head, 6, 1);
SLLInsert(&head, 5, 1);
SLLInsert(&head, 4, 1);
SLLInsert(&head, 3, 1);
SLLInsert(&head, 1, 1);
SLLInsert(&head, 2, 2);
displaySLL(head);

SLLDelete(&head, 4);
displaySLL(head);
}

void doublyLinkedList() {
DLL_NODE* head = NULL;
DLLInsert(&head, 7, 1);
DLLInsert(&head, 6, 1);
DLLInsert(&head, 4, 1);
DLLInsert(&head, 5, 2);
DLLInsert(&head, 1, 3);
DLLInsert(&head, 2, 4);
DLLInsert(&head, 3, 5);
displayDLL(head);
DLLDelete(&head, 3);
displayDLL(head);
}

int main()
{
int a;

printf("Choose the type of list: ");
printf("\n1. Singly linked list\n2. Doubly linked list\n");
printf("Your choice: ");
scanf("%d", &a);
switch (a)
{
case 1:
singlyLinkedList();
break;
case 2:
doublyLinkedList();
break;
default:
printf("Invalid choice !!!");
break;
}

return 0;
}
68 changes: 68 additions & 0 deletions data structures/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

// C program for linked list implementation of stack
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

// A structure to represent a stack
struct StackNode
{
int data;
struct StackNode* next;
};

struct StackNode* newNode(int data)
{
struct StackNode* stackNode =
(struct StackNode*) malloc(sizeof(struct StackNode));
stackNode->data = data;
stackNode->next = NULL;
return stackNode;
}

int isEmpty(struct StackNode *root)
{
return !root;
}

void push(struct StackNode** root, int data)
{
struct StackNode* stackNode = newNode(data);
stackNode->next = *root;
*root = stackNode;
printf("%d pushed to stack\n", data);
}

int pop(struct StackNode** root)
{
if (isEmpty(*root))
return INT_MIN;
struct StackNode* temp = *root;
*root = (*root)->next;
int popped = temp->data;
free(temp);

return popped;
}

int peek(struct StackNode* root)
{
if (isEmpty(root))
return INT_MIN;
return root->data;
}

int main()
{
struct StackNode* root = NULL;

push(&root, 10);
push(&root, 20);
push(&root, 30);

printf("%d popped from stack\n", pop(&root));

printf("Top element is %d\n", peek(root));

return 0;
}