单链表

来源:互联网 发布:nba2k17詹姆斯身体数据 编辑:程序博客网 时间:2024/06/08 06:19
#include <stdio.h>#include <stdlib.h>//definition for the singly-linked list.struct ListNode{    int val;    struct ListNode *next;};//convert an array of integer to a singly-linked list.void build(struct ListNode* head, int* nums, int numsSize){    struct ListNode *p, *q;    p = head;    for(int i=0; i<numsSize; ++i){        q = (struct ListNode*)malloc(sizeof(struct ListNode));        q->next = NULL;        q->val = nums[i];        p->next = q;        p = q;    }}//print the given singly-linked list.void print(const struct ListNode* head){    struct ListNode* p = head->next;    while(p != NULL){        printf("%d ", p->val);        p = p->next;    }}int main(){    struct ListNode* L = (struct ListNode*)malloc(sizeof(struct ListNode));    L->next = NULL;    L->val = -1;    int A[] = {1,2,3,4,5,6};    build(L, A, 6);    print(L);    return 0;}
0 0
原创粉丝点击