本程序主要体现了线性表的链式存储结构,主要实现了以下几个功能:
//构造函数--定义头结点,链表长度为0
//析构函数--释放所有结点空间
//构造函数(含参)--W把数组和n值传入给链表
//获取链表的长度--L
//按位查找,第i个元素--G(i)
//按值查找,返回位--C(x)
//插入操作--I(i,x)
//删除操作,删除第--D(i)
//判断是否为空--E
//遍历操作,依次输出--P
//输入Q,退出程序
下面是代码,如有不足的地方还请各位大佬多多指正:
//数据结构学习笔记(C++):线性表的链式存储(单链表) #includeusing namespace std; struct Node{//定义指针结构体 int data;//数据域 Node *next;//指针域 }; class linkList{ public: linkList();//构造函数 ~linkList();//析构函数 public: int getLength();//获取链表的长度 int getElem(int i);//按位查找,第i个元素 int locateElem(int x);//按值查找,返回位置 void insertElem(int i, int x);//插入操作,i位置插入x int deleteElem(int i);//删除操作,删除第i个 bool isEmpty();//判断是否为空 void printAllElem();//遍历操作,依次输出 void initList(int a[], int n);//构造函数,用数组进行链表的初始化 private: Node *first;//单链表的头指针 [引用-结点数据类型] int length;//定义链表的长度 }; linkList::linkList()//构造函数 { first = new Node;//生成新结点 first->next = NULL;//头指针为空 length = 0;//链表长度为 0 } linkList::~linkList()//析构函数:释放所有结点空间 { Node *p; while(first != NULL) { p = first; first = first->next; delete p; } } int linkList::getLength()//取表长函数 { return length; } int linkList::getElem(int i)//按位查找 { int x;//存放返回值 int count = 1;//计数器 Node* p = first->next;//工作指针初始化 while(p != NULL && count < i) { p = p->next; count++; }//count = i 时,跳出循环 x = p->data;//将此时的值存入x中去 if(p == NULL || i > length || i < 1) throw "查找位置输入错误"; return x; } int linkList::locateElem(int x)//按值查找 { Node * p = first->next;//定义工作指针,初始化指向头结点指针域 int count = 1;//计数器从 1 开始 while(p != NULL) { if(p->data == x) return count; p = p->next; count++; } return 0; } void linkList::insertElem(int i, int x)//插入操作 { Node* p = first, *s = NULL; int count = 0; while(p != NULL && count < i-1) { p = p->next; count++; }//循环结束(count = i-1,结点p也是指向的 i-1 的位置) ; if(p == NULL) throw "插入位置错误"; else{ s = new Node;//申请结点s, s->data = x;//数据域为x s->next = p->next;//p的指针给予了s p->next = s; //s->p:拿来吧你。(原本p指向下一个结点的指针被s抢夺过来了,这样s就能指向原来p指向的下一个结点了,属于完美的夺权篡位) } length++; } int linkList::deleteElem(int i)//删除操作 { int x; Node* p = first, *q = NULL; int count = 0; while(p != NULL && count < i-1)//查找到第 i-1 个元素的前一个结点 { p = p->next; count++; }//循环结束,(count = i-1,结点p也是指向的 i-1 的位置) ; 证明找到了要删除的结点 if(p == NULL || p->next == NULL) //结点p不存在,或结点p的后续结点不存在 throw "删除位置错误。"; else{//没有出现错误,那就继续 q = p->next;//将p指向被删结点 x = q->data;//暂存被删结点中的数据 p->next = q->next;//完美衔接 delete q;//删除q结点 length--;//链表长度减一 return x;//被删元素 } } void linkList::initList(int a[], int n)//有参构造函数:尾插法 { length = 0; first = new Node;//申请头结点 first->next = NULL;//指针域为NULL for(int i = n-1; i >= 0; i--)//这个地方比较有意思,如果你是 i = 0->n,那么链表中输出的就是倒序,有点像栈,很有意思 { Node *s = NULL;//定义工作指针s s = new Node;//申请新结点 s->data = a[i];//将数组a中的数据存放入s结点的数据域 s->next = first->next;//原头结点的指针给予给s(那不就是NULL嘛,就是让s做尾结点了呗,——>尾插法) first->next = s;//头结点的指针指向s length++; } } bool linkList::isEmpty()//判空函数 { if(first->next == NULL) return true; else return false; } void linkList::printAllElem()//遍历操作,依次输出 { Node* p = first->next;//工作指针p初始化 while(p != NULL) { cout< data<<"t"; p = p->next; } cout< >command) { if(command == 'Q') return 0; switch(command) { case 'W': cout<<"输入3个元素值:"< >a[i]; list.initList(a,3); cout<<"新建完成。"< >i; cout<<"你找的元素是:"< >x; cout<<"他在第"< >i>>x; list.insertElem(i,x); cout<<"插入完成"< >i; cout<<"元素:"<



