单链表尾插法的输入和输出

#include
#include
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef  int Status;
typedef struct List {
    ElemType data;
    struct List* next;
}Node,*LinkList;
Status InitList(LinkList* L);
Node* CrestFromHead(LinkList L);
void PrintfList(LinkList L);
Status InitList(LinkList * L) {
    *L = (LinkList)malloc(sizeof(Node));
    if (!(*L))
    {
        return ERROR;
    }
    (*L)->next = NULL;
}
Node* CrestFromHead(LinkList L) {
    Node* s,*r;
    char c;
    int flag = 1;
    r = L;
    while (flag)
    {
        c = getchar();
        if (c!='$')
        {
            s = (Node*)malloc(sizeof(Node));
            if (!s)
            {
                return ERROR;
            }
            /*头插法s->data = c;
            s->next = L->next;
            L->next = s;*/
            //尾插法
            /*以下做法使L不再指向头节点,所以不采用该种做法
            s->data = c;
            s->next = L->next;
            L->next = s;*/
            s->data = c;
            r->next = s;
            r = s;
        }
        else
        {
            flag = 0;
            r->next = NULL;
        }
    }
}

void PrintfList(LinkList L) {
    LinkList p;
    p = L->next;
    while (p!=NULL)
    {
            printf_s("%c", p->data);
            p = p->next;
        }
    }
int main() {
    LinkList L;
    InitList(&L);
    CrestFromHead(L);
    PrintfList(L);
    return 0;
}

 

你可能感兴趣的:(数据结构)