链表的主函数怎么写( 三 )


以下的代码也有类似的情况 。
5.c++数据结构中链表的主函数中代码实现怎么写#include using namespace std;template struct Node{T data;Node *next;};template class LinkList{private:Node *first;public:LinkList();~LinkList();void Display();void Insert(int i,T x);void Invert();void InsertF(T x);void InsertR(T x);void DeleteAll();};template void LinkList::InsertF(T x){Node *s;s=new Node;s->data=http://www.xuexi88.com/zhishi/x;s->next=first->next;first->next=s;}template void LinkList::InsertR(T x){Node*r,*s;for(r=first;r->next!=NULL;r=r->next);s=new Node;s->data=http://www.xuexi88.com/zhishi/x;s->next=r->next;r->next=s;}template LinkList::LinkList(){first=new Node;first->next=NULL;}template LinkList::~LinkList(){} template void LinkList::Display(){Node *p;for(p=first->next;p!=NULL;p=p->next)coutvoid LinkList::Insert(int i,T x){int count=0;Node*p;for(p=first;p->next!=NULL;p=p->next){if(count=i-1)break;count++;}if(count=i-1){Node*s;s=new Node;s->data=http://www.xuexi88.com/zhishi/x;s->next=p->next;p->next=s;}else throw"Illegal position";}template void LinkList::Invert(){Node*p,*s;p=first->next;first->next=NULL;while(p){s=p;p=p->next;s->next=first->next;first->next=s;}}template void LinkList::DeleteAll(){Node *s,*p;p=first;while(p->next)if(p->next->data>='0'&&p->next->data<='9'){s=p->next;p->next=s->next;delete s;}else p=p->next;}int main(){LinkListA;while(1){char ch;cin>>ch;if(ch='#')break;try{A.InsertR(ch);}catch(const char*message){cout<6.C语言:链表的常用操作,完成下列子函数,并写主函数调用const int NULL =0;template struct Node { T data; //数据域 struct Node * next; //指针域,在这里可省略};template class LinkList{public: LinkList(){first = new Node ; first->next = NULL;}//无参构造函数 LinkList(T a [], int n);//有参构造函数,使用含有n个元素的数组a初始化单链表 ~LinkList(); //析构函数 int GetLength(); //获取线性表的长度 Node * Get(int i); //获取线性表第i个位置上的元素结点地址 int Locate (T x); //查找线性表中值为x的元素,找到后返回其位置 void Insert (int i, T x);//在线性表的第i个位置上插入值为x的新元素 T Delete(int i); //删除线性表第i个元素,并将该元素返回 void PrintList(); //按次序遍历线性表中的各个数据元素 Node * GetFirst(){return first;}private: Node * first; //头指针};#include "linklist.h"#include "iostream"using namespace std;/*template LinkList::LinkList(T a [], int n) //头插法建立单链表{ first = new Node ; first ->next = NULL; //构造空单链表 for (int i=n-1;i>=0;i--) { Node * s = new Node ;//①建立新结点 s->data = http://www.xuexi88.com/zhishi/a[i]; //②将a[i]写入到新结点的数据域 s->next = first->next; //③修改新结点的指针域 first->next = s; //④修改头结点的指针域,将新结点加入到链表中 }}*/template LinkList::LinkList(T a [], int n) //尾插法建立单链表/*① 在堆中建立新结点:Node * s = new Node ;② 将a[i]写入到新结点的数据域:s->data = http://www.xuexi88.com/zhishi/a[i];③ 将新结点加入到链表中: r->next = s;④ 修改尾指针:r = s;*/{ first = new Node ; Node * r = first; for (int i=0;i * s = new Node ;//①建立新结点 s->data = http://www.xuexi88.com/zhishi/a[i]; //②将a[i]写入到新结点的数据域 r->next = s; //③将新结点加入到链表中 r = s; //④修改尾指针 } r->next = NULL; //终端结点的指针域设为空}template LinkList::~LinkList() //析构函数{ Node * p = first; //初始化工作指针p while (p) //要释放的结点存在 { first = p; //暂存要释放的结点 p = p -> next; //移动工作指针 delete first; //释放结点 }}template Node * LinkList::Get(int i) //获取线性表第i个位置上的元素{ Node * p = first -> next; //初始化工作指针 int j = 1; //初始化计数器 while ( p && j != i ) //两个条件都满足,则继续循环 { p = p -> next; //工作指针后移 j++; } if (!p) throw "查找位置非法"; //工作指针已经为空 else return p; //查找到第i个元素}template int LinkList::Locate (T x) //查找线性表中值为x的元素,找到后返回其位置{ Node * p = first ->next; //初始化工作指针 int j=1; while (p) { if (p->data =http://www.xuexi88.com/zhishi/= x) return j; //找到被查元素,返回位置 p = p->next; j++; } return -1; //若找不到,返回错误标识-1}template void LinkList::Insert (int i, T x)//在线性表的第i个位置上插入值为x的新元素{ Node * p = first; //初始化工作指针 if (i != 1) p = Get(i-1); //若不是在第一个位置插入,得到第i-1个元素的地址 。