zoukankan      html  css  js  c++  java
  • 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; }
  • 相关阅读:
    将ASCII字符串转换为UNICODE字符串
    GetLastError()返回值大全
    C++构造函数的调用
    DOM – 7.动态创建DOM + 8.innerText innerHTML value
    DOM
    DOM – 4.doucument属性
    用jquery操作xml文件
    請推薦有關網路的書
    Linux命令全称
    轻松架设时时监控工具Cacti
  • 原文地址:https://www.cnblogs.com/andrew3/p/8666922.html
Copyright © 2011-2022 走看看