Friday, December 22, 2017

Resignation Letter From the Post of Assistant Professor

To,
The Registrar, Vignan University
Vadlamudi, Guntur-52213

            Through HoD, Dept. of Computer Science & Engineering, Vignan University, Guntur.
From:
Manoj Kumar Patra
Asst. Professor, Dept. of CSE.
Vignan University. Vadlamudi
Date: 14/12/2017
Dear Sir,
I am writing this letter to inform you that I am submitting my resignation from the post of Assistant Professor. As required by my employment, please consider this letter as a notice period for a period of ONE month, effective today. My last day at work will be 14/01/18.
I am resigning as I have got the much waited opportunity to pursue PhD from NIT Rourkela.
I had a memorable time at Vignan University. All the faculties and students were supportive and helped me in doing my work. It is a tough decision for me to submit my resignation and I hope to continue personal relations with all of you. Please accept my heartfelt thanks for the opportunities of professional and personal development that Vignan University provided during last one and half year. I wish the management and the staff of Vignan every success in all their future ventures.

Thanking you,
Yours Sincerely,
Manoj Kumar Patra

Emp Id: 01376

Saturday, October 21, 2017

Program to Implement Addition, Subtraction, Multiplication and Division of Complex Number in C

/*==========================================================
**
**  File Name     : complex_no.c
**  Creation Date : Sat 21 Oct 2017 08:58:51 PM IST
**  Last Modified : Sat 21 Oct 2017 10:44:13 PM IST
**  Compiler      : gcc
**  Author        : Manoj Kumar Patra, Asst. Professor
**  Organization  : Vignan University, Guntur, India.
**
**==========================================================*/

#include<stdio.h>
#include<stdlib.h>
struct complex
{
    int real;
    int img;
};

void add(struct complex n1, struct complex n2);
void sub(struct complex n1, struct complex n2);
void mul(struct complex n1, struct complex n2);
void division(struct complex n1, struct complex n2);

int main()
{
    printf("\n= = = = Arithemetic Operations on Complex number in C = = = =\n\n");
    int choice;
    struct complex n1, n2;
    printf("Enter the real part of first complex number: ");
    scanf("%d",&n1.real);
    printf("Enter the imag part of first complex number: ");
    scanf("%d",&n1.img);
    printf("Enter the real part of second complex number: ");
    scanf("%d",&n2.real);
    printf("Enter the imag part of second complex number: ");
    scanf("%d",&n2.img);
    printf("= - - - - - - - - - - - - - - - - - - - - - - - - - - - - - =\n");
    if(n1.img>=0)
            printf("First complex number is = %d+%di\n", n1.real, n1.img);
    else
            printf("First complex number is = %d%di\n", n1.real, n1.img);
    if(n2.img>=0)
            printf("Second complex number is = %d+%di\n", n2.real, n2.img);
    else
            printf("Second complex number is = %d%di\n", n2.real, n2.img);
    printf("= - - - - - - - - - - - - - - - - - - - - - - - - - - - - - =\n");

    while(1)
    {        
        printf("\n1. Addition\n");
        printf("2. Subtraction\n");
        printf("3. Multiplication\n");
        printf("4. Division\n");
        printf("5. Quit\n");
        printf("Enter your choice (1-5): ") ;
        scanf("%d", &choice);

        switch(choice)
        {
            case 1:
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                add(n1,n2);
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                break;
            case 2:
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                sub(n1,n2);
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                break;
            case 3:
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                mul(n1,n2);
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                break;
            case 4:
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                division(n1,n2);
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                break;
            case 5:
                printf("Program Terminated\n");
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
                return;
            default :
                printf("Oops.. Wrong choice! Select Again\n");
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
        }
    }
}

void add(struct complex n1, struct complex n2)
{
    struct complex res;
    res.real = n1.real+n2.real;
    res.img = n1.img+n2.img;
    if(res.img>=0)
            printf("Addition of two complex numbers is = %d+%di\n", res.real, res.img);
    else
            printf("Addition of two complex numbers is = %d%di\n", res.real, res.img);
}

void sub(struct complex n1, struct complex n2)
{
    struct complex res;
    res.real = n1.real-n2.real;
    res.img = n1.img-n2.img;
    if(res.img>=0)
            printf("Subtraction of two complex numbers is = %d+%di\n", res.real, res.img);
    else
            printf("Subtraction of two complex numbers is = %d%di\n", res.real, res.img);
}

