赞
踩
对比c语言实现线性表的链式存储,分析不同之处!
LinkList.h
- #pragma once
-
- //在插入元素时,模板类中应该把每个T保存下来
- //模板类中应该能后 分配节点 缓存节点
- //怎么样缓存===> 做链表
-
- template<typename T>
- struct Node
- {
- T t;
- Node<T> *next;
- };
- template <typename T>
- class LinkList
- {
- public:
- LinkList(void);
- ~LinkList(void);
-
- void clear();
-
- int getLength();
-
- int insert(T &t,int pos);
-
- int get(int pos,T &t);
-
- int del(int pos,T &t);
- private:
- Node<T>*head;//相当于c语言中的链表的头结点
- int length;
- };
-
- #include "LinkList.h"
-
- template <typename T>
- LinkList<T>::LinkList()
- {
- this->head = new Node<T>;
- head->next = NULL;
- this->length = 0;
- }
-
- template <typename T>
- LinkList<T>::~LinkList()
- {
- //删除内部创建的节点
- Node<T> *tmp = NULL;
- while (head!=NULL)
- {
- tmp = head->next;
- delete head;
- head = tmp;
- }
- this->length = 0;
- head = NULL;
- }
-
- template <typename T>
- void LinkList<T>::clear()
- {
- //删除内部创建的节点
- Node<T> *tmp = NULL;
- while (this->head!=NULL)
- {
- tmp = head->next;
- delete head;
- head = tmp;
- }
- this->head = NULL;
-
- //创建头结点
- this->head = new Node<T>;
- head->next = NULL;
- this->length = 0;
- return ;
- }
-
- template <typename T>
- int LinkList<T>::getLength()
- {
- return this->length;
- }
-
- template <typename T>
- int LinkList<T>::insert(T &t,int pos)
- {
- Node<T> *current = this->head;
-
- //移动辅助指针变量current指向2号位置
- for (int i=0;i<pos&&(current->next!=NULL);i++)
- {
- current = current->next;
- }
- //思考一个问题? 新节点(要插入的节点)存在吗
-
- Node<T> *node = new Node<T>;
- if (node==NULL)
- {
- cout<<"func insert() err"<<endl;
- return -1;
- }
- node->t = t;//缓存外部数据
- node->next = NULL;
-
- //让新节点node指向后续节点
- node->next = current->next;
- //当前位置2号 的next域赋值为node
- current->next = node;
-
- this->length++;
- return 0;
- }
-
- template <typename T>
- int LinkList<T>::get(int pos,T &t)
- {
- Node<T> *current = this->head;
-
- //移动辅助指针变量current指向2号位置
- for (int i=0;i<pos&&(current->next!=NULL);i++)
- {
- current = current->next;
- }
- //思考一个问题? 新节点(要插入的节点)存在吗
- t = current->next->t;//把缓存的数据t 传递给调用者
- return 0;
- }
-
- template <typename T>
- int LinkList<T>::del(int pos,T &t)
- {
- Node<T> *current = this->head;
- Node<T> *ret = NULL;
-
- //移动辅助指针变量current指向2号位置
- for (int i=0;i<pos&&(current->next!=NULL);i++)
- {
- current = current->next;
- }
- ret = current->next;//被删除的元素节点
- t = ret->t;//把缓存的t拷贝
-
- current->next = ret->next;
- this->length--;
-
- delete ret;//释放
-
- return 0;
- }
- #include <iostream>
- #include "LinkList.cpp"
- using namespace std;
-
- struct Teacher
- {
- char name[20];
- int age;
- };
-
- void display_linklist()
- {
- Teacher t1,t2,t3;
- Teacher tmp;
- LinkList<Teacher> list;
- t1.age = 10;
- t2.age = 20;
- t3.age = 30;
- list.insert(t1,0);
- list.insert(t2,0);
- list.insert(t3,0);
-
- for (int i =0;i<list.getLength();i++)
- {
- list.get(i,tmp);
- cout<<tmp.age<<endl;
- }
- //链表清空
- list.clear();
-
- list.insert(t1,0);
- list.insert(t2,0);
- list.insert(t3,0);
-
- for (int i =0;i<list.getLength();i++)
- {
- list.get(i,tmp);
- cout<<tmp.age<<endl;
- }
-
- //链表的删除
- while(list.getLength()>0)
- {
- list.del(0,tmp);
- cout<<tmp.age<<endl;
- }
- }
-
- int main()
- {
- display_linklist();
- system("pause");
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。