Showing posts with label Lab programs. Show all posts
Showing posts with label Lab programs. Show all posts

Monday 8 August 2016

Lab Program 3 Stack 15CSL38 Data Structures in C Lab

Lab Program 3:

Design, Develop and Implement a menu driven Program in C for the following operations on STACK of Integers (Array Implementation of Stack with maximum size MAX) 
a. Push an Element on to Stack 
b. Pop an Element from Stack 
c. Demonstrate how Stack can be used to check Palindrome 
d. Demonstrate Overflow and Underflow situations on Stack 
e. Display the status of Stack 
f. Exit 
Support the program with appropriate functions for each of the above operations


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

int s[MAX];
int top = -1;

void push(int item);
int pop();
void palindrome();
void display();

void main()
{
            int choice, item;
            while(1)
            {
                        printf("\n\n\n\n~~~~~~Menu~~~~~~ : ");
                        printf("\n=>1.Push an Element to Stack and Overflow demo ");
                        printf("\n=>2.Pop an Element from Stack and Underflow demo");
                        printf("\n=>3.Palindrome demo ");
                        printf("\n=>4.Display ");
                        printf("\n=>5.Exit");
                        printf("\nEnter your choice: ");
                        scanf("%d", &choice);
                        switch(choice)
                        {
                                    case 1:             printf("\nEnter an element to be pushed: ");
                                                            scanf("%d", &item);
                                                            push(item);
                                                            break;
                                    case 2:             item = pop();
                                                            if(item != -1)
                                                                        printf("\nElement popped is: %d", item);
                                                            break;
                                    case 3:             palindrome();
                                                            break;
                                    case 4:             display();
                                                            break;
                                    case 5:             exit(1);
                                    default:            printf("\nPlease enter valid choice ") ;
                                                            break;
                    }
            }
}

void push(int item)
{
            if(top == MAX-1)
            {
                        printf("\n~~~~Stack overflow~~~~");
                        return;
            }

            top = top + 1 ;
            s[top] = item;
}

int pop()
{
            int item;
            if(top == -1)
            {
                        printf("\n~~~~Stack underflow~~~~");
                        return -1;
            }
            item = s[top];
            top = top - 1;
            return item;
}

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

void palindrome()
{
            int flag=1,i;
            printf("\nStack content are:\n");
            for(i=top; i>=0 ; i--)
                        printf("| %d |\n", s[i]);

            printf("\nReverse of stack content are:\n");
            for(i=0; i<=top; i++)
                        printf("| %d |\n", s[i]);

            for(i=0; i<=top/2; i++)
            {
                        if( s[i] != s[top-i] )
                        {
                                    flag = 0;
                                    break;
                        }
            }
            if(flag == 1)
            {
                        printf("\nIt is palindrome number");
            }
            else
            {
                        printf("\nIt is not a palindrome number");
            }
}


Output:

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 11

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 12

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 13

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 14

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 15

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 16
~~~~Stack overflow~~~~

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 4

Stack elements are:
|  15  |
|  14  |
|  13  |
|  12  |
|  11  |

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2
Element popped is: 15

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 4
Stack elements are:
|  14  |
|  13  |
|  12  |
|  11  |
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2
Element popped is: 14

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2
Element popped is: 13

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2
Element popped is: 12

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2
Element popped is: 11

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2
~~~~Stack underflow~~~~

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 4
~~~~Stack is empty~~~~

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 11

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 22

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 11

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 3
Stack content are:
|  11  |
|  22  |
|  11  |

Reverse of stack content are:
|  11  |
|  22  |
|  11  |

It is palindrome number

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2
Element popped is: 11

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2
Element popped is: 22

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2
Element popped is: 11

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 11

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 22

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 33

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 3
Stack content are:
|  33  |
|  22  |
|  11  |

Reverse of stack content are:
|  11  |
|  22  |
|  33  |

It is not a palindrome number

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 5



Also Credits to: 
Manoj Taleka  (manoj89biet@gmail.com)

Yashaswini Jogi (jogi.yash@gmail.com)