void mul(struct complex n1, struct complex n2)
{
    struct complex res;
    res.real = n1.real*n2.real-n1.img*n2.img;
    res.img = n1.real*n2.img+n1.img*n2.real;
    if(res.img>=0)
            printf("Multiplication of two complex numbers is = %d+%di\n", res.real, res.img);
    else
            printf("Multiplication of two complex numbers is = %d%di\n", res.real, res.img);
}

void division(struct complex n1, struct complex n2)
{
    struct complex res;
    res.real = (n1.real*n2.real + n1.img*n2.img)/(n2.real*n2.real+n2.img*n2.img);
    res.img = (n1.img*n2.real - n1.real*n2.img)/(n2.real*n2.real + n2.img*n2.img);
    if(res.img>=0)
            printf("Division of two complex numbers is = %d+%di\n", res.real, res.img);
    else
            printf("Division of two complex numbers is = %d%di\n", res.real, res.img);
}

OutPut:

manoj@ubuntu:~$ gcc complex_no.c -o complex_no
manoj@ubuntu:~$ ./complex_no

= = = = Arithemetic Operations on Complex number in C = = = =

Enter the real part of first complex number: 4
Enter the imag part of first complex number: 6
Enter the real part of second complex number: 2
Enter the imag part of second complex number: 3
= - - - - - - - - - - - - - - - - - - - - - - - - - - - - - =
First complex number is = 4+6i
Second complex number is = 2+3i
= - - - - - - - - - - - - - - - - - - - - - - - - - - - - - =

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit
Enter your choice (1-5): 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Addition of two complex numbers is = 6+9i
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit
Enter your choice (1-5): 2
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Subtraction of two complex numbers is = 2+3i
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit
Enter your choice (1-5): 3
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Multiplication of two complex numbers is = -10+24i
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit
Enter your choice (1-5): 4
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Division of two complex numbers is = 2+0i
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit
Enter your choice (1-5): 5
Program Terminated
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

manoj@ubuntu:~$

Thursday, October 19, 2017

Program to Implement Queue Using Linked List in C

/*==========================================================================================
 **
 **  File Name     : queue_linkedlist.c
 **  Creation Date : Fri 13 Oct 2017 11:47:03 PM IST
 **  Last Modified : Thu 19 Oct 2017 07:50:35 PM IST
 **  Compiler      : gcc
 **  Author        : Manoj Kumar Patra, Asst. Professor
 **  Organization  : Vignan University, Guntur, India.
 **
 **=========================================================================================*/

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

struct node
{
    int info;
    struct node *next;
};

struct node *front=NULL, *rear=NULL;

void enqueue(int item);
int dequeue();
void display();
int peek();
int isEmpty();
int main()
{
    int choice,item;
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    printf("= = = = = = = = = = Queue Using Linked List in C = = = = = = = =\n");
    while(1)
    {
        printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");     
        printf("1. Enqueue\n");
        printf("2. Dequeue\n");
        printf("3. Display\n");
        printf("4. Peek: Print the front element\n");
        printf("5. Quit\n");
        printf("\nEnter your choice : ");
        scanf("%d", &choice);
        switch(choice)
        {
            case 1:
                printf("Enter the number to be added in queue : ");
                scanf("%d",&item);
                enqueue(item);
                printf("Number %d das been added to the queue\n",item);
                break;
            case 2:
                item=dequeue();
                printf("Front element %d has been deleted\n",item);
                break;
            case 3:
                display();
                break;
            case 4:
                printf("Front element in the Queue is: %d\n",peek());
                break;
            case 5:
                printf("Program Terminated..!!\n");
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
                return;
            default :
                printf("Oops.. Wrong choice!!\n");
        }
    }
}

void enqueue(int item)
{
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    if(tmp==NULL)
    {
        printf("Memory not available\n");
        return;
    }
    tmp->info = item;
    tmp->next=NULL;
    if(front==NULL)
        front=tmp;
    else
        rear->next=tmp;
    rear=tmp;
}

int dequeue()
{
    struct node *tmp;
    int item;
    if(isEmpty( ))
    {
        printf("Queue Underflow\n");
        return;
    }
    tmp=front;
    item=tmp->info;
    front=front->next;
    free(tmp);
    return item;
}

int peek()
{
    if( isEmpty( ) )
    {
        printf("Queue Underflow\n");
        return;
    }
    return front->info;
}

int isEmpty()
{
    if(front==NULL)
        return 1;
    else
        return 0;

}

void display()
{
    struct node *ptr;
    ptr=front;
    if(isEmpty())
    {
        printf("Queue is empty\n");
        return;
    }
    printf("Queue elements are:\n\n");
    while(ptr!=NULL)
    {
        printf("%d\t",ptr->info);
        ptr=ptr->next;
    }
    printf("\n\n");
}

