Tuesday 21 August 2012

program to implement circular queue

#include<stdio.h>
#define MAX 10
int que[MAX];
int front=-1;
int rear=-1;
void insert();
void del();
void disp();
 main()
{
    int ch;
    while(1)
    {
    printf("\n..................MENU................\n");
    printf("select your choice\n");
    printf("1.insert\n");
    printf("2.delete\n");
    printf("3.display\n");
    printf("4.exit\n");
    printf("enter your choice:");
    scanf("%d",&ch);
    switch(ch)
    {

        case 1:
        insert();
        break;
        case 2:
        del();
        break;
        case 3:
        disp();
        break;
        case 4:
        exit(1);
        default :
        printf("invalid choice\n");

         }
    }
}
void insert()
{
    int val;
    if((front==0&&rear==MAX-1)||(front==rear+1))
    {
    printf("queue overflow\n");
    return;
    }
    if(front==-1)
    {
        front=0;
        rear=0;

    }
    else
        if(rear==MAX-1)
        {
            rear=0;
        }
        else
        rear=rear+1;
        printf("enter the element for insertion in queue:");
        scanf("%d",&val);
        que[rear]=val;

}
void del()
{
    if(front==-1)
    {
        printf("queue underflow\n");
        return;

    }
    printf("element deleted from queue is=%d",que[front]);
    if(front==rear)
    {
        front=-1;
        rear=-1;
    }
    else
        if(front==MAX-1)
        {
            front=0;
        }
        else
        front=front+1;

}
void disp()
{
    int f=front,r=rear;
    if(front==-1)
    {
        printf("queue is empty\n");
        return;

    }
    printf("queue elements:\n");
    if(f<=r)
    while(f<=r)
    {
        printf("%d\t",que[f]);
        f++;
    }
    else
    {
        while(f<=MAX-1)
        {
            printf("%d\t",que[f]);
            f++;

        }
        f=0;
        while(f<=r)
        {

            printf("%d\t",que[f]);
            f++;
        }
    }
}



                         OUTPUT

..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:1
enter the element for insertion in queue:1

..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:1
enter the element for insertion in queue:2

..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:1
enter the element for insertion in queue:3

..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:3
queue elements:
1       2       3
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:2
element deleted from queue is=1
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:2
element deleted from queue is=2
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:2
element deleted from queue is=3
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:1
enter the element for insertion in queue:8

..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:3
queue elements:
8
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:4

Process returned 1 (0x1)   execution time : 53.552 s
Press any key to continue.

program to implement circular linkedlist

