顺序链表 : 表头添加节点 + 表尾添加节点

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

typedef struct node{

        char value;
        struct node *next;
}node, *Linklist;



void main(){

        Linklist list;

        struct node *n0 = malloc(sizeof(struct node));

        struct node *ni = NULL;

        scanf("%c",&n0->value);
        
        n0->next = NULL;

        do{
                ni = malloc(sizeof(struct node));

                scanf("%c",&ni->value);

                ni->next = n0;

                n0 = ni;

                ni = NULL;

        }while(n0->value != 'q');

        printf("\n");

        struct node *t;

        for(;;){
                if(n0->next == NULL){

                        printf("%c",n0->value);
                        break;

                }else{

                        printf("%c",n0->value);
                        n0 = n0->next;
                }
        }

        printf("\n");


}



在表尾添加节点:

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

typedef struct node{

        char value;
        struct node *next;

}node, *Linklist;



void main(){

        struct node *head = malloc(sizeof(struct node));

        struct node *tail = malloc(sizeof(struct node));

        scanf("%c",&head->value);

        head->next = tail;

        
        struct node *tmp;
        do{


                tmp = malloc(sizeof(struct node));

                scanf("%c",&tmp->value);

                tail->next = tmp;

                tail = tmp;

                tmp = NULL;

        }while(tail->value != 'q');

        
        tmp = head;
        do{

                printf("%c",tmp->value);

                tmp = tmp->next;

        }while(tmp->next != NULL);

        printf("\n");

}














已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页