第19周上机实践项目1——动态链表体验(3)

来源:互联网 发布:海颐软件 编辑:程序博客网 时间:2024/05/06 15:55

问题及代码

/* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作    者:辛彬 * 完成日期:2015 年 1 月 30 日 * 版 本 号:v1.0 * * 问题描述:编写函数delete_first_node(),删除链表中的第一个结点。 * 输入描述:一些整数。 * 程序输出:删除后的链表。 */#include <iostream>using namespace std;struct Node{    int data;            //结点的数据    struct Node *next;  //指向下一结点};Node *head=NULL;    //将链表头定义为全局变量,以便于后面操作void delete_first_node();    //删除链表void make_list2();int main( ){    make_list2();    delete_first_node();    return 0;}void delete_first_node(){    Node *p=head,*q;    q=p;    p=p->next;    delete q;    cout<<"删除第一个节点后链表中的数据为:"<<endl;    while(p!=NULL)    {            cout<<p->data<<" ";        p=p->next;    }    return;}void make_list2(){    int n;    Node *p,*q;    cout<<"输入若干正数(以0或一个负数结束)建立链表:"<<endl;    cin>>n;    while(n>0)    {        p=new Node;        p->data=n;        p->next=NULL;        if(head==NULL)            head=p;        else            q->next=p;        q=p;        cin>>n;    }    return;}

运行结果:

0 0