想用c++实现线性表,把线性表作为一个类,进行类的插入删除查找等功能,写了一个头文件和源文件,虽然能运行,但不会显示东西好像没成功,请问哪些地方错了,如何改进
下面是头文件
- #ifndef _LINEAR_H
- #define _LINEAR_H
- #include<iostream>
- using namespace std;
-
- class linear
- {
- class Node
- {
- public:/*这两个设置为私有的无法访问 为什么 都写到类里面了 或者这个写到类外面要怎么处理*/
- int value;
- Node *next;
-
- Node(int x=0)
- {
- value=x;
- next=0;
- }
- } *head;
-
- public:
- void Insert(int pos,int value);//pos位置,value值
- int Delete(int pos,int *value);
- int Find(int value);
- linear()//构造函数
- {
- head=new Node;
- if(!head)
- throw -1;
- }
-
- ~linear()//析构函数
- {
- delete head;
- }
- };
-
- void linear::Insert(int pos,int value)
- {
- Node *p=head;
- int c=1;
-
- while(c<pos||!p->next)
- {
- p=p->next;
- c++;
-
- }
- Node *q=new Node(value);
- if(!q) throw -1;
- q=p->next;
- p->next=q;
- cout<<"已插入"<<value;
- }
-
- int linear::Delete(int pos,int *value)
- {
- Node *p=head;
- int c=1;
-
- while(c<pos||!p->next)
- {
- p=p->next;
- c++;
- }
-
- if(p->next)
- {
- Node *q=p->next;
- *value=q->value;
- p->next=q->next;
- delete q;
- }
- }
-
- int linear::Find(int value)
- {
- Node *p=head->next;
- int c=1;
- while(!p)
- {
- if(p->value==value)
- {
- cout<<"找到";
- c=0;
- return c;
- }
-
- p=p->next;
- c++;
- }
- if(c==1)
- cout<<"未找到";
- return -1;
- }
-
- #endif
-
下面是源文件
- #include"linear.h"
-
- int main()
- {
- linear biao;
- biao.Insert(1,3);
- biao.Insert(2,0);
- biao.Insert(3,7);
- biao.Insert(4,9);
- biao.Find(3);
- int value=3;
- biao.Delete(2,&value);
- return 0;
-
- }
-