OutPut:

manoj@ubuntu:~/ds/queue$ gcc queue_linkedlist.c -o queue_linkedlist
manoj@ubuntu:~/ds/queue$ ./queue_linkedlist
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
= = = = = = = = = = Queue Using Linked List in C = = = = = = = =
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek: Print the front element
5. Quit

Enter your choice : 1
Enter the number to be added in queue : 10
Number 10 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek: Print the front element
5. Quit

Enter your choice : 1
Enter the number to be added in queue : 20
Number 20 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek: Print the front element
5. Quit

Enter your choice : 1
Enter the number to be added in queue : 30
Number 30 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek: Print the front element
5. Quit

Enter your choice : 1
Enter the number to be added in queue : 40
Number 40 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek: Print the front element
5. Quit

Enter your choice : 3
Queue elements are:

10    20    30    40   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek: Print the front element
5. Quit

Enter your choice : 2
Front element 10 has been deleted
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek: Print the front element
5. Quit

Enter your choice : 4
Front element in the Queue is: 20
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek: Print the front element
5. Quit

Enter your choice : 3
Queue elements are:

20    30    40   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek: Print the front element
5. Quit

Enter your choice : 5
Program Terminated..!!
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

manoj@ubuntu:~/ds/queue$

Program to Implement Queue Using Array in C

/*==========================================================================================
**
**  File Name     : queue_array.c
**  Creation Date : Fri 13 Oct 2017 11:46:43 PM IST
**  Last Modified : Thu 19 Oct 2017 07:59:05 PM IST
**  Compiler      : gcc
**  Author        : Manoj Kumar Patra, Asst. Professor
**  Organization  : Vignan University, Guntur, India.
**
**=========================================================================================*/

#include<stdio.h>
#include<stdlib.h>
#define MAX 100

int queue_array[MAX];
int front=-1;
int rear=-1;

void enqueue(int item);
int dequeue();
int peek();
void display();
int isFull();
int isEmpty();

main()
{
    int choice,item;
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    printf("= = = = = = = = = = Queue Using Array in C = = = = = = = = = =\n");
    while(1)
    {
        printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
        printf("1. Enqueue\n");
        printf("2. Dequeue\n");
        printf("3. Display\n");
        printf("4. Peek\n");
        printf("5. Quit\n");
        printf("\nEnter your choice : ");
        scanf("%d",&choice);

        switch(choice)
        {
        case 1:
            printf("Enter the number to be added in queue : ");
            scanf("%d",&item);
            enqueue(item);
            printf("Number %d das been added to the queue\n",item);
            break;
        case 2:
            item=dequeue();
            printf("Front element %d has been deleted\n",item);
            break;
        case 3:
            display();
            break;
        case 4:
            printf("Front element in the Queue is: %d\n",peek());
            break;
        case 5:
            printf("Program Terminated..!!\n");
            printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
            exit(1);
        default:
            printf("Oops... Wrong choice..!!\n");
        }
    }
}

// 1 ok
void enqueue(int item)
{
    if(isFull())
    {
        printf("Queue Overflow\n");
        return;
    }
    if(front == -1)
        front=0;
    rear=rear+1;
    queue_array[rear]=item ;
}

// 2 ok
int dequeue()
{
    int item;
    if(isEmpty())
    {
        printf("Queue Underflow\n");
        exit(1);
    }
    item=queue_array[front];
    front=front+1;
    return item;
}

// 3 ok

void display()
{
    int i;
    if(isEmpty())
    {
        printf("Queue is empty\n");
        return;
    }
    printf("Queue elements are :\n\n");
    for(i=front; i<=rear; i++)
        printf("%d\t",queue_array[i]);
    printf("\n\n");
}


// 4 ok

int peek()
{
    if(isEmpty())
    {
        printf("Queue Underflow\n");
        exit(1);
    }
    return queue_array[front];
}


// 5 ok

int isEmpty()
{
    if(front==-1 || front==rear+1)
        return 1;
    else
        return 0;
}

// 6 ok
int isFull()
{
    if(rear == MAX-1)
        return 1;
    else
        return 0;
}

OutPut:

manoj@ubuntu:~/ds/queue$ gcc queue_array.c -o queue_array
manoj@ubuntu:~/ds/queue$ ./queue_array
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
= = = = = = = = = = Queue Using Array in C = = = = = = = = = =
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit

Enter your choice : 1
Enter the number to be added in queue : 10
Number 10 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit

Enter your choice : 1
Enter the number to be added in queue : 20
Number 20 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit

