顯示具有 Data Structure 標籤的文章。 顯示所有文章
顯示具有 Data Structure 標籤的文章。 顯示所有文章

2013年1月21日 星期一

[Link List][Stack][C Language] How to implement "Infix Order" by using link list


[Link List][Stack][C Language] How to implement "Infix Order" by using link list





#include <stdlib.h>

#include <stdio.h>

struct s_node

{

     int data;

     struct s_node *next;

};

typedef struct s_node s_list;

typedef s_list *link;

link operator=NULL;

link operand=NULL;

 

link push(link stack,int value)

{

    link newnode;

 

    newnode=(link) malloc (sizeof(s_list));

    if  (!newnode)

    {

        printf("Memory allocation failure!!!\n");

        return NULL;

    }

    newnode->data=value;

    newnode->next=stack;

    stack=newnode;

    return stack;

}

 

link pop(link stack,int *value)

{

    link top;

 

    if (stack !=NULL)

    {  

          top=stack;

          stack=stack->next;

      *value=top->data;

          free(top);

          return stack;

    }

    else

          *value=-1;

}

 

int empty(link stack)

{

    if (stack ==NULL)

         return 1;

    else

         return 0;

}

 

int is_operator(char operator)

{

    switch (operator)

    {

      case '+':  

      case '-':    

      case '*':   

      case '/':  

        return 1;

 

      default:  

        return 0;

    }

}

 

int priority(char operator)

{

   switch (operator)

   {

      case '+':

      case '-': return 1;

      case '*':

      case '/': return 2;

      default: return 0;

   }

}

 

int two_result(int operator,int operand1,int operand2)

{

    switch (operator)

    {

      case '+':return (operand2 + operand1);

      case '-':return (operand2 - operand1);

      case '*':return (operand2 * operand1);

      case '/':return (operand2 / operand1);

    }

}

 

void main( )

{

   char  expression[50];

   int position=0;

   int op=0;

   int operand1=0;

   int operand2=0;

   int evaluate=0;

 

   printf("Please input the inorder expression :");

   scanf("%s",expression);

 

   while (expression[position]!='\0' && expression[position]!='\n')

   {

         if (is_operator(expression[position]))

         {

            if (!empty(operator))

               while (priority(expression[position]) <= priority(operator->data) && ! empty(operator))

               {

 

                          operand=pop(operand,&operand1);

                          operand=pop(operand,&operand2);

                          operator=pop(operator,&op);

                         operand=push(operand, two_result(op,operand1,operand2));

                         break;

               }

 

                operator=push(operator,expression[position]);

          }

          else

          {

                operand=push(operand,expression[position]-48);

          }

          position++;

       

     }

 

   while (!empty(operator))

   {

      operator=pop(operator,&op);

      operand=pop(operand,&operand1);

      operand=pop(operand,&operand2);

 

      operand =push(operand,two_result(op,operand1,operand2));

   }

 

      operand=pop(operand,&evaluate);

      printf("The expression [ %s] result is '%d'\n",expression,evaluate);

}

 


[C Language][Stack] Using Link List to make Basic Stack working function



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

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

typedef struct s_node s_list;
typedef s_list *link;
link stack=NULL;

void print_stack( )
{
   link temp=NULL;
   temp=stack;

   if (temp==NULL)
       printf("The stack is empty!!\n");
   else
   {
        while (temp!=NULL)
        {
            printf("[%d] ",temp->data);
            temp=temp->next;
        }
        printf("\n");
   }
}

void push(int value)
{
    link newnode;
    newnode=(link) malloc (sizeof(s_list));
    newnode->data=value;
    newnode->next=stack;
    stack=newnode;
}

int pop( )
{
    link top;
    int temp;

    if (stack !=NULL)
    {
        top=stack;
        stack=stack->next;
        temp=top->data;
        free(top);
        return temp;
    }
    else
        return -1;
}

void main ( )
{
      link point;
      int select,value;
    do
    {
        printf("\n[1]Input a stack data");
        printf("\n[2]Output a stack data");
        printf("\n[3]Exit");
        printf("\nPlease select one=>");
        scanf("%d",&select);

        switch (select)
        {
         case 1:
             printf("\nPlease input the data=>");
             scanf("%d",&value);
             push(value);
             printf("[PUSH] The stack content current(top->bottom):");
             print_stack( );
             break;

         case 2:
             value=pop( );
             printf("The output value is [%d]",value);
             printf("\n");
             printf("The stack content current(top->bottom):");
             print_stack( );
             break;
        }
    } while (select !=3);
}

[C Language][Stack] Using Array to make Basic Stack working function


[C Language][Stack] Basic Stack working function



#include <stdlib.h>

#include <stdio.h>

#define MaxSize 10

int stack[MaxSize];

int top=-1;

 

void push(int value)

{

   int i;

 

   if (top >= MaxSize)

      printf("The stack is full!!\n");

   else

   {

        top++;

        stack[top]=value;

 

        printf("[Push] The stack content (top->bottom):");

        for (i=top;i>=0;i--)

            printf("[%d]",stack[i]);

        printf("\n");

   }

}



 

int pop( )

{

     int temp;    

     int i;

 

     if (top <  0)    

     {

            printf("\nThe stack is empty!!\n");

            return -1;

     }

 

     temp=stack[top];    

     top--;        

     printf("The pop value is [%d] \n",temp);

     printf("[POP] The stack content (top->bottom):");

    for (i=top;i>=0;i--)

             printf("[%d]",stack[i]);

     printf("\n");

     return temp;

}

 

 

void main ( )

{

    int select;    

    int stack[5];

    int i,value;

 

 

    do

    {

        printf("\n[1]Input a stack data");

        printf("\n[2]Output a stack data");

        printf("\n[3]Exit");

        printf("\nPlease select one=>");

        scanf("%d",&select);

 

        switch (select)

        {

         case 1: printf("\nPlease input the data=>");

             scanf("%d",&value);

             push(value);

             break;

         case 2: value=pop( );

             break;

        }

 

    } while (select !=3);

}

2011年10月23日 星期日

[Data Structure] tutor



Data Structure

  • Data Structure


    • Basic tree


      • Definition of Tree

      • Binary Tree

      • Expression of Binary Tree

      • Tree traversal


        • Preorder Traversal



          • 1. Visit the root.


            2. Traverse the left subtree.


            3. Traverse the right subtree.


        • Inorder Traversal



          • 1. Traverse the left subtree.    
            2. Visit the root.    
            3. Traverse the right subtree. 


        • Postorder Traversal


          • 1. Traverse the left subtree.
            2. Traverse the right subtree.
            3. Visit the root.