TZOJ 数据结构实验:单链表元素插入

描述

实现函数CreateHeader用于创建空链表,实现Insert函数并调用它完成带头节点链表的创建。 

部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码。

 

void PrintLinkList(Node *head)
{
    int flag = 0;
    Node *p = head->next, *q;
    while(p)
    {
        if(flag)
            printf(" ");
        flag = 1;
        printf("%d", p->data);
        q = p;
        p = p->next;
        free(q);
    }
    free(head);
}

int main()
{
    int n, d;

    Node *head;
    CreateHeader(&head);
    scanf("%d", &n);
    while(n--)
    {
        scanf("%d", &d);
        Insert(head, d);
    }
    PrintLinkList(head);
    return 0;
}

输入

 

 

输入n和n个节点,每个元素都插入到链表的最前面。

 

 

输出

 

 

输出创建好的链表并清空。

 

 

样例输入

 3

1 2 3

样例输出

 3 2 1

#include <iostream>
#include <stdio.h> #include <stdlib.h> #include <malloc.h> using namespace std; typedef struct Node { int data; struct Node *next; } Node; void CreateHeader(struct Node **head) { *head=(Node*)malloc(sizeof(Node)); (*head)->next=NULL; } void Insert(Node *head,int n) { Node* s; s=(Node*)malloc(sizeof(Node)); s->next=head->next; head->next=s; s->data=n; } void PrintLinkList(Node *head) { int flag = 0; Node *p = head->next, *q; while(p) { if(flag) printf(" "); flag = 1; printf("%d", p->data); q = p; p = p->next; free(q); } free(head); } int main() { int n, d; Node *head; CreateHeader(&head); scanf("%d", &n); while(n--) { scanf("%d", &d); Insert(head, d); } PrintLinkList(head); return 0; }

 

 

posted @ 2018-03-28 22:45  极限之旅  阅读(961)  评论(0编辑  收藏  举报