Enter your choice : 1
Enter the number to be added in queue : 30
Number 30 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit

Enter your choice : 1
Enter the number to be added in queue : 40
Number 40 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit

Enter your choice : 1
Enter the number to be added in queue : 50
Number 50 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit

Enter your choice : 3
Queue elements are :

10    20    30    40    50   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit

Enter your choice : 2
Front element 10 has been deleted
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit

Enter your choice : 3
Queue elements are :

20    30    40    50   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit

Enter your choice : 4
Front element in the Queue is: 20
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit

Enter your choice : 5
Program Terminated..!!
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

manoj@ubuntu:~/ds/queue$

Program to Implement Circular Linked List in C

/*==========================================================================================
 **
 **  File Name     : cll.c
 **  Creation Date : Sat 16 Sep 2017 10:18:25 PM IST
 **  Last Modified : Thu 19 Oct 2017 05:47:44 PM IST
 **  Compiler      : gcc
 **  Author        : Manoj Kumar Patra, Asst. Professor
 **  Organization  : Vignan University, Guntur, India.
 **
 **=========================================================================================*/

/*P3.3 Program of circular nexted list*/
#include<stdio.h>
#include<stdlib.h>

struct node
{
    int info;
    struct node *next;
};

struct node *create_list(struct node *start);
void display(struct node *start);
struct node *add_at_beg(struct node *start, int data);
struct node *add_at_end(struct node *start, int data);
struct node *add_after_node(struct node *start, int data, int item);
struct node *del_at_beg(struct node *start);
struct node *del_at_end(struct node *start);
struct node *del_a_node(struct node *start, int data);
void displayNtimes(struct node *start);

int main( )
{
    int choice,data,item;
    struct node *start=NULL;

    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    printf("= = = = = = = = = = Circular Linked List in C = = = = = = = = =\n");
    while(1)
    {
        printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
        printf(" 1. Create List\n");
        printf(" 2. Display CLL\n");
        printf(" 3. Add at Beg\n");
        printf(" 4. Add at End\n");
        printf(" 5. Add after node \n");
        printf(" 6. Delete at Beg\n");
        printf(" 7. Delete at End\n");
        printf(" 8. Delete a node\n");
        printf(" 9. Display CLL N Times\n");
        printf(" 10. Exit\n");

        printf("Enter your choice : ");
        scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                start=create_list(start);
                break;
            case 2:
                display(start);
                break;
            case 3:
                printf("Enter the element to be inserted : ");
                scanf("%d",&data);
                start=add_at_beg(start,data);
                break;
            case 4:
                printf("Enter the element to be inserted : ");
                scanf("%d",&data);
                start=add_at_end(start,data);
                break;
            case 5:
                printf("Enter the element to be inserted : ");
                scanf("%d",&data);
                printf("Enter the element after which to insert : ");
                scanf("%d",&item);
                start=add_after_node(start,data,item);
                break;          
            case 6:
                start=del_at_beg(start);
                printf("First node deleted..!\n");
                break;
            case 7:
                start=del_at_end(start);
                printf("Last node deleted..!\n");
                break;
            case 8:
                printf("Enter the element to be deleted : ");
                scanf("%d",&data);
                start=del_a_node(start,data);
                break;
            case 9:
                displayNtimes(start);
                break;
            case 10:
                printf("Program Terminated..!!\n");
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                exit(1);
            default:
                printf("Oops.. Wrong choice\n");
        }
    }
}

//1 ok
struct node *create_list(struct node *start)
{
    if(start!=NULL)
    {
        printf("CLL already exist\n");
        return start;
    }
    int data;
    printf("Enter the element to be inserted: ");
    scanf("%d",&data);
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    tmp->next=tmp;
    return tmp;
}

//2 ok
void display(struct node *s)
{
    struct node *p;
    if(s == NULL)
    {
        printf("CLL is empty!\n");
        return;
    }
    else
    {
        p=s;
        printf("\nThe elements of CLL are:\n");
        do
        {
            printf("%d\t",p->info);
            p=p->next;
        }while(p != s);
    }
    printf("\n");
}

//3 ok
struct node *add_at_beg(struct node *start,int data)
{
    struct node *tmp,*curr;
    curr=start;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    tmp->next=start;
    while(curr->next!=start)
    {
        curr=curr->next;
    }
    curr->next=tmp;
    return tmp;
}

//4 ok
struct node *add_at_end(struct node *start,int data)
{
    struct node *tmp, *curr;
    curr=start;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    tmp->next=start;
    while(curr->next!=start)
    {
        curr=curr->next;  
    }
    curr->next=tmp;
    return start;
}

