2018 C language programming (advanced) homework - 3rd homework

The third job

6-1 Odd-valued node linked list

1. Design ideas

Step 1: Store the input value in the linked list
Step 2: Classify and judge the value of the date in the node of the linked list according to the conditions required by the question, and store the date with an even number in a new linked list, and the other will be Odd ones are stored in a new list.

2. Experimental code

struct ListNode *readlist()
    {
        int data;
        struct ListNode *head=NULL,*p=NULL,*tail=NULL;
        scanf("%d",&data);
        while(data != -1){
            p = (struct ListNode *)malloc(sizeof(struct ListNode));
            p->data = data;
            p->next = NULL;
            if(head == NULL){
                head = p;
                tail = p;
            }else{
                tail->next = p;
                tail = p;
            }
            scanf("%d",&data);
    }
        return head; 

    } 

struct ListNode *getodd( struct ListNode **L )
{
     struct ListNode *p = *L,*m=NULL,*n=NULL,*head1=NULL,*head2=NULL;
     head1=(struct ListNode*)malloc(sizeof(struct ListNode));
     head2=(struct ListNode*)malloc(sizeof(struct ListNode));
    head1->next=NULL;
    head2->next=NULL;
    m=head1;
    n=head2;
     while (p) {
           if((p->data)%2 == 1){
                n->next=p;
                n=p;
             }else{
                m->next=p;
                m=p;
                 }
               p = p->next;
     }
     m->next=NULL;
    n->next=NULL;
    *L = head1->next;   
    return head2->next;
}

3. Problems encountered in the debugging process of this question and solutions

without

6-2 Processing of Student Score List

1. Design ideas

Step 1: Store the entered student ID, name and score in the linked list in the createlist function.
Step 2: Traverse the nodes of the linked list in the deletelist function.
Step 3: Return the head node of the linked list after judgment to the main function.

2. Experimental code

struct stud_node *createlist()
{
    struct stud_node *tail=NULL,*head=NULL,*p=NULL;
    int num=0,score=0;
    char name[20];
    scanf("%d",&num);
    while(num!=0)
    {   
        p=(struct stud_node*)malloc(sizeof(struct stud_node));
        p->num=num; 
        scanf("%s %d",p->name,&p->score);
        if(head==NULL)
        {
            head=p;
        }else
        {
            tail->next=p;
        }
        tail=p;
        scanf("%d",&num);
        p->next=NULL;
    }
    return head;
}
struct stud_node *deletelist( struct stud_node *head, int min_score )
{
    struct stud_node *ptr1=NULL,*ptr2=NULL;
    for(;head!=NULL;head=head->next)
    {
        if(head->score>=min_score)
        {
            if(ptr1==NULL)
            {
                    ptr1=head;
            }else
            {
                ptr2->next=head;
            }
            ptr2=head;
        }
    }
    if(ptr1==NULL)
    {
        return NULL;
    }else
    {
        ptr2->next=NULL;
    }
    return ptr1;
}

3. Problems encountered in the debugging process of this question and solutions

without

6-3 Linked list splicing

1. Design ideas

Step 1: First define an array and store the data in the linked list in the array.
Step 2: Reassign the values ​​in the array to the linked list by traversing the for statement;

2. Experimental code

struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2)
{
    int list[100],i=0,j=0,swap=0,count=0;
    while(list1!=NULL)
    {
        list[i]=list1->data;
        i++;
        list1=list1->next;
        count++;
    }
    while(list2!=NULL)
    {
        list[i]=list2->data;
        i++;
        list2=list2->next;
        count++;
    }
    for(i=0;i<count;i++)
    {
        for(j=i+1;j<count;j++)
        {
            if(list[i]>list[j])
            {
                swap=list[i];list[i]=list[j];list[j]=swap;
            }
        }
    }
    struct ListNode *p=NULL,*head=NULL,*tail=NULL;
    for(i=0;i<count;i++)
    {
        p=(struct ListNode*)malloc(sizeof(struct ListNode));
        p->data=list[i];
        p->next=NULL;
        if(head==NULL)
        {
            head=p;
        }else
        {
            tail->next=p;
        }
        tail=p;
    }
    return head;
}

3. Problems encountered in the debugging process of this question and solutions

without

Learning Summary and Progress

1. Summarize the knowledge points learned in the past two weeks and answer the following questions? (Express your understanding in your own words, no points for copying and pasting online)

(1) How to understand an array of pointers, and how does it relate to pointers and arrays? Why can you operate on arrays of pointers with secondary pointers?

I think an array of pointers is an array of pointers that contains both pointers and arrays. Because it is easier to operate with a secondary pointer.

(2) Change any topic of the third PTA assignment (1) of C Advanced to use the second-level pointer to operate on the pointer array.

int getindex(char *s)
{
char *a[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
char **p=&a[0];
//char *b='\0';
int i=0,result=-1;
for(i=0;i<7;i++)
{
    if(strcmp(s,*(p+i))==0)
    {
        return i;
    }
}
return result;
 } 

(3) What are the advantages of handling multiple strings with an array of pointers? Is it possible to directly input multiple strings to an uninitialized array of pointers? Why?

I think using an array of pointers can be more convenient and save space when dealing with multiple strings. No, I don't think the address pointed to by an uninitialized array pointer is deterministic.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324649358&siteId=291194637
Recommended