简单的链表建立与输出
2014-11-25 11:32
381 查看
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <string> using namespace std; struct student_type { int num; float score; struct student_type *next; }; typedef struct student_type Student; Student* create(void) { Student *head=NULL; Student *p1,*p2; int n=0; p1=p2=(Student*)malloc(sizeof(Student)); if(p1!=NULL) { n++; cout<<"\n请输入链表数据(输入0学号表示结束数据输入)————\n"; cout<<"请输入第一个学生的学号 "; cin>>p1->num; if(p1->num!=0) { cout<<"请输入第一个学生的分数 "; cin>>p1->score; head=p1; } else { free(p1); return head; } } else return head; ///开辟新节点 while(1) { p1=(Student*)malloc(sizeof(Student)); if(p1!=NULL) { cout<<"请输入第"<<n+1<<"个学生的学号"<<endl; cin>>p1->num; if(p1->num!=0) { n++; cout<<"请输入第"<<n<<"个学生的分数\n"; cin>>p1->score; p2->next=p1; p2=p1; } else { free(p1); p2->next=NULL; return head; } } } } void print(Student* head) { Student *p=head; cout<<"\n输出链表数据\n"; if(head!=NULL) do { cout<<p->num<<" "<<p->score<<endl; p=p->next; }while(p!=NULL); else cout<<"此链表为空"<<endl; } int main() { Student *head=NULL; int k=0; while(1) { cout<<endl; cout<<"1————创建链表"<<endl; cout<<"2————输出链表"<<endl; cout<<"0————退出"<<endl; cout<<"请选择\n"; cin>>k; switch(k) { case 0: exit(0); case 1: head=create(); continue; case 2: print(head); continue; } } return 0; }
相关文章推荐
- 链表的简单操作(建立,输出,插入,删除)
- 建立和输出一个简单链表
- 第二周(课上例题)——建立和输出一个简单的链表
- 建立和输出一个简单的链表
- 如何建立和输出一个简单链表
- 例 9.8 建立一个如图9.9所示的简单链表,它由3个学生数据的结点组成,要求输出各结点中的数据。
- 建立一个简单链表输出同学的学号,姓名
- 建立一个简单的链表,它由三个学生数据的结点组成输出各节点中的数据。
- 建立和输出一个简单链表
- 链表建立输出删除插入的综合实例
- 尾插法建立链表--顺序输出
- 头插法建立链表--逆序输出
- 创建一个简单的单向链表并将其倒序输出
- 双链表的建立、求长、定位、插入、删除、输出和释放(带头结点且成环状)
- 简单链表添加,然后顺序或逆序输出
- 简单链表的实现和输出
- 链表(建立+插入+删除+输出)
- 链表的建立,输出,删除,插入运算的c语言实现
- 编写creat,del,insert,print函数来建立 删除 插入 输出链表。
- 双向链表的建立以及输出