//5 ok
struct node *add_after_node(struct node *start,int data,int item)
{
    struct node *tmp,*curr;
    curr=start;
    do
    {
        if(curr->info==item)
        {
            tmp=(struct node *)malloc(sizeof(struct node));
            tmp->info=data;
            tmp->next=curr->next;
            curr->next=tmp;
            return start;
        }
        curr=curr->next;
    }while(curr!=start);
    printf("%d not present in the list\n",item);
    return start;
}

//6 ok
struct node *del_at_beg(struct node *start)
{
    struct node *tmp,*last;
    if(start==NULL)
    {
        printf("CLL is empty, Deletion not possible\n");
        return start;
    }
    /*If only one node is there*/
    else if(start->next==start)
    {
        tmp=start;
        start=NULL;
        free(tmp);
        return start;
    }
    else
    {
        last=start;
        while(last->next!=start)
        {
            last=last->next;
        }
        tmp=start;
        start=start->next;
        last->next=start;
        free(tmp);
        return start;
    }
}

//7 ok
struct node *del_at_end(struct node *start)
{
    struct node *tmp, *last, *prev;
    if(start==NULL)
    {
        printf("CLL is empty, Deletion not possible\n");
        return start;
    }
    /*If only one node is there*/
    else if(start->next==start)
    {
        tmp=start;
        start=NULL;
        free(tmp);
        return start;
    }
    else
    {
        last=start;
        while(last->next != start)
        {
            prev=last;
            last=last->next;
        }
        tmp=last;
        prev->next=last->next;
        free(tmp);
        return start;
    }
}

//8 ok
struct node *del_a_node(struct node *start,int item)
{
    struct node *tmp,*p,*pp;
    if(start==NULL)
    {
        printf("CLL is empty, Deletion not possible\n");
        return start;
    }
    /*If only one node is there*/
    else if(start->info==item)
    {
        p=start;
        while(p->next!=start)
        {
            p=p->next;
        }
        tmp=start;
        start=start->next;
        p->next=start;
        free(tmp);
        return start;
    }

    /*Deletion in between*/
    else
    {
        p=start;
        while(p->next!=start)
        {
            pp=p;
            p=p->next;
            if(p->info==item)   
            {
                tmp=p;
                pp->next=p->next;
                free(tmp);
                return start;
            }
        }

    }
    printf("Element %d not found in CLL\n",item);
    return start;
}

//9 ok
void displayNtimes(struct node *s)
{
    int n,count=0;
    printf("How many times you wanna print CLL?: ");
    scanf("%d",&n);
    struct node *p;
    if(s == NULL)
    {
        printf("CLL is empty!\n");
        return;
    }
    else
    {
        p=s;
        printf("\nThe elements of CLL are:\n");
        do
        {
            if(p->next == s)
            {
                count++;
            }
            printf("%d\t",p->info);
            p=p->next;

        }while(count < n);
    }
    printf("\n");
}


OutPut:

manoj@ubuntu:~/ds/linkedlist$ gcc cll.c -o cll
manoj@ubuntu:~/ds/linkedlist$ ./cll
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
= = = = = = = = = = Circular Linked List in C = = = = = = = = =
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 1
Enter the element to be inserted: 20
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 2

The elements of CLL are:
20   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 3
Enter the element to be inserted : 10
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 2

The elements of CLL are:
10    20   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 4
Enter the element to be inserted : 30
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 2

The elements of CLL are:
10    20    30   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 5
Enter the element to be inserted : 50
Enter the element after which to insert : 20
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 2

The elements of CLL are:
10    20    50    30   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 6
First node deleted..!
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 2

The elements of CLL are:
20    50    30   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 7
Last node deleted..!
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 2

The elements of CLL are:
20    50   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 8
Enter the element to be deleted : 30
Element 30 not found in CLL
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 8
Enter the element to be deleted : 50
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 2

The elements of CLL are:
20   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 9
How many times you wanna print CLL?: 5

The elements of CLL are:
20    20    20    20    20   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 10
Program Terminated..!!
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/ds/linkedlist$

Friday, October 13, 2017

Program to Implement Stack Using Linked List in C

/*==========================================================================================
**
**  File Name     : stack_linkedlist.c
**  Creation Date : Fri 13 Oct 2017 11:07:44 PM IST
**  Last Modified : Fri 13 Oct 2017 11:22:39 PM IST
**  Compiler      : gcc
**  Author        : Manoj Kumar Patra, Asst. Professor
**  Organization  : Vignan University, Guntur, India.
**
**=========================================================================================*/


