第四周项目1-两种方法建立单链表

/*
 *Copyright (c) 2017, 烟台大学计算机学院
 *All rights reserved.
 *文件名称:渣.cpp
 *作    者:朱坤昂
 *完成日期:2017年9月13日
 *版 本 号:v1.0
 *
 *问题描述:顺序表基本算法的实现
 *输入描述:一组数
 *程序输出:见程序运行截屏
*/
#include<bits/stdc++.h>
using namespace std;
typedef struct node
{
    int data;
    node *next;
}danlist;
danlist *head,*head1;
struct node * creat0(int num)///尾插法
{
    int i;
    danlist *p,*r;
    head=(struct node *)malloc(sizeof(struct node));
    r=head;
    for(i=0;i<num;i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        p->next=NULL;
        cin>>p->data;
        r->next=p;
        r=p;
    }
    return head;
}
struct node * creat1(int num)///头插法
{
    danlist *p;
    int i;
    head1=(struct node *)malloc(sizeof(struct node));
    head1->next=NULL;///不要忘了这个
    for(i=0;i<num;i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        cin>>p->data;
        p->next=head1->next;
        head1->next=p;
    }
    return head1;
}
void display(danlist *p)
{
    p=p->next;
    while(p->next!=NULL)
    {
        cout<<p->data<<' ';
        p=p->next;
    }
    cout<<p->data;
}
int main()
{
    int i,d,f;
    cin>>d;
    head=creat0(d);
    display(head);
    cin>>f;
    head1=creat1(f);
    display(head1);
    return 0;
}


运行结果截图:



知识点总结:

  线性表的存储方式!

学习心得:
  天行健,君子以自强不息;地势坤,君子以厚德载物!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值