Skip to content

Instantly share code, notes, and snippets.

@krishnakumarcn
Last active August 17, 2017 12:58
Show Gist options
  • Save krishnakumarcn/c2b0051131da439c8e6f69b530bbc763 to your computer and use it in GitHub Desktop.
Save krishnakumarcn/c2b0051131da439c8e6f69b530bbc763 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#define SIZE 5
struct Queue {
int arr[SIZE];
int front, rear;
};
typedef struct Queue Queue;
void enqueue(Queue*, int);
int overflow(Queue);
void display(Queue*);
int dequeue(Queue*);
int underflow(Queue);
int main() {
int choice;
int value;
Queue Q;
Q.front = -1;
Q.rear = -1;
printf("\n----\n1.Enqueue\n2.Dequeue\n3.Display\n-----\n");
do{
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
if (overflow(Q)) {
printf("\nQueue Overflow ! Dequeue some items..");
break;
}
printf("Enter the character: ");
scanf("%d", &value);
enqueue(&Q,value);
printf("\nEnqueued!: %d\n [TEST: index] front:%d rear:%d",value,Q.front,Q.rear);
break;
case 2:
if(underflow(Q)){
printf("\nQueue Underflow ! Enqueue some items..");
break;
}
value=dequeue(&Q);
printf("\nDequeued!: %d\n [TEST: index] front:%d rear:%d",value,Q.front,Q.rear);
break;
case 3:
display(&Q);
break;
}
}while(1);
}
int overflow(Queue Q) {
if ((Q.rear + 1)%SIZE == Q.front)
return 1;
return 0;
}
int underflow(Queue Q){
if(Q.rear==-1){
return 1;
}
return 0;
}
void enqueue(Queue *Q,int value) {
Q->arr[(++Q->rear) % SIZE] = value;
if(Q->front==-1)
Q->front=0;
}
void display(Queue *Q){
if(Q->rear==-1){
printf("Queue Empty!");
return;
}
for(int i=Q->front;i<=Q->rear;i++){
printf("%d\t",Q->arr[i%SIZE]);
}
}
int dequeue(Queue *Q){
int val=Q->arr[(Q->front)%SIZE ];
if(Q->front == Q->rear){
Q->front=Q->rear=-1;
}
else{
Q->front=Q->front+1;
}
return val;
}
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
#define INT_MAX 2147483647 //for UNIX
struct node {
int data;
struct node* next;
};
typedef struct node node;
/* Definitions */
node* init(node*);
node* insertAt(node*, int, int);
void display(node*);
node* deleteAt(node*, int);
int retrieveFrom(node*, int);
int locateElement(node*, int);
int firstElement(node*);
int lastElement(node*);
int nextPrevElement(node*,int,int);
int main() {
node *head = NULL;
head = init(head);
int choice;
do {
printf("\n-----Menu------\n1.Initialize\\Clear\n2.Insert at a position"
"\n3.Delete from position\n4.Retrieve From Position\n5.Locate Element\n"
"6.First Element\n7.Last Element\n8.Next Element\n9.Previous Element\n10.Display"
"\nEnter your choice: ");
scanf("%d", &choice);
int tempPos, tempVal;
switch (choice) {
case 1:
head = init(head);
printf("List initialized to zeros.\n");
break;
case 2:
printf("Enter the position and value: ");
scanf("%d%d", &tempPos, &tempVal);
head = insertAt(head, tempPos, tempVal);
break;
case 3:
printf("Enter the position to delete: ");
scanf("%d", &tempPos);
head = deleteAt(head, tempPos);
break;
case 4:
printf("Enter the position for Retrieval: ");
scanf("%d", &tempPos);
tempVal = retrieveFrom(head, tempPos);
if (tempVal == INT_MAX) {
break;
}
printf("Retrived: %d\n", tempVal);
break;
case 5:
printf("Enter the element to search: ");
scanf("%d", &tempVal);
tempPos = locateElement(head, tempVal);
if (tempPos == -1) {
printf("\nNot Found!!");
}
else {
printf("\nFound at position: %d", tempPos);
}
break;
case 6:
if (head->data == 0) {
printf("List is Empty!");
break;
}
printf("First element in the list is: %d", firstElement(head));
break;
case 7:
if (head->data == 0) {
printf("List is Empty!");
break;
}
printf("Last element in the list is: %d", lastElement(head));
break;
case 8:
printf("Enter the element: ");
scanf("%d", &tempVal);
tempPos = nextPrevElement(head, tempVal, 1);
if (tempPos == INT_MAX) {
printf("Error: No such value!");
break;
}
printf("Next element after %d is: %d", tempVal, tempPos);//for next, 3rd param is 1
break;
case 9:
printf("Enter the element: ");
scanf("%d", &tempVal);
tempPos = nextPrevElement(head, tempVal, 0);
if (tempPos == INT_MAX) {
printf("Error: No such value!");
break;
}
printf("Previous element before %d is: %d", tempVal, tempPos);//for next, 3rd param is 1
break;
case 10:
display(head);
break;
}
//scanf("%c", &ch); //for clearing buffer
//printf("Do you want to continue..?(Y/N): ");
//scanf("%c", &ch);
} while (1);/*ch == 'Y' || ch == 'y');*/
getchar();
}
node* init(node* head) {
head = calloc(MAX, sizeof(int));
head->next = NULL;
head->data = 0;
return head;
}
node* insertAt(node* head, int pos, int val) {
node *ptr = head;
if (pos > head->data) {
printf("List only contain %d elements.\n", head->data);
return head;
}
for (int i = 0; i < pos; i++) {
ptr = ptr->next;
}
node *newNode = malloc(sizeof(node));
newNode->data = val;
newNode->next = NULL;
head->data++;
newNode->next = ptr->next;
ptr->next = newNode;
return head;
}
node* deleteAt(node* head, int pos) {
if (pos >= head->data) {
printf("List only contain %d elements.\n", head->data);
return head;
}
node* ptr = head;
//printf("List[%d] : %d is deleted.", pos,);
for (int i = 0; i < pos; i++) {
ptr = ptr->next;
}
printf("List[%d] : %d is deleted", pos, ptr->next->data);
ptr->next = ptr->next->next;
head->data--;
return head;
}
void display(node *head) {
node* ptr = head->next;
while (ptr != NULL) {
printf("%d --> ", ptr->data);
ptr = ptr->next;
}
printf("NULL");
}
int retrieveFrom(node *head, int pos) {
node *ptr = head->next;
if (pos >= head->data) {
printf("List only contain %d elements.\n", head->data);
return INT_MAX;
}
for (int i = 0; i < pos; ptr = ptr->next, i++);
return ptr->data;
}
int locateElement(node* head, int val) {
node *ptr = head->next;
for (int i = 0; i < head->data; i++, ptr = ptr->next) {
if (ptr->data == val) {
return i;
}
}
return -1;
}
int firstElement(node *head) {
return head->next->data;
}
int lastElement(node *head) {
node* ptr = head->next;
while (ptr->next != NULL) {
ptr = ptr->next;
}
return ptr->data;
}
int nextPrevElement(node *head, int val, int flag) {
int currPos;
node *ptr = head->next;
currPos = locateElement(head, val);
if (currPos == -1) {
return INT_MAX;
}
if (flag == 1) {
if (currPos + 1 >= head->data)
return INT_MAX;
for (int i = 0; i < currPos + 1; i++, ptr = ptr->next);
return ptr->data;
}
else {
if (currPos - 1<0)
return INT_MAX;
for (int i = 0; i < currPos - 1; i++, ptr = ptr->next);
return ptr->data;
}
}
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
#define MAX 100
int* init(int*);
int* insertAt(int*, int, int);
void display(int*);
int* deleteAt(int*, int);
int retrieveFrom(int*, int);
int locateElement(int*, int);
int firstElement(int*);
int lastElement(int*);
int nextPrevElement(int*,int,int);
int count = 0;
int main(){
int *arr = calloc(MAX, sizeof(int));
//initialize arr to zero,
// first element gives total no.of elements
int choice;
arr = init(arr);
char ch = 'y';
do{
printf("\n-----Menu------\n1.Initialize\\Clear\n2.Insert at a position"
"\n3.Delete from position\n4.Retrieve From Position\n5.Locate Element\n"
"6.First Element\n7.Last Element\n8.Next Element\n9.Previous Element\n10.Display"
"\nEnter your choice: ");
scanf("%d", &choice);
int tempPos, tempVal;
switch (choice){
case 1:
arr = init(arr);
printf("List initialized to zeros.\n");
break;
case 2:
printf("Enter the position and value: ");
scanf("%d%d", &tempPos, &tempVal);
arr = insertAt(arr, tempPos, tempVal);
break;
case 3:
printf("Enter the position to delete: ");
scanf("%d", &tempPos);
arr = deleteAt(arr, tempPos);
break;
case 4:
printf("Enter the position for Retrieval: ");
scanf("%d", &tempPos);
tempVal = retrieveFrom(arr, tempPos);
printf("Retrived: %d\n", tempVal);
break;
case 5:
printf("Enter the element to search: ");
scanf("%d", &tempVal);
tempPos = locateElement(arr, tempVal);
if (tempPos == -1){
printf("\nNot Found!!");
}
else{
printf("\nFound at position: %d", tempPos);
}
break;
case 6:
if (count == 0)
printf("List is Empty!");
printf("First element in the list is: %d", firstElement(arr));
break;
case 7:
if (count == 0)
printf("List is Empty!");
printf("Last element in the list is: %d", lastElement(arr));
break;
case 8:
printf("Enter the element: ");
scanf("%d",&tempVal);
tempPos=nextPrevElement(arr,tempVal,1);
if(tempPos==INT_MAX){
printf("Error: No such value!");
break;
}
printf("Next element after %d is: %d",tempVal,tempPos);//for next, 3rd param is 1
break;
case 9:
printf("Enter the element: ");
scanf("%d",&tempVal);
tempPos=nextPrevElement(arr,tempVal,0);
if(tempPos==INT_MAX){
printf("Error: No such value!");
break;
}
printf("Previous element before %d is: %d",tempVal,tempPos);//for next, 3rd param is 1
break;
case 10:
display(arr);
}
//scanf("%c", &ch); //for clearing buffer
//printf("Do you want to continue..?(Y/N): ");
//scanf("%c", &ch);
} while (1);/*ch == 'Y' || ch == 'y');*/
}
int* init(int *arr){
arr = calloc(MAX, sizeof(int));
count = 0;
return arr;
}
void display(int *arr){
if (count == 0){
printf("List Empty!!");
return;
}
printf("\n-------------\n");
for (int i = 0; i < count; i++)
printf("%d\t", arr[i]);
printf("\nCount: %d\n--------------------\n", count);
}
int* insertAt(int *arr, int pos, int val){
if (pos > count){
printf("___________List only contain %d elements.\n", count);
return arr;
}
for (int i = count; i > pos; i--){
arr[i] = arr[i - 1];
}
arr[pos] = val;
count++;
return arr;
}
int* deleteAt(int* arr, int pos){
if (pos >= count){
printf("List only contain %d elements.\n", count);
return arr;
}
printf("Array[%d] : %d is deleted.", pos, arr[pos]);
for (int i = pos; i < count - 1; i++){
arr[i] = arr[i + 1];
}
count--;
return arr;
}
int retrieveFrom(int *arr, int pos){
return arr[pos];
}
int locateElement(int* arr, int val){
for (int i = 0; i < count; i++){
if (arr[i] == val){
return i;
}
}
return -1;
}
int firstElement(int *arr){
return arr[0];
}
int lastElement(int *arr){
return arr[count - 1];
}
int nextPrevElement(int *arr,int val,int flag){
int currPos;
currPos=locateElement(arr,val);
if(currPos==-1){
return INT_MAX;
}
if(flag==1){
if(currPos+1>=count)
return INT_MAX;
return arr[currPos+1];
}
else{
if(currPos-1<0)
return INT_MAX;
return arr[currPos-1];
}
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node {
int data;
struct node *left;
struct node* right;
};
typedef struct node node;
node* createBinTree(node*);
node* insert(node*,int);
void display(node*);
int main() {
node *header=NULL;
header=createBinTree(header);
display(header);
}
node* createBinTree(node* header) {
FILE *fp = fopen("testfile.txt", "r");
char s[100];
while( fgets(s,sizeof(s),fp)!=NULL ) {
int value=atoi(s);
// printf("%d ", value);
header=insert(header,value);
}
return header;
}
node* insert(node* header,int value){
if(header==NULL){
//printf("Inside null: ");
header=malloc( sizeof(node) );
header->left=NULL;
header->right=NULL;
header->data=value;
return header;
}
node *newNode=malloc(sizeof(node));
newNode->data=value;
newNode->left=NULL;
newNode->right=NULL;
node *ptr=header;
while(ptr->left !=NULL && ptr->right !=NULL ){
while(ptr->data > value && ptr->left ){
ptr=ptr->left;
}
while(ptr->data <= value && ptr->right){
ptr=ptr->right;
}
}
if( ptr->data > value ){
}
}
void display(node* header){
if(header==NULL)
return;
printf("%d",header->data);
display(header->left);
display(header->right);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment