DSA ADT Implementation
DSA ADT Implementation
dec_doubly_linkedlist.h
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
struct node
{
int data;
struct node *pre;
struct node *next;
};
def_doubly_linkedlist.c
#include "dec_doubly_linkedlist.h"
#include<stdio.h>
#include "dec_doubly_linkedlist.h"
int main()
{
struct node *head = NULL;
struct node *tail = NULL;
init(&head, &tail);
int element;
printf("ENTER ELEMENT : ");
scanf("%d", &element);
insertElement(&head, &tail, element, 1);
insertElement(&head, &tail, element+1, 1);
insertElement(&head, &tail, element+2, 1);
insertElement(&head, &tail, element+3, 1);
struct node *keyPtr = NULL;
int p = search(head, 10, &keyPtr);
if (p==0)
{
printf("NO\n");
}
else
{
printf("YES\n");
}
traverse(head, tail, 1);
int key;
printf("\nEnter Key : ");
scanf("%d", &key);
deleteNode(&head, &tail, key);
traverse(head, tail, 1);
destroy(&head, &tail);
printf("\n");
}
dec_cirularlinkedlist.h
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
def_circularlinkedlist.c
#include "dec_cirularlinkedlist.h"
}
free(newNode);
return 0;
}
prePtr = iter;
iter = iter->next;
} while (iter!=(*tail)->next);
return 0;
#include<stdio.h>
#include"dec_circularlinkedlist.h"
int main()
{
struct node *tail = NULL;
init(&tail);
insertAfterElement(&tail, 0, 7);
insertAfterElement(&tail, 7, 8);
insertAfterElement(&tail, 8, 9);
insertAfterElement(&tail, 7, 56);
struct node *keyPtr = NULL;
deleteElement(&tail, 8);
int p = searchElement(tail, 9, &keyPtr);
if (p==1)
{
printf("Element found\n");
}
else
{
printf("Element NOT found\n");
}
traversal(tail);
printf("\n");
destroy(&tail);
}
dec_queue_array.h
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#define SIZE 1000
struct queue{
int arr[SIZE];
int front;
int rear;
};
int init(struct queue **);
int isEmpty(struct queue *);
int isFull(struct queue *);
int getFront(struct queue *, int *dataPtr);
int getRear(struct queue *, int *dataPtr);
int enqueue(struct queue *, int);
int dequeue(struct queue *, int *);
int destroy(struct queue **);
def_queue_array.c
#include "dec_queue_array.h"
return 1;
}
}
if (qPtr->front == -1)
{
qPtr->front = 0;
}
return 1;
}
#include <stdio.h>
#include "dec_queue_array.h"
int main()
{
int i=0, p=0, q=0;
struct queue *head = NULL;
init(&head);
enqueue(head, 4);
enqueue(head, 214);
enqueue(head, 67);
enqueue(head, 13);
dequeue(head, &i);
printf("%d\n", i);
dequeue(head, &i);
printf("%d\n", i);
getFront(head, &p);
printf("%d\n", p);
getRear(head, &q);
printf("%d\n", q);
destroy(&head);
}
dec_queue_circular_array.h
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#define SIZE 1000
struct queue{
int arr[SIZE];
int front;
int rear;
};
def_queue_circular_array.c
#include "dec_queue_circular_array.h"
return 1;
}
}
#include <stdio.h>
#include "dec_queue_circular_array.h"
int main()
{
int i=0, p=0, q=0;
struct queue *head = NULL;
init(&head);
enqueue(head, 4);
enqueue(head, 45);
enqueue(head, 89);
getFront(head, &i);
printf("%d\n", i);
getRear(head, &i);
printf("%d\n", i);
dequeue(head, &p);
printf("%d\n", p);
dequeue(head, &p);
printf("%d\n", p);
dequeue(head, &q);
printf("%d\n", q);
enqueue(head, 456);
getFront(head, &i);
printf("%d\n", i);
getRear(head, &i);
printf("%d\n", i);
dequeue(head, &q);
printf("%d\n", q);
destroy(&head);
}
dec_queue_linkedlist.h
#include <stdio.h>
#include <stdlib.h>
struct qNode {
int data;
struct qNode *next;
};
struct queue {
struct qNode *front, *rear;
};
struct qNode* newNode(int k);
struct queue* createQueue();
int enqueue(struct queue* q, int k);
int dequeue(struct queue* q, int *dataPtr);
def_queue_linkedlist.c
#include "dec_queue_linkedlist.h"
if (q->rear == NULL) {
q->front = q->rear = temp;
return 1;
}
q->rear->next = temp;
q->rear = temp;
return 1;
}
int dequeue(struct queue* q, int *dataPtr)
{
if (q->front == NULL)
{
return 0;
}
struct qNode* temp = q->front;
*dataPtr = q->front->data;
q->front = q->front->next;
if (q->front == NULL)
{
q->rear = NULL;
}
return 1;
free(temp);
}
#include<stdio.h>
#include "dec_queue_linkedlist.h"
int main()
{
struct queue* q = createQueue();
int p = 0;
enqueue(q, 10);
enqueue(q, 20);
dequeue(q, &p);
printf("%d\n", p);
dequeue(q, &p);
printf("%d\n", p);
enqueue(q, 30);
enqueue(q, 40);
enqueue(q, 50);
dequeue(q, &p);
printf("%d\n", p);
printf("Queue Front : %d \n", q->front->data);
printf("Queue Rear : %d\n", q->rear->data);
return 0;
}
dec_stack_array.h
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
def_stack_array.c
#include "dec_stack_array.h"
}
int pop(struct stack *s, int *data)
{
if(isEmpty(s) == 1)
{
//cannot pop from an empty stack
return 0;
}
*data = s->arr[s->top];
s->top = s->top - 1;
return 1;
}
int peek(struct stack *s, int *data)
{
if (isEmpty(s))
{
//can`t peek stack is empty
return 0;
}
*data = s->arr[s->top];
return 1;
}
int destroy(struct stack **s)
{
free(*s);
*s = NULL;
return 1;
}
#include "dec_stack_array.h"
int main()
{
//DRIVER CODE
int i = 0;
struct stack *s = NULL;
init(&s);
push(s, 101);
push(s, 203);
push(s, 305);
push(s, 408);
while (! isEmpty(s))
{
pop(s, &i);
printf("%d\n", i);
}
destroy(&s);
return 1;
}
STACKS USING LINKED LISTS
stackdec_linklist.h
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
struct stack {
struct node *top;
};
struct node {
int data;
struct node* next;
};
int init(struct stack **stack);
int push(struct stack *stack, int data);
int pop(struct stack *stack, int *dataPtr);
int peek(struct stack *stack, int *dataPtr);
int isEmpty(struct stack *stack);
int isFull(struct stack *stack);
int destroy(struct stack **stack);
stackdef_linklist.c
#include "stackdec_linklist.h"
temp = sPtr->top;
*dataPtr = temp->data;
sPtr->top = temp->next;
free(temp);
return 1;
}
int isFull(struct stack *sPtr)
{
struct node *newNode = (struct node *)malloc(sizeof(struct node));
if (newNode == NULL)
{
return 1;
}
else
{
free(newNode);
return 0;
}
}
int destroy(struct stack **sPtrPtr)
{
int data = 0;
while(!isEmpty(*sPtrPtr))
{
pop(*sPtrPtr, &data);
}
free(*sPtrPtr);
*sPtrPtr = NULL;
return 1;
}
#include<stdio.h>
#include "stackdec_linklist.h"
int main()
{
int i=0, n=0;
struct stack *sPtr = NULL;
init(&sPtr);
push(sPtr, 34);
push(sPtr, 93);
peek(sPtr, &n);
printf("%d\n", n);
while (! isEmpty(sPtr))
{
pop(sPtr, &i);
printf("%d\n", i);
}
destroy(&sPtr);
return 1;
}
stackdec_linklist_char.h
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
struct stack {
struct node *top;
};
struct node {
char data;
struct node* next;
};
int init(struct stack **stack);
int push(struct stack *stack, char data);
int pop(struct stack *stack, char *dataPtr);
int peek(struct stack *stack, char *dataPtr);
int isEmpty(struct stack *stack);
int isFull(struct stack *stack);
int destroy(struct stack **stack);
stackdef_linklist_char.c
#include "stackdec_linklist_char.h"
temp = sPtr->top;
*dataPtr = temp->data;
sPtr->top = temp->next;
free(temp);
return 1;
}
int isFull(struct stack *sPtr)
{
struct node *newNode = (struct node *)malloc(sizeof(struct node));
if (newNode == NULL)
{
return 1;
}
else
{
free(newNode);
return 0;
}
}
int destroy(struct stack **sPtrPtr)
{
char data ;
while(!isEmpty(*sPtrPtr))
{
pop(*sPtrPtr, &data);
}
free(*sPtrPtr);
*sPtrPtr = NULL;
return 1;
}
#include "stackdec_linklist_char.h"
#include <string.h>
//REVERSING THE GIVEN STRING USING ADT
int main()
{
struct stack *sPtr = NULL;
char data ;
init(&sPtr);
char s[] = "HELLO EVERYONE";
int n;
n = strlen(s);
for (int i = 0; i<n; i++)
{
if(!push(sPtr, s[i]))
{
return 1;
}
}
while (!isEmpty(sPtr))
{
pop(sPtr, &data);
printf("%c", data);
}
printf("\n");
destroy(&sPtr);
return 0;
linked_list_dec.h
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include "linked_list_dec.h"
#include<stdio.h>
#include "linked_list_dec.h"
int main()
{
struct node *head = NULL;
init(&head);
insertNode(&head, 45, 1);
insertNode(&head, 20, 2);
insertNode(&head, 78, 2);
int p = searchKey(head, 990);
if(p)
{
printf("yes, element found\n");
}
else
{
printf("No, element not found\n");
}
traverseList(head);
printf("\n");
deleteNode(&head, 20);
traverseList(head);
destroy(&head);
}