zoukankan      html  css  js  c++  java
  • 编写打印一个单链表所有元素的程序

    #include<stdio.h>
    #include<stdlib.h>
    struct Node;
    typedef struct Node *PtrToNode;
    typedef PtrToNode List;
    typedef PtrToNode Position;
    
    struct Node{
        int  Ele;
        Position Next;
    };
    void
    PrintArray(List L)
    {
        Position p;
        p = L;
        while( p->Next != NULL )
        {
            printf("%d",p->Next->Ele);
            p = p->Next;
        }
    }
    Position
    CreateTable( void )
    {
        PtrToNode L;
        L = malloc( sizeof( struct Node ) );
        if( L == NULL){
            printf("out of space ");
            return ;
        }
        L->Next = NULL;
        return L;
    }
    
    int main()
    {
    
        int i;
        Position L,p;
        PtrToNode TmpCell;
        L = CreateTable();
        p = L;
        for(i = 0; i<3; i++ )
        {
            TmpCell = malloc(sizeof(struct Node ));
            if(TmpCell == NULL)
            {
                printf("error");
                return;
            }
            p->Next = TmpCell;
    
            TmpCell->Ele = i;
            TmpCell->Next = NULL;
    
            p = TmpCell;
        }
        PrintArray( L );
    }
    View Code

     这里面涉及到:for循环做一个链表由下往上做,链表却是由上往下

  • 相关阅读:
    第二阶段团队冲刺第五天
    第二阶段冲刺七
    第二阶段冲刺六
    第二阶段冲刺五
    第二阶段冲刺四
    冲刺第二阶段三
    冲刺第二阶段二
    冲刺第二阶段 一
    项目总结
    第二阶段SCRUM
  • 原文地址:https://www.cnblogs.com/gabygoole/p/4620786.html
Copyright © 2011-2022 走看看