#include<stdio.h>
#include<stdlib.h>
struct node
{
    int info;
    struct node *next;
}*top=NULL;
void push(int item);
int pop();
void display();
int peek();
int isEmpty();

int main()
{
    printf("\n= = = = = = = = = = Stack Using Linked List in C = = = = = = = = = =\n");
    int choice,item;
    while(1)
    {        
        printf("1. Push\n");
        printf("2. Pop\n");
        printf("3. Display all items of the stack\n");
        printf("4. Display item at the top\n");
        printf("5. Quit\n");
        printf("Enter your choice : ") ;
        scanf("%d", &choice);

        switch(choice)
        {
        case 1:
            printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
            printf("Enter the item to be pushed : ");
            scanf("%d",&item);
            push(item);
            printf("The item %d pushed into the stack\n",item);
            printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
            break;
        case 2:
            item=pop();
            printf("Item %d has been Popped out\n",item);
            printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
            break;
        case 3:
            display();
            printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
            break;
        case 4:
            printf("Item at the top is %d\n",peek());
            printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");          
            break;
        case 5:
            printf("Program Terminated\n");
            printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
            return;
        default :
            printf("Oops.. Wrong choice! Select Again\n");
            printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
        }
    }
}

void push(int item)
{
    struct node *temp;
    temp=(struct node *)malloc(sizeof(struct node));
    if(temp==NULL)
    {
        printf("Stack Overflow\n");
        return;
    }
    temp->info=item;
    temp->next=top;
    top=temp;
}

int pop()
{
    struct node *temp;
    int item;
    if(isEmpty())
    {
        printf("Stack Underflow\n");
        exit(1);
    }
    temp=top;
    item=temp->info;
    top=top->next;
    free(temp);
    return item;
}

void display()
{     
    struct node *ptr;
    ptr=top;
    if(isEmpty())
    {  
        printf("Stack is empty\n");
        return;
    }
    printf("Stack elements :\n");
    while(ptr!=NULL)
    {
        printf(" %d\n",ptr->info);
        ptr=ptr->next;
    }
}

int peek()
{
    if(isEmpty())
    {
        printf("Stack Underflow\n");
        exit(1);
    }
    return top->info;
}

int isEmpty()
{
    if(top == NULL)
        return 1;
    else
        return 0;
}

OutPut:

manoj@ubuntu:~/ds/stack$ gcc stack_linkedlist.c -o stack_linkedlist
manoj@ubuntu:~/ds/stack$ ./stack_linkedlist

= = = = = = = = = = Stack Using Linked List in C = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 1
The item 1 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 2
The item 2 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 3
The item 3 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 4
The item 4 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 3
Stack elements :
 4
 3
 2
 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 2
Item 4 has been Popped out
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 4
Item at the top is 3
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 3
Stack elements :
 3
 2
 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 5
Program Terminated
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/ds/stack$

Program to Implement Stack Using Array in C

/*==========================================================================================
**
**  File Name     : stack_array.c
**  Creation Date : Fri 13 Oct 2017 10:20:56 PM IST
**  Last Modified : Fri 13 Oct 2017 10:50:21 PM IST
**  Compiler      : gcc
**  Author        : Manoj Kumar Patra, Asst. Professor
**  Organization  : Vignan University, Guntur, India.
**
**=========================================================================================*/

#include<stdio.h>
#include<stdlib.h>
#define MAX 10
int stack_array[MAX];
int top = -1;
void push(int item);
int pop();
int peek();
int isEmpty();
int isFull();
void display();

int main()
{
    printf("\n= = = = = = = = = = Stack Using Array in C = = = = = = = = = =\n");
    int choice,item;
    while(1)
    {
        printf("1. Push\n");
        printf("2. Pop\n");
        printf("3. Display all stack elements\n");
        printf("4. Display the top element\n");
        printf("5. Quit\n");
        printf("Enter your choice: ");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1 :
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                printf("Enter the item to be pushed : ");
                scanf("%d",&item);
                push(item);
                printf("The item %d pushed into the stack\n",item);
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                break;
            case 2:
                item = pop();
                printf("Item %d has been Popped out\n",item);
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                break;
            case 3:
                display();
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                break;
            case 4:
                printf("Item at the top is : %d\n", peek() );
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                break;
            case 5:
                printf("Program Terminated\n");
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                return;
            default:
                printf("Oops.. Wrong choice! Select Again\n");
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
        }
    }
}

void push(int item)
{
    if( isFull() )
    {
        printf("Stack Overflow\n");
        return;
    }
    top = top+1;
    stack_array[top] = item;
}

