Skip to content

Instantly share code, notes, and snippets.

@pSnehanshu
Last active October 24, 2018 10:59
Show Gist options
  • Save pSnehanshu/fa1f0c5a046bff7b8e8387b05cb640ad to your computer and use it in GitHub Desktop.
Save pSnehanshu/fa1f0c5a046bff7b8e8387b05cb640ad to your computer and use it in GitHub Desktop.
To be submitted on 27 sep
#include <stdio.h>
// Inserts an element. Returns the number of new elements.
int insert(int arr[], int len, int num, int key)
{
int i;
if (len == key)
{
arr[key] = num;
return len + 1;
}
else if (key > len || key < 0) {
printf("\n****************************\nCannot insert at that position.\n");
}
else
{
for (i = len - 1; i >= key; i--)
{
// Shifting elements to right
arr[i + 1] = arr[i];
// Stop when key is reached.
if (i == key)
{
arr[key] = num;
return len + 1;
}
}
}
return len;
}
// Inserts an element. Returns the number of new elements.
int delete (int arr[], int len, int key)
{
int i;
for (i = key; i < len; i++)
{
arr[i] = arr[i + 1];
}
return len - 1;
}
// Display a menu
int menu()
{
int option;
printf("\nSelect an option:\n 1) Insert an element\n 2) Delete an element\n 3) Exit\nAns: ");
scanf("%d", &option);
return option;
}
int display(int arr[], int len)
{
int i;
for (i = 0; i < len; i++)
printf("[%d]=>%d ", i, arr[i]);
}
// Driver Code
int main()
{
int arr[20] = {};
int len = 0, num, key;
while (1)
{
printf("\n----------------------------------------\nArray: ");
display(arr, len);
printf("\n");
switch (menu())
{
case 1:
printf("What do you want to insert? : ");
scanf("%d", &num);
printf("At which position do you want to insert %d? : ", num);
scanf("%d", &key);
len = insert(arr, len, num, key);
break;
case 2:
printf("Enter the position of the element that you want to delete: ");
scanf("%d", &key);
len = delete (arr, len, key);
break;
case 3:
return 0;
default:
printf("\nInvalid option\n");
}
}
}
#include <stdio.h>
#include <stdlib.h>
// defining the node
typedef struct node
{
int info;
struct node *link;
} mynode;
mynode *start = NULL;
// Function Definitions ////////////////////////////////////////////////////////////
// display the linked list
void display()
{
if (start == NULL)
printf("\n\nEmpty List\n");
else
{
mynode *ptr = start;
printf("------------------------------------------------------\nStart => %d =>", ptr->info);
while (ptr->link != NULL)
{
ptr = ptr->link;
printf(" %d =>", ptr->info);
}
printf(" NULL\n");
}
}
//insert a node at first position
void insertAtFirst(int info)
{
mynode *new = (mynode *)malloc(sizeof(mynode));
new->info = info;
new->link = NULL;
if (start == NULL)
start = new;
else
{
new->link = start;
start = new;
}
}
//insert a node at last position
void insertAtLast(int info)
{
mynode *new = (mynode *)malloc(sizeof(mynode));
new->info = info;
new->link = NULL;
if (start == NULL)
start = new;
else
{
mynode *ptr = start;
while (ptr->link != NULL)
ptr = ptr->link;
ptr->link = new;
}
}
//insert after given key
void insertAfterKey(int info, int key)
{
mynode *new = (mynode *)malloc(sizeof(mynode));
new->info = info;
new->link = NULL;
if (start == NULL)
printf("\n\nCan't perform this operation");
else
{
mynode *ptr = start;
while (ptr->info != key && ptr->link != NULL)
ptr = ptr->link;
//If key not found
if (ptr->info != key)
printf("\n\nKey not Found\n");
else
{
new->link = ptr->link;
ptr->link = new;
}
}
}
////////////////////////////////////////////////////////////////
int main()
{
int choice, info, key;
do
{
printf("\nMenu:\n1) Insert at first\n2) Insert at last\n3) Insert after key\n");
printf("Response: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter value :");
scanf("%d", &info);
insertAtFirst(info);
break;
case 2:
printf("Enter value :");
scanf("%d", &info);
insertAtLast(info);
break;
case 3:
printf("Enter key :");
scanf("%d", &key);
printf("Enter value :");
scanf("%d", &info);
insertAfterKey(info, key);
break;
default:
printf("\nWrong Choice\n");
}
display();
} while (1);
return 0;
}
/* C implementation QuickSort */
#include<stdio.h>
// A utility function to swap two elements
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high- 1; j++)
{
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
// Driver program to test above functions
int main()
{
int arr[] = {-10, 7, 8, 9, 1, 5};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n-1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
const int max = 10;
int top = -1;
int stack[10];
void push(int value)
{
if(top == max - 1)
printf("\nStack is full");
else{
top++;
stack[top] = value;
}
}
void pop()
{
if (top == -1)
printf("\nCan't perform this operation");
else
top--;
}
void display()
{
if (top == -1)
printf("\nEmpty Stack\n");
else
{
int i = top;
printf("\nTop -> %d |", stack[i]);
i--;
while (i>=0)
{
printf(" %d |", stack[i]);
i--;
}
}
}
int main()
{
int choice, info;
do
{
printf("\n\n\nEnter Choice : \n");
printf("1.Push to stack\t\t2.Pop from stack\t3.Exit\nAns: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter value :");
scanf("%d", &info);
push(info);
display();
break;
case 2:
pop();
display();
break;
case 3:
return 0;
default:
printf("\nWrong Choice\n");
}
} while (1);
}
#include <stdio.h>
#include <stdlib.h>
// defining the node
typedef struct node
{
int info;
struct node *link;
} mynode;
mynode *top = NULL;
void push(int info)
{
mynode *new = (mynode *)malloc(sizeof(mynode));
new->info = info;
new->link = NULL;
if (top == NULL)
top = new;
else
{
new->link = top;
top = new;
}
}
void pop()
{
if (top == NULL)
printf("\n\nCan't perform this operation");
else
{
mynode *temp = top;
top = top->link;
free(temp);
}
}
void display()
{
if (top == NULL)
printf("\n\nEmpty Stack\n");
else
{
mynode *ptr = top;
printf("\n\nTop -> %d |", ptr->info);
while (ptr->link != NULL)
{
ptr = ptr->link;
printf(" %d |", ptr->info);
}
}
}
int main()
{
int choice, info;
do
{
printf("\n\n\nEnter Choice : \n");
printf("1.Push to stack\t\t2.Pop from stack\t3.Exit\nAns: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter value :");
scanf("%d", &info);
push(info);
display();
break;
case 2:
pop();
display();
break;
case 3:
return 0;
default:
printf("\nWrong Choice\n");
}
} while (1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment