Posts Tagged ‘資工2’

10
八月

暑修資料結構 程式 08/10: ex2.c

   Posted by: admin    in 104(暑)資料結構

#include <stdio.h>
int main()
{
    int iData[10]={12, 5, 7, 16, 13, 19, 6, 18, 4, 11};
    int i,j, tt;
    for (i=0; i<10; i++)
       printf("%2d ", iData[i]);
    printf("\n");
    for (i=9; i>0; i--)
    {
        for (j=1; j <=i; j++)
        {
            if (iData[j-1] > iData[j])
            {
                tt = iData[j-1];
                iData[j-1] = iData[j];
                iData[j] = tt;
            }
        }
        for (j=0; j<10; j++)
             printf("%2d ", iData[j]);
        printf("\n");
    }
 
    return 0;
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

10
八月

暑修資料結構 程式 08/4: ex1.c

   Posted by: admin    in 104(暑)資料結構

#include <stdio.h>
int main()
{
	int x, y;
	int s[10][10];
	for (x=1; x <=9; x++)
	{
		for (y=1; y <=9; y++)
		{
			s[x][y]= x*y;
			printf("%2d x %2d = %2d\n", x, y, s[x][y]);
		}		
	}
	return 0;
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

3
九月

資料結構程式 09/03: ex11.c

   Posted by: admin    in 103(暑)資料結構

#include <stdio.h> //標準輸出入函數的標頭檔
#include <stdlib.h>
typedef struct node//節點結構的自定型別定義 
{
    int data;//整數 
    struct node *left;//節點的指標 
    struct node *right;//節點的指標
} Node;
Node* NewNode()
{
    Node *newNode = malloc(sizeof(Node));//新節點(node) 
    newNode->data = 0;
    newNode->left = NULL;
    newNode->right = NULL;
    return  newNode; 
}
void Travel(Node* curNode)
{
     if (curNode==NULL)
        return;
     Travel(curNode->left);//走訪左子樹
     printf("%3d ", curNode->data);//走訪樹根
     Travel(curNode->right);//走訪右子樹
}
int main(int argc, char *argv[])//
{//程式寫在main()函數裡
    Node* Tree = NewNode();
    Node* curNode = NULL;
    int i, newdata;
    srand( (unsigned) time( NULL ) );//srand()是產生亂數的種子
    //插入20個資料項到這個樹
    newdata = rand()%100;
    Tree->data = newdata;
    for (i=1; i< 20; i++)
    {
        newdata = rand()%100;
        curNode = Tree       
        while (1)
        {
              if (newdata < curNode->data)
              {
                 if (curNode->left == NULL)
                 {
                     curNode->left = NewNode();
                     curNode->left->data = newdata;
                     break;                                
                 }
                 else
                     curNode = curNode->left;
              }
              else
              {
                 if (curNode->right == NULL)
                 {
                     curNode->right = NewNode();
                     curNode->right->data = newdata;
                     break;                                   
                 }
                 else
                     curNode = curNode->right;
              }
        }        
    }
    //使用中序列印出整個二元搜尋樹
    Travel(Tree);
    return 0;

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

1
九月

資料結構程式 09/01: ex10.c

   Posted by: admin    in 103(暑)資料結構

#include <stdio.h> //標準輸出入函數的標頭檔
#include <stdlib.h>
void Travel(int* Tree, int curNode)
{
     if (Tree[curNode]==-1)
        return;
     Travel(Tree, curNode * 2);//走訪左子樹
     printf("%3d ", Tree[curNode]);//走訪樹根
     Travel(Tree, curNode * 2 + 1);//走訪右子樹
}
int main(int argc, char *argv[])//主函數入口
 
    int Level = 12;
    int Nodes = 8192;
    int Tree[Nodes];
    int i, j, data, curNode, curLevel;
    srand( (unsigned) time( NULL ) );//srand()是產生亂數的種子
    //讓每個節點啟始值是-1表示是空的 
    for (i=0; i< Nodes; i++)
        Tree[i] = -1;
    //插入20個資料項到這個樹
    for (i=0; i< 20; i++)
    {
        data = rand()%100;
        curNode = 1;//根節點是編號是1 
        curLevel = 1;//目前階層 
        while (curLevel <= Level)
        { 
          if (Tree[curNode] == -1)//目前節點是空的
          { 
            Tree[curNode] = data;
            break;
          } 
          else if (data < Tree[curNode])
            curNode = 2 * curNode;
          else //if (data >= Tree[curNode])
              curNode = 2 * curNode +1;
          curLevel++; 
        } 
    }
    //使用中序列印出整個二元搜尋樹
    Travel(Tree, 1);
    return 0; 
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

29
八月

資料結構程式 08/29: ex9.c

   Posted by: admin    in 103(暑)資料結構

Node* push(Node *stack, int indata)
{
     Node *newNode = malloc(sizeof(Node))//新節點(node) 
     newNode->data = indata;
     newNode->next = stack;
     return newNode;
}
Node* pop(Node *stack)
{
    Node *oldnode = stack;
    stack = stack->next;
    free(oldnode);
    return stack;
}
int main()
{
    Node *stack = NULL;
    int i;
    srand( (unsigned) time( NULL ) )//srand()是產生亂數的種子
    //在這個堆疊上push資料 
    for (i=1; i<5; i++)
 
        stack = push(stack, rand()%100);
        PrintList(stack);
    }
    //在這個堆疊上pop資料 
    for (i=1; i<5; i++)
 
        stack = pop(stack);
        PrintList(stack);
    }
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,