int pop()
{
    int item;
    if(isEmpty())
    {
        printf("Stack Underflow\n");
        exit(1);
    }
    item = stack_array[top];
    top = top-1;
    return item;
}

void display()
{
    int i;
    if(isEmpty())
    {
        printf("Stack is empty\n");
        return;
    }
        printf("Stack elements are:\n\n");
    for(i=top; i>=0; i--)
        printf("%d\t",stack_array[i]);
    printf("\n\n");
}

int peek()
{
    if( isEmpty() )
    {
        printf("Stack Underflow\n");
        exit(1);
    }
    return stack_array[top];
}

int isEmpty()
{
    if(top == -1)
        return 1;
    else
        return 0;
}

int isFull()
{
    if(top == MAX-1)
        return 1;
    else
        return 0;
}


OutPut:




manoj@ubuntu:~/ds/stack$ gcc stack_array.c -o stack_array 
manoj@ubuntu:~/ds/stack$ ./stack_array

= = = = = = = = = = Stack Using Array in C = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 1
The item 1 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 2
The item 2 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 3
The item 3 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 4
The item 4 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 5
The item 5 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 3
Stack elements are:

5    4    3    2    1   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 2
Item 5 has been Popped out
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 2
Item 4 has been Popped out
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 3
Stack elements are:

3    2    1   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 4
Item at the top is : 3
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 5
Program Terminated
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/ds/stack$
manoj@ubuntu:~/ds/stack$ ./stack_array

= = = = = = = = = = Stack Using Array in C = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 1
The item 1 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 2
The item 2 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 3
The item 3 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 4
The item 4 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 5
The item 5 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 3
Stack elements are:

5    4    3    2    1   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 2
Item 5 has been Popped out
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 2
Item 4 has been Popped out
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 3
Stack elements are:

3    2    1   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 4
Item at the top is : 3
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 5
Program Terminated
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/ds/stack$
manoj@ubuntu:~/ds/stack$ ./stack_array

= = = = = = = = = = Stack Using Array in C = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 1
The item 1 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 2
The item 2 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 3
The item 3 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 4
The item 4 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 5
The item 5 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 3
Stack elements are:

5    4    3    2    1   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 2
Item 5 has been Popped out
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 2
Item 4 has been Popped out
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 3
Stack elements are:

3    2    1   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 4
Item at the top is : 3
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 5
Program Terminated
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/ds/stack$
manoj@ubuntu:~/ds/stack$ ./stack_array

= = = = = = = = = = Stack Using Array in C = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 1
The item 1 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 2
The item 2 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 3
The item 3 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 4
The item 4 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 5
The item 5 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 3
Stack elements are:

5    4    3    2    1   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 2
Item 5 has been Popped out
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 2
Item 4 has been Popped out
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 3
Stack elements are:

3    2    1   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 4
Item at the top is : 3
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 5
Program Terminated
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/ds/stack$

Program to Implement Breadth First Search Using Adjacency Matrix in C

/*==========================================================================================
 **
 **  File Name     : bfs1.c
 **  Creation Date : Sat 14 Oct 2017 12:17:50 AM IST
 **  Last Modified : Sat 14 Oct 2017 01:42:00 AM IST
 **  Compiler      : gcc
 **  Author        : Manoj Kumar Patra, Asst. Professor
 **  Organization  : Vignan University, Guntur, India.
 **
 **=========================================================================================*/

#include<stdio.h>
#include<stdlib.h>
#define MAX 100
int n;/*Number of vertices in the graph*/
int adj[MAX][MAX]; /*Adjacency Matrix*/
int visited[MAX]; /* Not Visited: 0 ,visited: 1 */
void create_graph();
void BFS_Traversal();
int queue[MAX], front=-1,rear=-1;
int isEmptyQueue();
void enQueue(int vertex);
int deQueue();
int main()
{
    printf("\n= = = = = = = = Depth First Search Using Adjacency Matrix = = = = = = = =\n");
    int s,i;
    printf("\nEnter Number of Vertices: ");
    scanf("%d",&n);
    printf("Enter the data into matrix:\n");
    create_graph();
    for(i=0; i<n; i++)
    {
        visited[i]=0;
    }
    printf("\nEnter starting vertex for Breadth First Search: ");
    scanf("%d", &s);
    printf("\nVisit Sequence: \n");
    BFS_Traversal(s);
    printf("\n\n");
}

