Professional Documents
Culture Documents
Single Linked List
Single Linked List
Single Linked List
#include<stdio.h>
#include<stdlib.h>
struct node{
int key;
};
temp->key = key;
temp->next = NULL;
return temp;
temp->next= *head;
*head = temp;
if(*head = NULL){
*head = temp;
return 0;
while(last->next != NULL){
last = last->next;
last->next = temp;
}
void insert_after(struct node* prev,int key){
if(prev == NULL){
printf("Not found.");
return;
temp->next = prev->next;
prev->next = temp;
while(temp!=NULL){
if(temp->key == key){
return temp;
temp= temp->next;
return NULL;
*head = temp->next;
free(temp);
while(temp!= NULL){
printf("%d\t",temp->key);
temp= temp->next;
int main(){
int n = sizeof(key)/sizeof(key[0]);
for(int i=0;i<n;i++){
insert_at_end(&head,key[i]);
print_list(head);
insert_at_beginning(&head,111);
insert_at_end(&head,999);
insert_after(prev,555);
delete_key(&head,87);
delete_key(&head,356);
delete_key(&head,124);
print_list(head);
#include<stdlib.h>
struct node{
int data;
};
struct node* createnode(int data){
newnode->data = data;
newnode->prev = NULL;
newnode->next = NULL;
return newnode;
if(*head == NULL){
*head = newnode;
else{
newnode->next = *head;
(*head)->prev = newnode;
*head= newnode;
if(*head == NULL){
*head = newnode;
else{
while(current->next != NULL){
current = current->next;
current->next = newnode;
newnode->prev = current;
}
}
while(current!= NULL){
if(current->data == key){
return 1;
current = current->next;
return 0;
current = current->next;
if(current == NULL){
if(current->prev != NULL){
current->prev->next = current->next;
else{
*head = current->next;
if(current->next != NULL){
current->next->prev = current->prev;
free(current);
printf("Linked list\n");
while(current!=NULL){
printf("%d\t",current->data);
current= current->next;
while(tail->next != NULL){
tail= tail->next;
while(tail!=NULL){
printf("%d\t",tail->data);
tail = tail->prev;
printf("\n");
int main(){
insert_at_end(&head,6);
insert_at_end(&head,8);
insert_at_end(&head,55);
insert_at_end(&head,22);
insert_at_beginning(&head,2);
insert_at_end(&head,5);
if(search(head,8)){
printf("Element 8 is present.\n");
else{
delete_node(&head,55);
print_list(head);
print_opp(head);
return 0;
#include<stdlib.h>
struct node{
int data;
};
newnode->data = data;
newnode->next = NULL;
return newnode;
if(*head == NULL){
*head = newnode;
newnode->next = newnode;
else{
while(last->next != *head){
last = last->next;
}
last->next = newnode;
newnode->next = *head;
*head = newnode;
if(*head == NULL){
*head = newnode;
newnode->next = newnode;
else{
while(last->next != *head){
last = last->next;
last->next = newnode;
newnode->next= *head;
if(current == NULL){
return 0;
do{
if(current->data == key)
return 1;
current = current->next;
while(current!= head);
return 0;
if(*head == NULL){
return;
while(current->data != key){
if(current->next == *head){
return;
prev = current;
current = current->next;
*head = NULL;
free(current);
return;
if(current == *head){
prev = (*head)->next;
while(prev->next != *head)
prev = prev->next;
*head = (*head)->next;
prev->next = *head;
free(current);
prev->next = *head;
free(current);
else{
prev->next = current->next;
free(current);
if(current == NULL)
return;
do{
printf("%d\t",current->data);
current= current->next;
}while(current!= head);
printf("\n");
int main(){
insert_at_end(&head,6);
insert_at_end(&head,8);
insert_at_end(&head,55);
insert_at_end(&head,22);
insert_at_beginning(&head,2);
insert_at_end(&head,5);
if(search(head,8)){
printf("Element 8 is present.\n");
}else{
delete_node(&head,55);
printf("Linked list:");
print_list(head);
return 0;
Queue in array
#include <stdio.h>
struct Queue {
int arr[MAX_SIZE];
};
q->front = -1;
q->rear = -1;
}
// Function to enqueue element into queue
if (isFull(q)) {
printf("Queue is full\n");
return;
if (isEmpty(q)) {
q->front = q->rear = 0;
} else {
q->rear++;
q->arr[q->rear] = data;
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
if (q->front == q->rear) {
} else {
q->front++;
return dequeuedData;
printf("Queue is empty\n");
return -1;
return q->arr[q->front];
if (isEmpty(q)) {
printf("Queue is empty\n");
return;
int i = q->front;
printf("Queue: ");
while (i != q->rear) {
i = (i + 1) % MAX_SIZE;
printf("%d\n", q->arr[i]);
int main() {
struct Queue q;
initialize(&q);
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
printQueue(&q);
return 0;
#include <stdlib.h>
struct Node {
int data;
};
struct Queue {
};
q->front = NULL;
q->rear = NULL;
if (newNode == NULL) {
return;
newNode->data = data;
newNode->next = NULL;
if (isEmpty(q)) {
} else {
q->rear->next = newNode;
q->rear = newNode;
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
q->front = temp->next;
free(temp);
if (q->front == NULL) {
q->rear = NULL;
return dequeuedData;
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
return q->front->data;
if (temp == NULL) {
printf("Queue is empty\n");
return;
printf("Queue: ");
temp = temp->next;
printf("\n");
int main() {
struct Queue q;
initialize(&q);
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
printQueue(&q);
return 0;
struct Stack {
int arr[MAX_SIZE];
int top;
};
s->top = -1;
if (isFull(s)) {
printf("Stack Overflow\n");
return;
s->arr[++s->top] = data;
if (isEmpty(s)) {
printf("Stack Underflow\n");
return -1;
return s->arr[s->top--];
if (isEmpty(s)) {
printf("Stack is empty\n");
return -1;
}
return s->arr[s->top];
if (isEmpty(stack)) {
printf("Stack is empty\n");
return;
printf("Stack: ");
printf("\n");
int main() {
struct Stack s;
initialize(&s);
push(&s, 10);
push(&s, 20);
push(&s, 30);
print_stack(&s);
return 0;
}
Stack using linked list
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
struct Stack {
};
s->top = NULL;
if (newNode == NULL) {
newNode->data = data;
newNode->next = s->top;
s->top = newNode;
if (isEmpty(s)) {
printf("Stack Underflow\n");
return -1;
s->top = temp->next;
free(temp);
return poppedData;
if (isEmpty(s)) {
printf("Stack is empty\n");
return -1;
return s->top->data;
printf("Stack: ");
while (current != NULL) {
current = current->next;
printf("NULL\n");
int main() {
struct Stack s;
initialize(&s);
push(&s, 10);
push(&s, 20);
push(&s, 30);
printStack(&s);
return 0;
Linear search
#include <stdio.h>
if (arr[i] == key) {
return i; // Return the index if the key is found
int main() {
if (index != -1) {
} else {
return 0;
Binary Search
#include <stdio.h>
if (arr[mid] == target)
return mid;
left = mid + 1;
else
right = mid - 1;
return -1;
// Main function
int main() {
if (result == -1)
else
return 0;
}
Bubble sort
#include <stdio.h>
*a = *b;
*b = temp;
printf("\n");
int main() {
printf("\n");
bubbleSort(arr, n);
printf("\n");
return 0;
Quick Sort
#include <stdio.h>
*a = *b;
*b = temp;
return i + 1;
// Partitioning index
printf("\n");
quickSort(arr, pi + 1, high);
printf("\n");
// Main function
int main() {
printArray(arr, n);
quickSort(arr, 0, n - 1);
printArray(arr, n);
return 0;
Selection Sort
#include <stdio.h>
*a = *b;
*b = temp;
int min_index = i;
min_index = j;
swap(&arr[min_index], &arr[i]);
printf("\n");
int main() {
printf("\n");
selectionSort(arr, n);
printf("\nSorted array: ");
printf("\n");
return 0;
Insertion Sort
#include <stdio.h>
int i, key, j;
key = arr[i];
j = i - 1;
/* Move elements of arr[0..i-1], that are greater than key, to one position ahead
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
}
printf("\n");
int main() {
printf("\n");
insertionSort(arr, n);
printf("\n");
return 0;
Merge Sort
#include <stdio.h>
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
i = 0;
j = 0;
k = l;
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
k++;
i++;
k++;
arr[k] = R[j];
j++;
k++;
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
printf("\n");
printf("\n");
// Main function
int main() {
printArray(arr, n);
mergeSort(arr, 0, n - 1);
printArray(arr, n);
return 0;
Heap Sort
#include <stdio.h>
*a = *b;
*b = temp;
largest = left;
largest = right;
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
printf("\n");
int main() {
printf("\n");
heapSort(arr, n);
printf("\nSorted array: ");
printf("\n");
return 0;
BST
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
return createNode(value);
return root;
if (root == NULL) {
printf("Tree is empty\n");
return -1;
current = current->left;
return current->data;
if (root == NULL) {
printf("Tree is empty\n");
return -1;
current = current->right;
return current->data;
if (root != NULL) {
inorder(root->left);
inorder(root->right);
if (root != NULL) {
preorder(root->left);
preorder(root->right);
if (root != NULL) {
postorder(root->left);
postorder(root->right);
}
}
int main() {
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
inorder(root);
printf("\n");
preorder(root);
printf("\n");
postorder(root);
printf("\n");
return 0;
}
Postfix-Infix
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct Stack {
int top;
unsigned capacity;
int* array;
};
if (!stack)
return NULL;
stack->top = -1;
stack->capacity = capacity;
if (!stack->array)
return NULL;
return stack;
}
// Function to push element onto stack
stack->array[++stack->top] = item;
if (isEmpty(stack))
return -1;
return stack->array[stack->top--];
if (isEmpty(stack))
return -1;
return stack->array[stack->top];
return 1;
return 2;
return -1;
if (!postfix)
return NULL;
if (!stack)
return NULL;
int j = 0;
if (isalnum(infix[i]))
postfix[j++] = infix[i];
push(stack, infix[i]);
postfix[j++] = pop(stack);
else
pop(stack);
} else {
postfix[j++] = pop(stack);
push(stack, infix[i]);
}
while (!isEmpty(stack))
postfix[j++] = pop(stack);
postfix[j] = '\0';
return postfix;
if (!stack)
return -1;
if (isdigit(postfix[i]))
else {
switch (postfix[i]) {
case '+':
break;
case '-':
break;
case '*':
case '/':
break;
return pop(stack);
int main() {
free(postfix);
return 0;