0% found this document useful (0 votes)
44 views33 pages

DSA ADT Implementation

This document describes data structures and algorithms for doubly linked lists, circular linked lists, and queues implemented using arrays. It includes the header and code for implementing each with functions for initialization, insertion, traversal, search, deletion, and destruction. Sample main functions are provided to demonstrate usage of each data structure.

Uploaded by

Nikhita Evuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views33 pages

DSA ADT Implementation

This document describes data structures and algorithms for doubly linked lists, circular linked lists, and queues implemented using arrays. It includes the header and code for implementing each with functions for initialization, insertion, traversal, search, deletion, and destruction. Sample main functions are provided to demonstrate usage of each data structure.

Uploaded by

Nikhita Evuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

DOUBLY LINKED LISTS

dec_doubly_linkedlist.h

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>

struct node
{
int data;
struct node *pre;
struct node *next;
};

int init(struct node **head, struct node **tail);


int insertElement(struct node **head, struct node **tail, int element,
int position);
int traverse(struct node *head, struct node *tail, int dirFlag);
int search(struct node *head, int key, struct node **keyPtr);
int deleteNode(struct node **head, struct node **tail, int key);
int destroy(struct node **head, struct node **tail);

def_doubly_linkedlist.c

#include "dec_doubly_linkedlist.h"

int init(struct node **head, struct node **tail)


{
*head = NULL;
*tail = NULL;
return 1;
}

int insertElement(struct node **head, struct node **tail, int element,


int position)
{
struct node *newNode = (struct node *)malloc(sizeof(struct node));
int i=1;
struct node *iter = *head;
struct node *pred = NULL;
if(!newNode)
{
return 0;
}
newNode->data = element;
while(iter && i<position)
{
pred = iter;
iter = iter->next;
i=i+1;
}
if(!iter && i!=position)
{
//Non existent position has been specified
return 0;
}
if(!pred)
{
newNode->pre = NULL;
newNode->next = *head;
if(!(*head))
{
*tail = newNode;
}
else
{
(*head)->pre = newNode;
}
*head = newNode;
}
else
{
newNode->pre = pred;
newNode->next = pred->next;
pred->next = newNode;
if(pred != *tail)
{
//Middle insertion
newNode->next->pre = newNode;
}
else
{
*tail = newNode;
}
}
return 1;
}

int traverse(struct node *head, struct node *tail, int dirFlag)


{
if(!head)
{
return 0;
}
struct node *iter = NULL;
if(!dirFlag)
{
//FORWARD DIRECTION TRAVERSAL
for(iter = head;iter;iter=iter->next)
{
printf("<-%d->", iter->data);
}
return 1;
}
else
{
//Backward direction traversal
for(iter=tail;iter;iter=iter->pre)
{
printf("<-%d->", iter->data);
}
return 1;
}

int search(struct node *head, int key, struct node **keyPtr)


{
struct node *iter = head;
while(iter && iter->data != key)
{
iter = iter->next;
}
if(!iter)
{
//UNSUCCESSFULL SEARCH
*keyPtr = NULL;
return 0;
}
else
{
//SUCCESSFULL SEARCH
*keyPtr = iter;
return 1;
}
}

int deleteNode(struct node **head, struct node **tail, int key)


{
struct node *keyPtr = NULL;
search(*head, key, &keyPtr);
if(keyPtr == NULL)
{
return 0;
}
if(keyPtr == *head && keyPtr == *tail)
{
*head = NULL;
*tail = NULL;
}
else if(keyPtr == *head)
{
*head = (*head)->next;
(*head)->pre = NULL;
}
else if(keyPtr != *tail)
{
keyPtr->next->pre = keyPtr->pre;
keyPtr->pre->next = keyPtr->next;
}
else
{
//(keyPtr == *tail)
*tail = keyPtr->pre;
(*tail)->next = NULL;
}
free(keyPtr);
return 1;
}
int destroy(struct node **head, struct node **tail)
{
int flag = 0;
if(!(*head))
{
return 1;
}
while(*head)
{
flag = deleteNode(head, tail, (*head)->data);
if(!flag)
{
return 0;
}
}
return 1;
}

//SAMPLE CODE USING DOUBLY LINKED LISTS

#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");
}

ADT for CIRCULARLY LINKED LISTS

dec_cirularlinkedlist.h

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>

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

int init(struct node **tail);


