The greatest mistake you can make in life is to be continually fearing you will make one
Showing posts with label Data Structure Programs. Show all posts
Showing posts with label Data Structure Programs. Show all posts

Wednesday, 15 June 2011

C program for Evaluating a Postfix Expression

The Algorithm for Evaluating a Postfix Expression  is given here

Program:

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#define SIZE 40
int stack[SIZE];
int top=-1;

void push(int n)
{
    if(top==SIZE-1)
    {
        printf("Stack is full\n");
        return;
    }
    else
    {
        top=top+1;
        stack[top]=n;
        printf("Pushed element is %d\n",n);
    }
}

int pop()
{
    int n;
    if(top==-1)
    {
        printf("Stack is empty\n");
        return;
    }
    else
    {
        n=stack[top];
        top=top-1;
        printf("The poped element is %d\n",n);
        return(n);
    }
}

int evaluate(int op1, int op2,char ch)
{
    printf("op1=%d op2=%d ch=%c\n",op1,op2,ch);
    int n;
    if (op1<op2)
    {
        n=op1;
        op1=op2;
        op2=n;
        }
    if(ch=='+')
        n=op1+op2;
    else if(ch=='-')
        n=op1-op2;
    else if(ch=='*')
        n=op1*op2;
    else if(ch=='/')
        n=op1/op2;
    else if(ch=='%')
        n=op1%op2;
    else
    {
        printf("The operator is not identified\n");
        exit(0);
    }
    printf("n=%d\n",n);
    return(n);
}

int main()
{
      char str[50],ch,ch1;
      int i=0,n,op1,op2;

      printf("Enter the Postfix string\n");
      scanf("%s",str);
      ch=str[i];
      while(ch!='\0')
      {
        printf("The char is=%c\n",ch);
           //if(ch=='1' || ch=='2' || ch=='3' || ch=='4' || ch=='5')//
           if(isdigit(ch))
           {
                n=ch-'0';
                push(n);
           }
           else
           {
                op1=pop();
                op2=pop();
                n=evaluate(op1,op2,ch);
                push(n);
           }
           ch=str[++i];
      }
      printf("The value of the arithmetic expression is=%d\n",pop());
      return;
}

Friday, 12 February 2010

C Programs Based on Queue

Algorithm 1Write an algorithm for inserting an element in to the queue using array
Solution:
    Let Q be the array of specified size, SIZE
  1. Initialize front=0,rear=-1
  2. Input the value to be inserted and assign to variable "data"
  3. If (rear > = SIZE)
          (a) Display "Queue Overflow"
          (b) Exit.
  4. Else
          (a) rear=rear+1;
  5. Q[rear]=data;
  6. Exit
Algorithm 2: Write an algorithm for deleting an element from the queue using array
Solution
    Let Q be the array of specified size,SIZE
  1. if (rear < front )
        (a) front=0,rear=-1
        (b) Display "The queue is empty"
        (c) Exit
  2. else
        (a) Data=Q[front]
        (b) front=front+1;
  3. Exit

Thursday, 11 February 2010

C Programs Based on Stack

Program 1: Write a C program to implement stack operations push and pop using array.

Program 2: Write a C program to implement stack operations push and pop using Linked List.

Program 3:Design an algorithm,using a stack to read five characters from a keyboard and display them in reverse order.

Program 4:Design an algorithm,using a stack,which will convert a decimal number to binary.

Program 5:Design an algorithm using a stack,which will test whether an input string is palindromic or not

Program 6:Write a C function to copy one stack to another assuming the stack is implemented using array

Program 7:Write a C function to copy one stack to another assuming the stack is implemented using Linked list

C Programs based on LInked List

Sample Program 1:
Simple linear linked list constructed in c.Adding 10 elements into the list and print them
Program

#include "stdio.h"
#include "stdlib.h"

struct Node
{
   //each node contains a data and a pointer to the next node in the list
   int data;
   struct Node *next;
};

typedef struct Node node;

int main()
{
    node *newnode,*head,*first;
    int i;
    head=NULL;
    first=NULL;

    //Inserting 10 nodes in a list
    for(i=1;i<=10;i++)
    {
       newnode=(node*)malloc(sizeof(node));
       newnode->data=i;
       newnode->next=NULL;
  
       if(head==NULL)
       {
          head=newnode;
          first=newnode; //To point the first node in the list
        }
        head->next=newnode;
        head=newnode;
    }
     //Print the nodes in the list
     newnode=first;
     while(newnode)
     {
          printf("%d\t",newnode->data);
          newnode=newnode->next;
     }
}