Lab Program 2 String 15CSL38 Data Structures in C Lab

Lab Program 2:


Design, Develop and Implement a Program in C for the following operations on Strings
a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR with REP if PAT exists in STR.
c. Report suitable messages in case PAT does not exist in STR
Support the program with functions for each of the above operations. Don't use Built-in functions.

#include<stdio.h>
char str[50], pat[20], rep[20], ans[50];
int     c=0, m=0, i=0, j=0, k, flag=0;
void stringmatch()
{
                 while(str[c] !='\0')
                {
                                if(str[m] == pat[i])
                                {
                                                i++;
                                                m++;
                                                if(pat[i] == '\0')
                                                {             
                                                                flag = 1;
                                                                for(k=0; rep[k]!='\0'; k++, j++)
                                                                {
                                                                                ans[j] = rep[k];
                                                                }
                                                                i = 0;
                                                                c = m;
                                                }
                                }
                                else
                                {
                                                ans[j]= str[c];
                                                j++;
                                                c++;
                                                m=c;
                                                i=0;
                                }
                }
                ans[j]='\0';
}
void main()
{
                printf("\nEnter the main string:");
                gets(str);
                printf("\nEnter the pat string:");
                gets(pat);
                printf("\nEnter the replace string:");
                gets(rep);
                stringmatch();
                if(flag == 1)
                                printf("\nResultant string is %s", ans);
                else
                                printf("\nPattern string is not found");
}

Enter the main string:    hello aabhii howaab
Enter the pat string:        aab
Enter the replace string:                               klmno

Resultant string is: hello klmnohii howklmno





Method 2:

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

char str[50], pat[50], rep[50];
int start = 0, patfound = 0;
int lasts, lastp, lastr;

void replacepattern()
{
            int i, j;
            lastr = strlen(rep)-2;
           
            if(lastp != lastr)
            {
                        printf("\nInvalid length of replace string");
                        exit(0);
            }
            else
            {
                        i = start;
                        for(j=0; j<=lastr; j++)
                        {
                                    str[i] = rep[j];
                                    i++;
                        }
            }
            return;
}

void findpattern()
{
            int i, j, inmatch;
            lasts = (strlen(str))-2;
            lastp = (strlen(pat))-2;
            int endmatch;

            for(endmatch = lastp; endmatch<=lasts; endmatch++, start++)
            {
                        if(str[endmatch] == pat[lastp])
                        {
                                    inmatch = start;
                                     j=0;
                                    while(j<lastp)
                                    {
                                                if(str[inmatch] == pat[j])
                                                {
                                                            inmatch++;
                                                            j++;
                                                }
                                                else
                                                {
                                                            break;
                                                }
                                    }
                                    if(j == lastp)
                                    {
                                                patfound = 1;
                                                replacepattern();
                                    }
                        }
            }
            return;
}

void main()
{
            printf("\nEnter the main string(STR): ");
            fgets(str, 50, stdin);

            printf("\nEnter the pattern to be matched(PAT): ");
            fgets(pat, 50, stdin);

            printf("\nEnter the string to be replaced(REP): ");
            fgets(rep, 50, stdin);

            printf("\nThe string before pattern match is:\n %s", str);

            findpattern();

            if(patfound == 0)
                        printf("\nThe pattern is not found in the main string");
            else
                        printf("\nThe string after pattern match and replace is: \n %s ",str);
            return;
}


 Output:


Case 1:

Enter the main string(STR):    Hello hii how are you hii

Enter the pattern to be matched(PAT): hii

Enter the string to be replaced(REP): xyz

The string before pattern match is:
 Hello hii how are you hii

The string after pattern match and replace is:
 Hello xyz how are you xyz




Case 2:

Enter the main string(STR): Hello hii how are you

Enter the pattern to be matched(PAT): abc

Enter the string to be replaced(REP): xyz

The string before pattern match is:
 Hello hii how are you

The pattern is not found in the main string



Also Credits to: 
Manoj Taleka  (manoj89biet@gmail.com)
Yashaswini Jogi (jogi.yash@gmail.com)