Pages

Custom Search
Showing posts with label reverse. Show all posts
Showing posts with label reverse. Show all posts

Thursday, October 17, 2013

Reversing array contents in the same array.

Reversing array contents:

In this program, we have to reverse the array contents without using any temporary array.
This can be achieved using swapping technique.
Have one index 'i' pointing to first element and one index 'j' pointing to last element.
Swap their contents.
Increment i, decrement j
Continue as long as i is less than or equal to j.
Done.
Print the array contents again, which are now in reverse order.


Source Code:



1:  #include <stdio.h>  
2:  #include <stdlib.h>  
3:    
4:  int main()  
5:  {  
6:       int i, a[10], t, n, j;  
7:         
8:       printf("\nEnter the size of the array: ");  
9:       scanf("%d", &n);  
10:         
11:       printf("\nEnter the array elements: ");  
12:       for(i=0;i<n;i++)  
13:            scanf("%d", &a[i]);  
14:              
15:       for(i=0,j=n-1;i<=j;i++,j--)  
16:       {  
17:            t=a[i];  
18:            a[i]=a[j];  
19:            a[j]=t;  
20:       }  
21:         
22:       printf("\nThe reverse of the array is: ");  
23:       for(i=0;i<n;i++)  
24:            printf("%d ", a[i]);  
25:       printf("\n");  
26:         
27:       return 1;  
28:  }  


Monday, October 10, 2011

Reversing a Stack using Queue.


#include <stdio.h>
#include <stdlib.h>
#define SIZE 5

struct tag
{
        int a[SIZE];
        int top;
};                                                                 //Structure definition for stack
typedef struct tag stack;


struct tagg
{
        int items[SIZE];
        int rear;
        int front;
};                                                                  //Structure definition for queue
typedef struct tagg QUEUE;


void push();
int pop();
void display();
void rev();                       //Function declarations(parameters not required at declaration)


int main()
{
        int ch, element, dnum;
        stack st;
        st.top= -1;

        do
        {                                                                 //Displaying menu for operations on stack
                printf("\n1.) PUSH");
                printf("\n2.) POP");
                printf("\n3.) DISPLAY");
                printf("\n4.) REVERSE");
                printf("\n5.) EXIT");
                printf("\nEnter your choice: ");
                scanf("%d", &ch);

                switch(ch)
                {
                        case 1: printf("\nEnter element to push: ");
                                    scanf("%d", &element);
                                    push(&st, element);
                                    break;

                        case 2: dnum = pop(&st);
                                    if(dnum != -1)
                                    printf("\nDeleted element: %d", dnum);
                                    break;

                        case 3: display(st);
                                    break;

                        case 4: rev(&st);
                                    break;

                        case 5: break;

                        default: printf("\nWrong choice!! Enter choice again.");
                  }
         }while(ch!=5);

}

void push(stack *s, int ele)
{
        if(s->top == SIZE-1)
        {
                printf("\nStack is overflow");
        }
        else
        {
                (s->top)++;
                s->a[s->top] = ele;;
        }
}

int pop(stack *s)
{
        if(s->top == -1)
        {
                printf("\nStack is underflow");
                return(-1);
        }
        else
        {
                return(s->a[(s->top)--]);
        }
}

void display(stack s)
{
        int i;
        if(s.top == -1)
        {
               printf("\nStack is empty");
        }
        else
        {
                for(i=s.top; i>=0; i--)
                {
                           printf("\n%d", s.a[i]);
                }
         printf("\n");
         }
}

//Function to reverse a stack using queue
void rev(stack *s)
{
        QUEUE q;
        q.rear = -1;
        q.front = 0;

        if(s->top==-1)
        {
                printf("\nStack empty");
                return;
        }

        while((s->top) > -1)
        {
                 q.items[++(q.rear)] = s->a[(s->top)--];
        }

        while(q.front <= q.rear)
        {
                  s->a[++(s->top)] = q.items[(q.front)++];
        }

}

Custom Search