/*
*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;
}
运行结果截图:
知识点总结:
线性表的存储方式!
学习心得:
天行健,君子以自强不息;地势坤,君子以厚德载物!