int insertAfterElement(struct node **tail, int pred, int element);
int traversal(struct node *tail);
int searchElement(struct node *tail, int key, struct node **keyPtr);
int deleteElement(struct node **tail, int key);
int destroy(struct node **tail);

def_circularlinkedlist.c

#include "dec_cirularlinkedlist.h"

int init(struct node **tail)


{
*tail = NULL;
}

int insertAfterElement(struct node **tail, int pred, int element)


{
struct node *prePtr = NULL;
struct node *newNode = (struct node *)malloc(sizeof(struct node));
if(!newNode)
{
return 0;
}
newNode->data=element;
if(*tail ==NULL)
{
newNode->next = newNode;
*tail = newNode;
return 1;
}
else
{
prePtr = (*tail)->next;
do
{
if(prePtr->data == pred)
{
newNode->next = prePtr->next;
prePtr->next = newNode;
return 1;
}
prePtr = prePtr->next;
} while (prePtr != (*tail)->next);

}
free(newNode);
return 0;
}

int traversal(struct node *tail)


{
struct node *iter = NULL;
if(!tail)
{
return 0;
}
iter = tail->next;
do
{
printf("%d ", iter->data);
iter = iter->next;
}while(iter != tail->next);
return 1;
}

int searchElement(struct node *tail, int key, struct node **keyPtr)


{
struct node *iter = NULL;
*keyPtr = NULL;
if(!tail)
{
return 0;
}
iter = tail->next;
do
{
if(key == iter->data)
{
*keyPtr = iter;
return 1;
}
iter = iter->next;
} while (iter!=tail->next);

int deleteElement(struct node **tail, int key)


{
struct node *prePtr = NULL;
struct node *iter = NULL;
if(!(*tail))
{
return 0;
}
prePtr = *tail;
iter = (*tail)->next;
do
{
if(key==iter->data)
{
if(prePtr == iter)
{
*tail = NULL;
}
else
{
prePtr->next = iter->next;
if(iter==(*tail))
{
(*tail) == prePtr;
}
}
free(iter);
return 1;
}

prePtr = iter;
iter = iter->next;
} while (iter!=(*tail)->next);

return 0;

int destroy(struct node **tail)


{
struct node *temp = NULL;
struct node *iter = NULL;
if(!(*tail))
{
return 1;
}
iter = (*tail)->next;
while(*tail)
{
temp = iter->next;
deleteElement(tail, iter->data);
iter = temp;
}
return 1;
}
//SAMPLE CODE FOR CIRCULAR LINKED LISTS

#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);
}

QUEUES USING ARRAYS

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"

int init(struct queue **qPtrPtr)


{
*qPtrPtr = (struct queue*)malloc(sizeof(struct queue));
if(!(*qPtrPtr))
{
return 0;
}
(*qPtrPtr)->front = -1;
(*qPtrPtr)->rear = -1;
return 1;
}

int isEmpty(struct queue *qPtr)


{
return (qPtr->front == -1)?1:0;
}

int isFull(struct queue *qPtr)


{
return (qPtr->rear == SIZE-1)?1:0;
}

int getFront(struct queue *qPtr, int *dataPtr)


{
if(isEmpty(qPtr))
{
return 0;
}
else
{
*dataPtr = qPtr->arr[qPtr->front];

return 1;
}
}

int getRear(struct queue *qPtr, int *dataPtr)


{
if(isEmpty(qPtr))
{
return 0;
}
else
{
*dataPtr = qPtr->arr[qPtr->rear];
return 1;
}
}

int enqueue(struct queue *qPtr, int data)


{
if(isFull(qPtr))
{
return 0;
}
qPtr->rear = qPtr->rear + 1;
qPtr->arr[qPtr->rear] = data;

if (qPtr->front == -1)
{
qPtr->front = 0;
}
return 1;
}

int dequeue(struct queue *qPtr, int *dataPtr)


{
if(isEmpty(qPtr))
{
return 0;
}
*dataPtr = qPtr->arr[qPtr->front];
for (int i=(qPtr->front)+1;i<=qPtr->rear;i++)
{
qPtr->arr[i-1] = qPtr->arr[i];
}
qPtr->rear = qPtr->rear-1;
if(qPtr->rear == -1)
{
qPtr->front = -1;
}
return 1;
}

int destroy(struct queue **qPtrPtr)


{
free(*qPtrPtr);
*qPtrPtr = NULL;
return 1;
}

//SAMPLE CODE FOR QUEUES USING ARRAYS

#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);
}

