使用链表反向打印数据

简介: 使用链表反向打印数据
#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
#define  MAX 100
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef int ElmType;
typedef  struct node{
    ElmType data;
    struct node* next;
}Node,*LinkedList;
typedef enum {ERROR=-1,OK = 0} Status;
LinkedList node_create(ElmType data){           
    LinkedList  p=(LinkedList)malloc(sizeof(Node));
    p->data=data;
    p->next=NULL;
    return p;
}
LinkedList Create(LinkedList head,int n){
    LinkedList p=head;
    ElmType i;
    for(i=0;i<n;i++){
        p->next=node_create(i);
        p=p->next;
    }
    return head;
}
Status list_print(LinkedList head){                
    if(head==NULL){
        return ERROR;
    }
    ElmType a[MAX];
    ElmType i=0;
    LinkedList p=head->next;
    for(i=0;i<MAX;i++){
        a[i]=p->data;
        p=p->next;
    }
    for(i=MAX-1;i>=0;i--){
        printf("%d\n",a[i]);
    }
}
int main(int argc, char *argv[]) {
    LinkedList head=node_create(0);   //创造头节点
    Create(head,MAX);                    //创造列表
    list_print(head);                    //打印列表
    free(head);
    return 0;
}
相关文章
|
22天前
【编织时空二:探究顺序表与链表的数据之旅】(下)
【编织时空二:探究顺序表与链表的数据之旅】
|
22天前
|
存储 索引
【编织时空二:探究顺序表与链表的数据之旅】(上)
【编织时空二:探究顺序表与链表的数据之旅】
|
22天前
|
存储 缓存
【编织时空四:探究顺序表与链表的数据之旅】(上)
【编织时空四:探究顺序表与链表的数据之旅】
【编织时空四:探究顺序表与链表的数据之旅】(上)
|
22天前
|
存储 缓存 算法
【编织时空四:探究顺序表与链表的数据之旅】(下)
【编织时空四:探究顺序表与链表的数据之旅】
|
22天前
【编织时空三:探究顺序表与链表的数据之旅】(下)
【编织时空三:探究顺序表与链表的数据之旅】
|
22天前
|
算法
【编织时空三:探究顺序表与链表的数据之旅】(上)
【编织时空三:探究顺序表与链表的数据之旅】
|
22天前
【编织时空一:探究顺序表与链表的数据之旅】(下)
【编织时空一:探究顺序表与链表的数据之旅】
|
22天前
|
存储 C语言 索引
【编织时空一:探究顺序表与链表的数据之旅】(上)
【编织时空一:探究顺序表与链表的数据之旅】
|
6月前
数据结构单链表之交换链表中的节点而不交换数据 | 第八套
数据结构单链表之交换链表中的节点而不交换数据 | 第八套
30 0
|
12月前
|
缓存 NoSQL API
【Redis基础知识 六】Redis底层数据编码之链表
【Redis基础知识 六】Redis底层数据编码之链表
42 0
【Redis基础知识 六】Redis底层数据编码之链表