Output:
1 2 3 4 5 6 7 8 9 10


Sample Program 2
Write a C program to perform the basic operation like insertion,deletion,display in a singly linked list.
solution
#include<stdio.h>
#include<stdlib.h>

void Insert_list(int);
void Insert_pos(int,int);
void delete_list(int);
void display(void);
void search(int);

struct Node
{
    int data;
    struct Node *next;
};

typedef struct Node node;
node *head=NULL,*curr=NULL;

int main()
{
    int ch,n,m;
    do
    {
        printf("ENter your choice\n1.Insert end of list\n2.Insert at position\n3.Delete\n4.Search\n5.Display\n6.Exit\n");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:
                    printf("Enter the data to insert\n");
                    scanf("%d",&n);
                    Insert_list(n);
                    break;
            case 2:
                    printf("Enter the data to insert\n");
                    scanf("%d",&n);
                    printf("Enter the position after the data is insert\n");
                    scanf("%d",&m);
                    Insert_pos(n,m);
                    break;
            case 3:
                    printf("Enter the data item to be delete\n");
                    scanf("%d",&n);
                    delete_list(n);
                    break;
            case 4:
                    printf("Enter the data item to be search\n");
                    scanf("%d",&n);
                    search(n);
                    break;
            case 5:
                    display();
                    break;
            case 6:
                    exit(0);
            default:
                    printf("Enter a valid digit\n");
                    break;
        }
    }while(ch>=1 && ch<=6);
}
void Insert_list(int n)
{
    node *newnode;
    //creating a new node with data n
    newnode=(node *)malloc(sizeof(node));
    newnode->data=n;
    newnode->next=NULL;
    //check the head node. If head is NULL then the list is empty
    //insert this node as the head node
    if(head==NULL)
    {
        head=newnode;
        curr=head;
    }
    else
    {
        //curr is the pointer pointing the last node of the list
        curr->next=newnode;
        curr=newnode;
    }
}

void Insert_pos(int n,int pos)
{
    node *nnode,*newnode;
    int i;
    if(head==NULL)
    {
        printf("The list is empty\n");
        return;
    }
    //assigning a temporary pointer nnode to point the head node
    nnode=head;
    //finding the position in the list
    for(i=0;i<pos-1;i++)
    {
        nnode=nnode->next;
        if(nnode==NULL) //reached end of list
        {
            printf("There are less than %d nodes in the list\n",pos);
            return;
        }
    }
    //found the position to insert
    //create a newnode with given data n
    newnode=(node *)malloc(sizeof(node));
    newnode->data=n;
    newnode->next=NULL;
    //inserting after the position
    newnode->next=nnode->next;
    nnode->next=newnode;
    printf("Node with value %d inserted after %d position\n",n,pos);
    return;
}

void delete_list(int n)
{
//Search the given item in the list if it is present delete or print a message
    node *nnode,*temp;
    if(head==NULL)
    {
        printf("The List is Empty\n");
        return;
    }
    //If the data is present in the first node
    if(head->data==n)
    {
        temp=head;
        head=head->next;
        free(temp);
        return;
    }
    nnode=head;

    while(nnode->next->next!=NULL)
    {
        if(nnode->next->data==n)
        {
            temp=nnode->next;
            nnode->next=temp->next;
            free(temp);
            printf("node with value %d is deleted\n",n);
            return;
        }
        else
        {
            nnode=nnode->next;
        }
    }
    //Deleting the last node
    if(nnode->next->data==n)
    {
        temp=nnode->next;
        nnode->next=NULL;
        free(temp);
        printf("The node with value %d is deleted\n",n);
        return;
    }
    //Reached end of list and the item is not present
    printf("The node with value %d is not found\n",n);
    return;
}

void search(int n)
{
    node *nnode;
    nnode=head;
    while(nnode)
    {
        if(nnode->data==n)
        {
            printf("Node with data %d found\n",n);
            return;
        }
        nnode=nnode->next;
    }
    printf("Node with data %d is not found\n",n);
}

void display()
{
    node *nnode;
    if(head==NULL)
    {
        printf("Empty list\n");
        return;
    }
    nnode=head;
    while(nnode)
    {
        printf("%d->",nnode->data);
        nnode=nnode->next;
    }
}

Exercise Programs in Linked list
  1. Modify sample program 1 to store items in the reverse order. ie while printing the list the output should be 10 9 8 7 6 5 4 3 2 1.