CIRCULAR QUEUES USING ARRAYS

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;
};

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_circular_array.c

#include "dec_queue_circular_array.h"

int init(struct queue **qPtrPtr)


{
*qPtrPtr = (struct queue*)malloc(sizeof(struct queue));
if(!(*qPtrPtr))
{
return 0;
}
(*qPtrPtr)->front = -1;
(*qPtrPtr)->rear = -1;
return 1;
}

int isEmpty(struct queue *qPtr)


{
return (qPtr->front == -1 && qPtr->rear == -1)?1:0;
}

int isFull(struct queue *qPtr)


{
return (((qPtr->rear)+1)%SIZE == qPtr->front)?1:0;
}

int getFront(struct queue *qPtr, int *dataPtr)


{
if(isEmpty(qPtr))
{
return 0;
}
else
{
*dataPtr = qPtr->arr[qPtr->front];

return 1;
}
}

int getRear(struct queue *qPtr, int *dataPtr)


{
if(isEmpty(qPtr))
{
return 0;
}
else
{
*dataPtr = qPtr->arr[qPtr->rear];
return 1;
}
}

int enqueue(struct queue *qPtr, int data)


{
if(isFull(qPtr))
{
return 0;
}
qPtr->rear = (qPtr->rear + 1) % SIZE; // circular queue
qPtr->arr[qPtr->rear] = data;
if (qPtr->front == -1)
{
qPtr->front = 0;
}
return 1;

int dequeue(struct queue *qPtr, int *dataPtr)


{
if(isEmpty(qPtr))
{
return 0;
}
if(qPtr->rear == qPtr->front)
{
*dataPtr = qPtr->arr[qPtr->front];
qPtr->front = -1;
qPtr->rear = -1;
return 1;
}
*dataPtr = qPtr->arr[qPtr->front];
qPtr->front = (qPtr->front+1)%SIZE;
return 1;
}

int destroy(struct queue **qPtrPtr)


{
free(*qPtrPtr);
*qPtrPtr = NULL;
return 1;
}

//SAMPLE CODE FOR CIRCULAR QUEUES USING ARRAYS

#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);
}

QUEUES FOR LINKED LISTS

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"

struct qNode* newNode(int k)


{
struct qNode* temp = (struct qNode*)malloc(sizeof(struct qNode));
temp->data = k;
temp->next = NULL;
return temp;
}

struct queue* createQueue()


{
struct queue* q = (struct queue*)malloc(sizeof(struct queue));
q->front = q->rear = NULL;
return q;
}

int enqueue(struct queue* q, int k)


{

struct qNode* temp = newNode(k);

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);
}

//SAMPLE CODE FOR QUEUES USING LINKED LISTS

#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;
}

STACKS USING ARRAYS

dec_stack_array.h

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>

#define SIZE 1000


struct stack
{
int arr[SIZE];
int top;
};
int init(struct stack **);
int push(struct stack*, int );
int pop(struct stack*, int*);
int peek(struct stack*, int*);
int isEmpty(struct stack*);
int isFull(struct stack*);
int destroy(struct stack**);

def_stack_array.c

#include "dec_stack_array.h"

int init(struct stack** s)


{
* s = (struct stack *)malloc(sizeof(struct stack));
if (*s == NULL){
//malloc got failed
return 0;
}
else
{
(*s)->top = -1;
return 1;
}
}
int isEmpty(struct stack *s){
if(s->top == -1)
{
return 1;
}
else
{
return 0;
}
}

int isFull(struct stack *s)


{
if (s->top == SIZE - 1)
{
return 1;
}
else
{
return 0;
}
}
int push(struct stack *s, int data)
{
if (isFull(s))
{
//stack is full cant push anymore
return 0;
}
s->top = s->top + 1;
s->arr[s->top] = data;
return 1;

}
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;
}

//SAMPLE CODE FOR STACKS USING ARRAYS

#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"

int init(struct stack **sPtrPtr)