void create_graph()
{
    int i,j;
    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {
            scanf("%d",&adj[i][j]);
        }
        printf("\n");
    }
    printf("\nThe adjecency matrix of the graph is:\n");
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    for(i=0;i<n;i++)
    {
        printf("\n");
        for(j=0;j<n;j++)
        {
            printf("%d\t",adj[i][j]);
        }
        printf("\n");
    }
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
}

void BFS_Traversal(int i)
{
    int j;
    printf("V%d\t",i);
    if(front==-1)
    {
        visited[i]=1;
        enQueue(i);
    }
    for(j=0; j<n; j++)
    {
        if(visited[j]==0 && adj[i][j]==1)
        {
            visited[j]=1;
            enQueue(j);
        }
    }
    deQueue();
    if(!isEmptyQueue())
    {
        BFS_Traversal(queue[front]);
    }
}

int isEmptyQueue()
{
    if(front == -1 || front > rear )
        return 1;
    else
        return 0;
}

void enQueue(int vertex)
{
    if (front == -1)  /*If queue is initially empty */
    {
        front = 0;
    }
    rear = rear+1;
    queue[rear] = vertex ;
}

int deQueue()
{
    front = front+1;
}

Program to Implement Depth First Search in C - Using Stack

Depth First Search:

In Depth First Search we are using stack data structure to traverse a graph. First select a vertex randomly, mark it as visited vertex and push this node into the stack. Then select an unvisited and adjacent vertex of the vertex which is at the top of the stack. Mark that vertex as visited and push it into the stack. Continue the same process until there is no adjacent vertex of the vertex at the top of the stack. Then delete one element from the stack and continue the same process until stack is empty.

Steps:
1. Select a vertex randomly from the graph as a starting point.
2. Mark that vertex as visited and push that vertex into the stack.
3. Select an adjacent vertex of vertex at the top of the stack and which is not visited.
4. Mark that vertex as visited and push that vertex into the stack.
5. Continue the step 3 and 4 until there is no vertex to be visited from the vertex at the top of the stack.
6. If there is no vertex to be visited from the vertex at the top of the stack, delete one element from the stack.
7. Repeat step 3 and 4 until stack is empty.


/*==========================================================================================
 **
 **  File Name     : dfs_stack.c
 **  Creation Date : Fri 13 Oct 2017 04:13:10 PM IST
 **  Last Modified : Fri 13 Oct 2017 08:09:25 PM IST
 **  Compiler      : gcc
 **  Author        : Manoj Kumar Patra, Asst. Professor
 **  Organization  : Vignan University, Guntur, India.
 **
 **=========================================================================================*/

#include<stdio.h>
void DepthFirstSearch(int);
int graph[100][100],visited[100],n;
void main()
{
    int i,j;
    printf("Enter total number of vertices in the graph: ");
    scanf("%d",&n);
    printf("\nEnter adjecency matrix of the graph:\n");
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            scanf("%d",&graph[i][j]);
        }
        printf("\n");
    }
    //initially mark all verteces as unvisited
    printf("\nThe adjecency matrix of the graph is:\n");
    printf("= = = = = = = = = = = = = = = = = = = =\n");
    for(i=0;i<n;i++)
    {
        printf("\n");
        for(j=0;j<n;j++)
        {
            printf("%d\t",graph[i][j]);
        }
        printf("\n");
    }
    printf("= = = = = = = = = = = = = = = = = = = =\n");
    for(i=0;i<n;i++)
        visited[i]=0;
    printf("\nVisited Sequence: \n");
    DepthFirstSearch(0);
    printf("\n\n");
}

void DepthFirstSearch(int i)
{
    int stack[n],top=-1;
    int j, curr;
    printf("V%d\t",i);
    visited[i]=1;
    stack[++top]=i;
    while(top != -1)
    {
        curr=stack[top];
        for(j=0;j<n;j++)
        {
            if(visited[j]==0 && graph[i][j]==1)
            {
                stack[++top]=j;
                DepthFirstSearch(stack[top]);
            }
        }
        top--;
    }
}

OutPut:

manoj@ubuntu:~$ gcc dfs_stack.c -o dfs_stack
manoj@ubuntu:~$ ./dfs_stack
Enter total number of vertices in the graph: 5

Enter adjecency matrix of the graph:
0
1
1
0
0

1
0
0
1
1

1
0
0
1
0

0
1
1
0
0

0
1
0
1
0


The adjecency matrix of the graph is:
= = = = = = = = = = = = = = = = = = = =

0    1    1    0    0   

1    0    0    1    1   

1    0    0    1    0   

0    1    1    0    0   

0    1    0    1    0   
= = = = = = = = = = = = = = = = = = = =

Visited Sequence: V0    V1    V3    V2    V4   

manoj@ubuntu:~$