25
八月

資料結構程式 08/25: ex7.c

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

Node* NewNode()
{
    Node *newNode = malloc(sizeof(Node));//新節點(node) 
    newNode->data = rand()%100;
    newNode->next = NULL;
    return  newNode; 
}
void PrintList(Node *listA)
{
    Node *curNode = listA;
    printf("Print the nodes:");
    while ( curNode !=NULL)//從第一個節點把資料印出來
    {
        printf("%2d ",curNode->data);
        curNode = curNode->next;
    }   
    printf("\n");
}
int main(int argc, char *argv[])//主函數入口
{
    Node *listA = NULL;//->永遠指向第一個的指標 
    Node *curNode = NULL;//->操作(插入或刪除)第N個資料的指標 
    Node *newNode;//->操作(插入或刪除)第N個資料的指標 
    int i;
    srand( (unsigned) time( NULL ) );//srand()是產生亂數的種子
    listA = NewNode();
    PrintList(listA);
    //在這個串列附加上新的資料節點 
    curNode = listA;
    for (i=1; i<20; i++)
    {
        newNode = NewNode();
        if ( newNode->data < listA->data)
        {
             newNode->next = listA;
             listA = newNode;
        }
        else
        {
            curNode = listA;
            while (curNode->next!=NULL && curNode->next->data < newNode->data)
                  curNode = curNode->next;
            newNode->next = curNode->next;
            curNode->next = newNode;
        }
        PrintList(listA);
    }
    return 0; 
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

This entry was posted on 星期一, 八月 25th, 2014 at 03:22:51 and is filed under 103(暑)資料結構. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a reply

Name (*)
Mail (will not be published) (*)
URI
Comment