{
*sPtrPtr = (struct stack *)malloc(sizeof(struct stack));
if (*sPtrPtr == NULL)
{
return 0;
}
(*sPtrPtr)->top = NULL;
return 1;
}
int push(struct stack *sPtr, int data)
{
struct node *newNode = (struct node *)malloc(sizeof(struct node));
if (newNode == NULL)
{
return 0;
}
newNode->data = data;
newNode->next = sPtr->top;
sPtr->top=newNode;
return 1;
}
int isEmpty(struct stack *sPtr)
{
if (sPtr->top == NULL)
{
return 1;
}
else
{
return 0;
}
}
int peek(struct stack *sPtr, int *dataPtr)
{
if (isEmpty(sPtr))
{
return 0;
}
*dataPtr = (sPtr->top)->data;
return 1;
}
int pop(struct stack *sPtr, int *dataPtr)
{
struct node *temp = NULL;
if (isEmpty(sPtr))
{
return 0;
}

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;
}

//SAMPLE CODE FOR STACKS USING LINKED LISTS

#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;
}

STACKS USING LINKED LISTS FOR CHARACTERS

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"

int init(struct stack **sPtrPtr)


{
*sPtrPtr = (struct stack *)malloc(sizeof(struct stack));
if (*sPtrPtr == NULL)
{
return 0;
}
(*sPtrPtr)->top = NULL;
return 1;
}
int push(struct stack *sPtr, char data)
{
struct node *newNode = (struct node *)malloc(sizeof(struct node));
if (newNode == NULL)
{
return 0;
}
newNode->data = data;
newNode->next = sPtr->top;
sPtr->top=newNode;
return 1;
}
int isEmpty(struct stack *sPtr)
{
if (sPtr->top == NULL)
{
return 1;
}
else
{
return 0;
}
}
int peek(struct stack *sPtr, char *dataPtr)
{
if (isEmpty(sPtr))
{
return 0;
}
*dataPtr = (sPtr->top)->data;
return 1;
}
int pop(struct stack *sPtr, char *dataPtr)
{
struct node *temp = NULL;
if (isEmpty(sPtr))
{
return 0;
}

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;
}

//SAMPLE CODE FOR STACKS USING LINKED LISTS FOR CHARACTERS

#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 IMPLEMENTATION

linked_list_dec.h

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>

struct node *head;


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

void init(struct node **sPtrPtr);


int insertNode(struct node **sPtrPtr, int data, int position);
int traverseList(struct node *sPtr);
int searchKey(struct node *sPtr, int key);
int deleteNode(struct node **sPtrPtr, int key);
int missingNo (struct node* sPtr, int n);
int destroy(struct node **sPtrPtr);
linked_list_def.c

#include "linked_list_dec.h"

void init(struct node **head)


{
*head = NULL;
}

int insertNode (struct node **head, int data, int pos)


{
struct node *newNode = (struct node *)malloc(sizeof(struct node));
if (newNode == NULL)
{
return 0;
}
(* newNode).data = data;
struct node *pre = *head;
if (pos==1)
{
//INSERTION AT THE BEGINNING
newNode->next = *head;
*head = newNode;
return 1;
}
else
{
for(int i=1;i<=(pos-2) && pos!=0;i++)
{
pre=pre->next;
}
}
newNode->next = pre->next;
pre->next = newNode;
return 1;
}

int traverseList(struct node *sPtr)


{
struct node *iter = sPtr;
while(iter!=NULL)
{
printf("%d->", iter->data);
iter = iter->next;
}
return 1;
}

int searchKey(struct node *sPtr, int key)


{
struct node *iter = sPtr;
while(iter!=NULL)
{
if(iter->data==key)
{
return 1;
}
iter = iter->next;
}
return 0;
}

int deleteNode(struct node **sPtrPtr, int data)


{
struct node *iter = *sPtrPtr;
struct node *pre = NULL;
while(iter!=NULL && iter->data != data)
{
pre = iter;
iter = iter->next;
}
if (iter == NULL)
{
return 0;
}
else
{
if(pre==NULL)
{
*sPtrPtr = (*sPtrPtr)->next;
}
else
{
pre->next=iter->next;
}
free(iter);
}
}

int missingNo (struct node* sPtr, int n)


{
struct node* ptr = sPtr;
int sum;
sum = (n+1)*(n+2)/2;
while (ptr)
{
sum = sum - ptr->data;
ptr = ptr->next;
}
return sum;
}

int destroy(struct node **sPtrPtr)


{
free(*sPtrPtr);
*sPtrPtr = NULL;
return 1;
}

//SAMPLE CODE FOR LINKED LISTS

#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);
}

You might also like