Tuesday 6 March 2018

Array Queue using Arrays in C

#include<stdio.h>

int arr[10],front=-1,rear=-1,i;

void enqueue(int object){

 if(rear>=10)

  printf("\nNo enough space to enqueue");

 else if(front == -1 && rear == -1)

 {

 front=0;

 rear++;

  arr[rear]=object;

}

 else

 {
  arr[++rear]=object;
printf("\n %d just moved in to stack.",object);
 }

}

void dequeue(){

 if(front == -1 && rear==-1)

  printf("\nNo object to dequeue");

 else{

  printf("\n%d just moved out of queue",arr[front]);

  front++;

 }

}

void traverse(){

 printf("\n\nTraverse:");

    if(rear==-1)

     printf("\nNo Elements to display");

    else

     for(i=front;i<=rear;i++)

         printf("\n Queue[%d]=%d",i,arr[i]);

}

void main(){

 for(i=0;i<5;i++)

  enqueue(i);

 traverse();

 dequeue();

 dequeue();

 traverse();

 enqueue(6);

 traverse();

}

Delete After a Particular Element in Doubly Linked List

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

struct Node
{
    struct Node *prev;
    int data;
    struct Node *next;
}*head,*tail,*nn,*loc;
int insertValue()
{
    int value;
    printf("\nInsert Value:");
    scanf("%d",&value);
    return value;
}
int insertBeg()
{
    nn=malloc(sizeof(struct Node));
    nn->data=insertValue();
    nn->prev=NULL;
    if(head==NULL && tail==NULL)
    {
        head=nn;
        tail=nn;
        nn->next=NULL;
    }
    else
    {
        head->prev=nn;
        nn->next=head;
        head=nn;
    }
    return 0;
}
int deleteAfterElement()
{
    if(head==NULL && tail==NULL)
        return 1;
    int ele;
    printf("Enter Value To Delete:");
    scanf("%d",&ele);
    loc=head;
    while(loc->next!=NULL)
    {
        if(loc->next->data==ele){
            struct Node *temp;
            temp=loc->next;
            loc->next->next->prev=loc;
            loc->next=loc->next->next;
            free(temp->next);
        }
        loc=loc->next;
    }
    return 0;
}
int traverseF()
{
    if(head==NULL && tail==NULL)
        return 0;
    loc=head;
    while(loc!=NULL)
    {
        printf("\n%u <- %d -> %u",loc->prev,loc->data,loc->next);
        loc=loc->next;
    }
    return 0;
}
int main()
{
    insertBeg();
    insertBeg();
    insertBeg();
    insertBeg();
    traverseF();
    if(deleteAfterElement(33))
        printf("Error!No Values Entered");
    traverseF();
    return 0;
}

Wednesday 28 February 2018

Queue of Array in C

#include<stdio.h>
int arr[10],front=0,rear=-1,i;
void enqueue(int object){
if(rear>=10)
printf("No enough space to enqueue");
else
arr[++rear]=object;
}
void dequeue(){
if(rear==-1)
printf("No object to dequeue");
else{
printf("%d just moved out of queue",arr[front++]);
}
}
void traverse(){
    if(rear==-1)
    printf("No Elements to display");
    else
    for(i=front;i<=rear;i++)
        printf("\n Queue[%d]=%d",i,arr[i]); 
}
void main(){
for(i=0;i<5;i++)
enqueue(i);
traverse();

}

Most Simple Array Stack in C

Here goes the most simple Array Stack program in C:


#include<stdio.h>
#define MAX 50
int stack[MAX],top=-1,i;
void push(int item)
{
    if(top>=MAX) printf("\nStack Overflow!");
    else
        stack[++top]=item;
}
void pop()
{
    if(top==-1) printf("\nStack Underflow!");
    else
        printf("\n%d had been popped out",stack[top--]);
}
void traverse()
{
    if(top==-1) printf("\nNo Elements to display!");
    else
     for(i=0;i<=top;i++)
         printf("\n stack[%d]=%d",i,stack[i]); 
}
void main()
{
  for(i=0;i<50;i++)
     push(i);
    traverse();
    for(i=0;i<50;i++)
     pop();
    traverse();
}

Wednesday 21 February 2018

Doubly Linked List Operations using C

Doubly Link List

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct node{
struct node *prev;
int data;
struct node  *next;
}*head,*tail,*loc,*nn;

enterValue(){
int val;
printf("\nEnter Value:");
scanf("%d",&val);
return val;
}
memAlloc(){
nn=malloc(sizeof(struct node));
return 0;
}
insertB(){
memAlloc();
nn->data=enterValue();
nn->prev=NULL;
if(head==NULL && tail==NULL){
tail=nn;
nn->next=NULL;
head=nn;
}
else{
nn->next=head;
head->prev=nn;
head=nn;
}
return 0;
}

int insertL(){
memAlloc();
nn->data=enterValue();
nn->next=NULL;
if(head==NULL && tail==NULL){
head=nn;
tail=nn;
nn->prev=NULL;
}
else{
tail->next=nn;
nn->prev=tail;
tail=nn;
}
return 0;
}

insertAV(){

}
traverseF(){
loc=head;
printf("Forward Traverse:\n");
while(loc!=NULL){
printf("%u+%d+%u \n",loc->prev,loc->data,loc->next);
loc=loc->next;
}
return 0;
}

traverseB(){
loc=tail;
printf("Backward Traverse:\n");
while(loc!=NULL){
printf("%u+%d+%u \n",loc->prev,loc->data,loc->next);
loc=loc->prev;
}
}

int main(int argc, char *argv[]) {
int c;
header:
printf("\n1. Insert Before First Link");
printf("\n2. Insert After Last Link");
printf("\n3. Insert After Value");
printf("\n4. Traverse Forward");
printf("\n5. Traverse Backward");
printf("\n6. Exit");
printf("\nYour Choice:");
if(getch()=='1')
insertB(22);
else if(getch()=='2')
insertL(43);
else if(getch()=='3')
insertAV();
else if(getch()=='4')
traverseF();
else if(getch()=='5')
traverseB();
else if(getch()=='6')
goto footer;
else
{
 printf("Wrong Choice Please Enter Again!");
 goto header;
}
footer:
return 0;
}

Wednesday 24 January 2018

Basic Array Operations

Here are very basic operations on a linear array before pouring your hands in Data Stuructures, these list of operations on array would be very useful for you.
Main Menu
1. First option for inserting value
2. Second option consists of deletion of value by the index or by the address of value .
3. Third option let the user search the value in an array.
4. Fourth option lets the user reverse the elements in the array.
5. Fifth option lets the user rotate the elements in the array.
6. Sixth option lets the use shift the elements on either the right side or left side.
7. Seventh option lets the user count the number of occurences of element in the array.
8. Last option consists of displaying the address of the an element.

Link for Executable file: Link for Executable file
The source code is perfectly working in Codeblocks.


Thursday 21 September 2017

Accepting and Printing Sum of Prime Numbers in Range

Here is a c program for accepting the ranges & printing the prime numbers and their sum, here goes the program perfectly working in Devc++ 5.11:

#include<stdio.h>
int main(){
int a,i,j,flag,b,sum=0;
printf("Enter starting point:");
scanf("%d",&a);
i=a;
printf("Enter ending point:");
scanf("%d",&b);
printf("Prime Numbers are:\n");
while(i<b){
flag=1;
j=2;
while(j<i){
if(i%j==0)
flag=0;
j++;
}
if(flag==1){
printf("%d\n",i);
sum=sum+i;
}

i++;
}
printf("Total of prime number b/w range from %d to %d is %d",a,b,sum);
}
Program C file
Program Application File