DOC

chugao

By Albert Hayes,2014-09-15 17:14
6 views 0
chugao

#include<stdio.h>

    #include<stdlib.h>

    #include<string.h>

/*’?e??‘Ì */

    typedef struct node *ptNode;

struct node

    {

     char id[5];

     short score;

     ptNode lLink,rLink; }sNode;

    ptNode Create(int n) {

     ptNode head,ptr,newNode;

     int i;

     char *p;

     if((head = (ptNode)malloc(sizeof(sNode))) == NULL)

     {

     printf("Malloc Space Failed!\n");

     exit(0);

     }

     head->id[0] = '\0';

     head->lLink = NULL;

     head->rLink = NULL;

     ptr = head;

     for(i=1; i<n+1; i++)

     {

     int k=5;

     char c_In;

     if((newNode = (ptNode) malloc(sizeof(sNode))) == NULL)

     {

     printf("cannot find space!\n");

     exit(0);

     }

     ptr->rLink = newNode;

     printf("Please input the %d man's id:\n",i);

     scanf("%s",newNode->id);

     if(strlen(newNode->id) > 5)

     {

     printf("The Student's is too long! Input again? (Y/N)?");

     scanf("%s",&c_In);

     if((c_In == 'Y') || (c_In == 'y'))

     {

     printf("Please input the %d man's id:\n",i);

     scanf("%s",newNode->id);

     }

     else if((c_In == 'N') || (c_In == 'n'))

     {

     exit(0);

     }

     }

    /********************************************************/

     if(strlen(newNode->id) < 5)

     {

     p = &(newNode->id[strlen(newNode->id)]);

     do

     {

     newNode->id[k] = *p;

     *p = '0';

     p--;

     k--;

     }

     while(p != (newNode->id-1));

     }

    /*****************************************************/

     printf("Please input the %d man's score:\n",i);

     scanf("%d",&newNode->score);

     if((newNode->score <= 0) || (newNode->score > 100))

     {

     newNode->score = 0;

     printf("Scores override the normal size!\n");

     printf("Please input the %d man's score :\n",i);

     scanf("%d",&newNode->score);

     newNode->lLink = ptr;

     newNode->rLink = NULL;

     ptr = newNode;

     }

     else

     {

     newNode->lLink = ptr;

     newNode->rLink = NULL;

     ptr = newNode;

     }

     }

     head->lLink = newNode;

     ptr->rLink = head;

     return (head);

}

void output(ptNode head)

    {

     ptNode ptr;

     ptr = head->rLink;

     printf("\nNow the double list is:\n");

     while(ptr != head)

     {

     printf("The %s man's",ptr->id);

     printf("\t");

     printf("%d \n",ptr->score);

     ptr = ptr->rLink;

     }

     printf("\n");

}

ptNode sort(ptNode head)

    {

     ptNode endPtr,Ptr;

     int *p1;

     int* p2;

     for(endPtr=head->rLink;endPtr->rLink;endPtr=endPtr->rLink)

     {

     for(Ptr=endPtr->rLink;Ptr;Ptr=Ptr->rLink)

     {

     if((strcmp(endPtr->id,Ptr->id)) > 0)

     {

     p2 = Ptr->rLink->lLink;

     Ptr->rLink->lLink = Ptr->lLink;

     Ptr->lLink = endPtr->lLink;

     endPtr->lLink =p2;

     p1 = head->rLink;

     head->rLink = endPtr->rLink;

     endPtr->rLink =Ptr->rLink;

     Ptr->rLink = p1;

     }

     }

     }

     return(head);

}

    void main(void) {

     ptNode head;

     head = Create(4);

     sort(head);

     output(head);

}

Report this document

For any questions or suggestions please email
cust-service@docsford.com