当前位置:网站首页>6-8 creating and traversing linked lists

6-8 creating and traversing linked lists

2022-06-11 17:37:00 Tomatoes_ Menon

This topic requires the implementation of the following three functions , Achieve the following functions :

Enter several positive integers , With -1 end , Take the way of adding nodes to the linked list to establish a one-way linked list . Traverse and output this one-way linked list .

Function interface definition :

 Add node functions to the tail of the linked list :
struct link *AppendNode(struct link *head,int data);
head: Chain header node pointer , If head by NULL, A new header node will be created 
data: Nodes to add data The number 
 The function returns : The node pointer of the chain header after adding nodes 
 Traversal display linked list function :
void DisplyNode(struct link *head);
head : Chain header node pointer 

Sample referee test procedure :

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

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

struct link *AppendNode(struct link *head,int data);
void DisplyNode(struct link *head);
void DeleteMemory(struct link *head);

int main()
{
    char    c;
    int data = 0;
    struct link *head = NULL;      /*  Chain header pointer  */
    while (1)
    {
        scanf("%d",&data);
        if (data==-1)
            break;

        head = AppendNode(head,data);/*  towards head Add a node to the end of the linked list of the header pointer  */
    }
    DisplyNode(head);        /*  Displays the information of each node in the current linked list  */
    DeleteMemory(head);           /*  Free all dynamically allocated memory  */
    return 0;
}
/* In this way  void DeleteMemory(struct link *head);*/
void DeleteMemory(struct link *head)
{
    struct link *p, *pnext;
    p = head;
    while (p != NULL)
    {
        pnext = p->next;
        free(p);
        p = pnext;
    }

}

/* In this way  struct link *AppendNode(struct link *head,int data); */

/* In this way  void DisplyNode(struct link *head); */


/*  Please fill in the answer here  */

Input format :

A series of positive integers , With -1 end

Output format :

Output each item in the one-way linked list , Between each number -> Connect

sample input :

Here's a set of inputs . for example :

1 3 5 7 9 -1

sample output :

Here is the corresponding output . for example :

1->3->5->7->9

struct link *AppendNode(struct link *head,int data)
{
    struct link *p=NULL, *q=head; 
    p=(struct link*)malloc(sizeof(struct link));
    if(p==NULL){
        exit(0);
    }
    if(head==NULL){
        head=p;
    }
    else{
       while(q->next!=NULL){
           q=q->next;
           }
        q->next=p;
    }  
    p->data=data;
    p->next=NULL;
    return head;
}
void DisplyNode(struct link *head)
{
    struct link*p;
    p=head;
    int i=1;
    while(p!=NULL)
    {
        
        if(i==1){
            printf("%d",p->data);
        }else{
        printf("->%d",p->data);
        }
        i++;
        p=p->next;
    }
}

原网站

版权声明
本文为[Tomatoes_ Menon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111717106301.html