#include<malloc.h>
void disp();
void create(int data);
void insertbeg(int data);
void insertbet(int data,int pos);
void del(int data);
void count();
void search(int data);
struct node
{
    int info;
    struct node *link;

}*last=NULL;
main()
{
int m,pos,i,n,ch;
while(1)
{
printf("\n\n\n........MENU..........\n");
printf("\n1.create list\n");
printf("2.dispaly\n");
printf("3.insert at bigining\n");
printf("4.insert in between\n");
printf("5.delete\n");
printf("6.count\n");
printf("7.search\n");
printf("8.exit\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)

{
    case 1:
    printf("how many nodes:");
    scanf("%d",&n);
    printf("enter the element:\n");
    for(i=0;i<n;i++)
    {

        scanf("%d",&m);
        create(m);
    }
    break;
    case 2:
    disp();
    break;
    case 3:
    printf("enter element:");
    scanf("%d",&m);
    insertbeg(m);
    break;
    case 4:
    printf("enter element:");
    scanf("%d",&m);
    printf("enter the position:");
    scanf("%d",&pos);
    insertbet(m,pos);
    break;
    case 5:
    printf("enter the element:");
    scanf("%d",&m);
    del(m);
    break;

    case 6:
    count();
    break;
    case 7:
    printf("enter the element you want to search:");
    scanf("%d",&m);
    search(m);
    break;
    case 8:
    exit(1);
}

}

}

void create(int data)
{
    struct node *q,*tmp;
    tmp=malloc(sizeof(struct node ));
    tmp->info=data;

    if(last==NULL)
    {
        last=tmp;
        tmp->link=last;

    }
    else
    {
        tmp->link=last->link;
        last->link=tmp;
        last=tmp;

    }
    return;
}
void disp()
{
    struct node *q;
    if(last==NULL)
    {
        printf("list is empty\n");
        return;

    }
    printf("the list is:\n");
    q=last->link;
    while(q!=last)
    {
        printf("%d-->",q->info);
        q=q->link;

    }printf("%d",last->info);
}
void insertbeg(int data)
{

    struct node *tmp;
    tmp=malloc(sizeof(struct node));
    tmp->info=data;
    tmp->link=last->link;
    last->link=tmp;

}
void insertbet(int data,int pos)
{
    struct node *q,*tmp;
    int i;
    q=last;
    for(i=0;i<pos-1;i++)
    {

        q=q->link;
        if(q->link==last)
         {
        printf("there is less than %d element\n",pos);
        return;
         }
    }
    tmp=malloc(sizeof(struct node));
    tmp->info=data;
    tmp->link=q->link;
    q->link=tmp;
}

void del(int data)
{

    struct node *tmp,*q;
    if((last->link==last)&&(last->info==data))
    {
        tmp=last;
        last=NULL;
        free(tmp);

    }
    q=last->link;
    if(q->info==data)
    {
        tmp=q;
        last->link=q->link;
        free(tmp);
        return;
    }
    q=last;
    while(q->link!=last)
    {
        if(q->link->info==data)
        {
            tmp=q->link;
            q->link=tmp->link;
            free(tmp);

        }
        q=q->link;

    }
    if(q->link->info==data)
    {
        tmp=q->link;
        q->link=last->link;
        free(tmp);
        last=q;
        return;
    }

}
void count()
{
    struct node *q;
    int cnt=0;
    if(last==NULL)
    printf("list is empty\n");
    else
    {
        q=last;
        do
        {

            cnt=cnt+1;
            q=q->link;
        }while(q!=last);
        printf("number of items=%d\n",cnt);

        }
    }


void search(int data)
{
    struct node *q;
    if(last==NULL)
    printf("list is empty\n");
    else
    {
        q=last;
        do
        {
            if(q->info==data)
            {
            printf("element is fount\n");
            return;
            }
            q=q->link;
    }while(q!=last);
printf("searched element is not found\n");
return;
}
}
#include<malloc.h>
void disp();
void create(int data);
void insertbeg(int data);
void insertbet(int data,int pos);
void del(int data);
void count();
void search(int data);
struct node
{
    int info;
    struct node *link;

}*last=NULL;
main()
{
int m,pos,i,n,ch;
while(1)
{
printf("\n\n\n........MENU..........\n");
printf("\n1.create list\n");
printf("2.dispaly\n");
printf("3.insert at bigining\n");
printf("4.insert in between\n");
printf("5.delete\n");
printf("6.count\n");
printf("7.search\n");
printf("8.exit\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)

{
    case 1:
    printf("how many nodes:");
    scanf("%d",&n);
    printf("enter the element:\n");
    for(i=0;i<n;i++)
    {

        scanf("%d",&m);
        create(m);
    }
    break;
    case 2:
    disp();
    break;
    case 3:
    printf("enter element:");
    scanf("%d",&m);
    insertbeg(m);
    break;
    case 4:
    printf("enter element:");
    scanf("%d",&m);
    printf("enter the position:");
    scanf("%d",&pos);
    insertbet(m,pos);
    break;
    case 5:
    printf("enter the element:");
    scanf("%d",&m);
    del(m);
    break;

    case 6:
    count();
    break;
    case 7:
    printf("enter the element you want to search:");
    scanf("%d",&m);
    search(m);
    break;
    case 8:
    exit(1);
}

}

}

void create(int data)
{
    struct node *q,*tmp;
    tmp=malloc(sizeof(struct node ));
    tmp->info=data;

    if(last==NULL)
    {
        last=tmp;
        tmp->link=last;

    }
    else
    {
        tmp->link=last->link;
        last->link=tmp;
        last=tmp;

    }
    return;
}
void disp()
{
    struct node *q;
    if(last==NULL)
    {
        printf("list is empty\n");
        return;

    }
    printf("the list is:\n");
    q=last->link;
    while(q!=last)
    {
        printf("%d-->",q->info);
        q=q->link;

    }printf("%d",last->info);
}
void insertbeg(int data)
{

    struct node *tmp;
    tmp=malloc(sizeof(struct node));
    tmp->info=data;
    tmp->link=last->link;
    last->link=tmp;

}
void insertbet(int data,int pos)
{
    struct node *q,*tmp;
    int i;
    q=last;
    for(i=0;i<pos-1;i++)
    {

        q=q->link;
        if(q->link==last)
         {
        printf("there is less than %d element\n",pos);
        return;
         }
    }
    tmp=malloc(sizeof(struct node));
    tmp->info=data;
    tmp->link=q->link;
    q->link=tmp;
}

void del(int data)
{

    struct node *tmp,*q;
    if((last->link==last)&&(last->info==data))
    {
        tmp=last;
        last=NULL;
        free(tmp);

    }
    q=last->link;
    if(q->info==data)
    {
        tmp=q;
        last->link=q->link;
        free(tmp);
        return;
    }
    q=last;
    while(q->link!=last)
    {
        if(q->link->info==data)
        {
            tmp=q->link;
            q->link=tmp->link;
            free(tmp);

        }
        q=q->link;

    }
    if(q->link->info==data)
    {
        tmp=q->link;
        q->link=last->link;
        free(tmp);
        last=q;
        return;
    }

}
void count()
{
    struct node *q;
    int cnt=0;
    if(last==NULL)
    printf("list is empty\n");
    else
    {
        q=last;
        do
        {

            cnt=cnt+1;
            q=q->link;
        }while(q!=last);
        printf("number of items=%d\n",cnt);

        }
    }


void search(int data)
{
    struct node *q;
    if(last==NULL)
    printf("list is empty\n");
    else
    {
        q=last;
        do
        {
            if(q->info==data)
            {
            printf("element is fount\n");
            return;
            }
            q=q->link;
    }while(q!=last);
printf("searched element is not found\n");
return;
}
}

                                        OUTPUT

........MENU..........

1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:1
how many nodes:5
enter the element:
1
2
3
4
5



........MENU..........

1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:2
the list is:
1-->2-->3-->4-->5


........MENU..........

1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:3
enter element:6



........MENU..........

1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:2
the list is:
6-->1-->2-->3-->4-->5


........MENU..........

1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:4
enter element:7
enter the position:5



........MENU..........

1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:2
the list is:
6-->1-->2-->3-->7-->4-->5


........MENU..........

1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:5
enter the element:7



........MENU..........

1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:2
the list is:
6-->1-->2-->3-->4-->5


........MENU..........

1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:6
number of items=6



........MENU..........

1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:7
enter the element you want to search:8
searched element is not found



........MENU..........

1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:8

Process returned 1 (0x1)   execution time : 94.079 s
Press any key to continue.

program to implement stack using linked list


#include<stdio.h>
#include<malloc.h>
void disp();
void push(int data);
void pop();
struct node
{
    int info;
    struct node *link;

}*top=NULL;
main()
{
int ch,i,m;
while(1)
{
printf("\n..............MENU...........\n");
printf("\n1.push element\n");
printf("2.pop element\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)

{
    case 1:
      printf("\nenter the element:");
    scanf("%d",&m);
    push(m);
    break;
    case 2:
    pop();
    break;
    case 3:
    disp();
    break;
    case 4:
    exit(1);
}

}

}

void push(int data)
{
    struct node *tmp;

    tmp=malloc(sizeof(struct node));
    tmp->info=data;
    tmp->link=top;
    top=tmp;
}
void pop()
{
    struct node *tmp;
    tmp=malloc(sizeof(struct node));

    if(top==NULL)
    printf("stack is empty\n");
    else
    {
      tmp=top;
      printf("popped item=%d\n",tmp->info);
        top=top->link;
        tmp->link=NULL;
        free(tmp);
    }


}
void disp()
{

    struct node *tmp;
    tmp=top;
    if(top==NULL)
    printf("stack is empty\n");
    else
    {

      printf("stack elements\n");
      while(tmp!=NULL)
      {
          printf("%d\t",tmp->info);
          tmp=tmp->link;
      }
    }
}


                    OUTPUT


..............MENU...........

1.push element
2.pop element
3.display
4.exit
enter your choice:1

enter the element:1

..............MENU...........

1.push element
2.pop element
3.display
4.exit
enter your choice:1

enter the element:2

..............MENU...........

1.push element
2.pop element
3.display
4.exit
enter your choice:1

enter the element:3

..............MENU...........

1.push element
2.pop element
3.display
4.exit
enter your choice:3
stack elements
3       2       1
..............MENU...........

1.push element
2.pop element
3.display
4.exit
enter your choice:2
popped item=3

..............MENU...........

1.push element
2.pop element
3.display
4.exit
enter your choice:2
popped item=2

..............MENU...........

1.push element
2.pop element
3.display
4.exit
enter your choice:2
popped item=1

..............MENU...........

1.push element
2.pop element
3.display
4.exit
enter your choice:2
stack is empty

..............MENU...........

1.push element
2.pop element
3.display
4.exit
enter your choice:4

Process returned 1 (0x1)   execution time : 26.988 s
Press any key to continue.

program to implement stack using array

#include<stdio.h>
#define MAX 10
void push(int stack[MAX],int *top,int val);
void pop(int stack[MAX],int *top,int *val);
void tra(int stack[MAX],int *top);
main()
{
    int stack[MAX],top=-1,n,val,ch;
    while(1)
    {
    printf("\n..............MENU..............\n");
    printf("\nselect your choice\n");
    printf("1.push\n");
    printf("2.pop\n");
    printf("3.traverse\n");
    printf("4.exit\n");
    printf("enter your choice:");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1:
        printf("enter the element to be pushed:");
        scanf("%d",&val);
        push(stack,&top,val);
        break;
        case 2:
        pop(stack,&top,&val);

        break;
        case 3:
        tra(stack,&top);
        break;
        case 4:
        exit(1);
        default :
        printf("invalid choice\n");

    }
}}

void push(int stack[MAX],int *top,int val)
{
    if(*top<MAX)
    {
        *top=*top+1;
        stack[*top]=val;
    }
    else
    {
        printf("the stack is full can not push a value\n");
        return;
    }
}
void pop(int stack[MAX],int *top,int *val)
{
    if(*top>=0)
    {
        *val=stack[*top];
        *top=*top-1;
        printf("the value poped is %d",*val);
    }
    else
    {
        printf("the stack is empty \n");
        return;
    }
}
void tra(int stack[MAX],int *top)
{
    int i;
    if(*top==-1)
    {
        printf("the stack is empty\n");
        return;
    }
    else
    {
        printf("the elements in the stack are\n");
        for(i=*top;i>=0;i--)
        printf("%d\t",stack[i]);
    }
}

                               OUTPUT

..............MENU..............

select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:1
enter the element to be pushed:1

..............MENU..............

select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:1
enter the element to be pushed:2

..............MENU..............

select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:1
enter the element to be pushed:3

..............MENU..............

select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:3
the elements in the stack are
3       2       1
..............MENU..............

select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:2
the value poped is 3
..............MENU..............

select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:2
the value poped is 2
..............MENU..............

select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:2
the value poped is 1
..............MENU..............

select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:2
the stack is empty

..............MENU..............

select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:4

Process returned 1 (0x1)   execution time : 32.152 s
Press any key to continue.

program to implement queue using linked list

#include<stdio.h>
#include<malloc.h>
void disp();
void pop();
void add();
struct node
{
    int info;
    struct node *link;

}*rear,*front=NULL;
main()
{
int ch;
while(1)
{
printf("\n.............MENU..............\n");
printf("\n1.push element to queue\n");
printf("2.dispaly\n");
printf("3.delete element from queue\n");
printf("4.exit\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)

{
    case 1:
    add();
    break;
    case 2:
    disp();
    break;
    case 3:
    pop();
    break;
    case 4:
    exit(1);
}

}

}

void add()
{
    struct node *tmp;
    int data;
    printf("enter the element:");
    scanf("%d",&data);
    tmp=malloc(sizeof(struct node ));
    tmp->info=data;
    tmp->link=NULL;
    if(front==NULL)

        front=tmp;
     else
        rear->link=tmp;
        rear=tmp;
}
void disp()
{
    struct node *tmp;

    if(front==NULL)
    printf("the queue is empty\n");
    else
    {
        printf("the elements:\n");
      tmp=front;

    while(tmp!=rear)
    {

        printf("<--%d",tmp->info);
        tmp=tmp->link;
    }
        printf("<-%d\n",tmp->info);
    }
}
void pop()
{

    struct node *tmp;
    if(front==NULL)
    printf("underflow\n");
    else
    {

      tmp=malloc(sizeof(struct node));
      tmp=front;
      printf("\ndeleted element is=%d",tmp->info);
      front=front->link;
      free(tmp);
    }
}

                                        OUTPUT
.............MENU..............

1.push element to queue
2.dispaly
3.delete element from queue
4.exit
enter your choice:1
enter the element:1

.............MENU..............

1.push element to queue
2.dispaly
3.delete element from queue
4.exit
enter your choice:1
enter the element:2

.............MENU..............

1.push element to queue
2.dispaly
3.delete element from queue
4.exit
enter your choice:1
enter the element:3

.............MENU..............

1.push element to queue
2.dispaly
3.delete element from queue
4.exit
enter your choice:2
the elements:
<--1<--2<-3

.............MENU..............

1.push element to queue
2.dispaly
3.delete element from queue
4.exit
enter your choice:3

deleted element is=1
.............MENU..............

1.push element to queue
2.dispaly
3.delete element from queue
4.exit
enter your choice:3

deleted element is=2
.............MENU..............

1.push element to queue
2.dispaly
3.delete element from queue
4.exit
enter your choice:3

deleted element is=3
.............MENU..............

1.push element to queue
2.dispaly
3.delete element from queue
4.exit
enter your choice:3
underflow

.............MENU..............

1.push element to queue
2.dispaly
3.delete element from queue
4.exit
enter your choice:4

Process returned 1 (0x1)   execution time : 48.970 s
Press any key to continue.

Monday 20 August 2012

program to implement queue using array


#include<stdio.h>
#define MAX 10
int que[MAX];
int rear=-1,front=-1;
void push();
void pop();
void tra();
main()
{
    int ch;
    while(1)
    {
    printf("\n..............MENU..............\n");
    printf("\nselect your choice\n");
    printf("1.insert\n");
    printf("2.delete\n");
    printf("3.dispaly\n");
    printf("4.exit\n");
    printf("enter your choice:");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1:

        insert();
        break;
        case 2:
        del();

        break;
        case 3:
        disp();
        break;
        case 4:
        exit(1);
        default :
        printf("invalid choice\n");

    }
}}

void insert()
{
    int val;
    if(rear==MAX-1)
    {
        printf("queue overflow\n");
        return;
    }
    else
    {
        if(front==-1)
        front=0;

      printf("enter the element to be inserted:");
        scanf("%d",&val);
    rear++;
    que[rear]=val;
    }
}
void del()
{
    if(front==-1 || front>rear)
    {

        printf("queue underflow");
        return;
    }
    else
    {
        printf("element deleted from the queue is=%d",que[front]);
        front++;
    }
}
void disp()
{
    int i;
    if(front==-1 || front>rear)
    {
        printf("queue is empty\n");
        return;
    }
    else
    {
        printf("the elements in the queue are\n");
        for(i=front;i<=rear;i++)
        printf("%d\t",que[i]);
    }
}

                                     OUTPUT

..............MENU..............

select your choice
1.insert
2.delete
3.dispaly
4.exit
enter your choice:1
enter the element to be inserted:1

..............MENU..............

select your choice
1.insert
2.delete
3.dispaly
4.exit
enter your choice:1
enter the element to be inserted:2

..............MENU..............

select your choice
1.insert
2.delete
3.dispaly
4.exit
enter your choice:1
enter the element to be inserted:3

..............MENU..............

select your choice
1.insert
2.delete
3.dispaly
4.exit
enter your choice:3
the elements in the queue are
1       2       3
..............MENU..............

select your choice
1.insert
2.delete
3.dispaly
4.exit
enter your choice:2
element deleted from the queue is=1
..............MENU..............

select your choice
1.insert
2.delete
3.dispaly
4.exit
enter your choice:2
element deleted from the queue is=2
..............MENU..............

select your choice
1.insert
2.delete
3.dispaly
4.exit
enter your choice:2
element deleted from the queue is=3
..............MENU..............

select your choice
1.insert
2.delete
3.dispaly
4.exit
enter your choice:2
queue underflow
..............MENU..............

select your choice
1.insert
2.delete
3.dispaly
4.exit
enter your choice:4

Process returned 1 (0x1)   execution time : 36.864 s
Press any key to continue.