Algorithm - Create list

Master data structures and algorithms, in particular, linked lists, interview questions often test program

The following is a list created by the end of interpolation

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<malloc.h>
#include <string.h>

typedef  struct node {
    int data;
    struct node *next;
}Node;

Node *LinkListCreat(int n)//链表创建函数
{
    Node *p;
    Node *head,*rear;
    int i = 0; 
    Node *Tal;
    
    head = (Node *)malloc(sizeof(Node));
    rear = head;
    rear->next = NULL;

    for (i = 0; i < n; i++)
    {
        p = (Node *)malloc(sizeof(Node));
         
        printf("请输入第%d个数:", i + 1);
        scanf("%d", &p->data);
        p->next = rear->next;
        rear->next = p;
        rear = p;
    
    }
    return head; 

} 
int LinkListOut (the Node * P) // list output function 
{ 
    the printf ( " output list: \ n- " ); 
    P = p-> Next; // first node points to the first node 
    int I = . 1 ;
     the while (! p-> Data = NULL) 
    { 
        the printf ( " output values of the% d:% d \ n- " , I, p-> Data); 
        I ++ ; 
        P = p-> Next; 
    } 

} 
int   main () 
{ 
    int A = . 5 ;
    int i;
    Node *q;
    q=LinkListCreat(a);
    LinkListOut(q);
    free(q);


}

Output

First interpolation function is as follows

 

* LinkListCreat the Node ( int n-) // list creating function 
{ 
    the Node * P; 
    the Node * head, * REAR;
     int I = 0 ; 
    the Node * Tal; 
    
    head = (the Node *) the malloc ( the sizeof (the Node)); 
    head -> = Next NULL; 

    for (I = 0 ; I <n-; I ++ ) 
    { 
        P = (the Node *) the malloc ( the sizeof (the Node)); 
         
        the printf ( " Please enter the number of D%: " , I +1);
        scanf("%d", &p->data);
        p->next = head->next;
        head->next = p;
    
    }

The output in FIG.

 

Guess you like

Origin www.cnblogs.com/cyyz-le/p/10994397.html