C++实现单链表数据结构
myList.h
#ifndef MYLIST_H
#define MYLIST_H
typedef struct node
{
int data;
struct node *next;
}Node;
class myList
{
public:
myList();
~myList();
void initList(); // 初始化单链表
int getData(int pos); //得到pos位置的元素
void insert(int val, int pos); //将值为val的元素插入到单链表的pos位置
int remove(int pos); //删除第pos个结点,并返回该结点的值
void reverse(); //对单链表进行转置
int searchValue(int val); //寻找值为val的结点在单链表中的位置
int getLength(); //返回单链表的长度
void print(); //遍历打印单链表
private:
Node *head; //头结点指针
Node *cur; //当前结点指针
int length; //单链表长度
};
#endif
myList.cpp
#include <iostream>
#include "myList.h"
using namespace std;
myList::myList()
{
head = NULL;
cur = NULL;
length = 0;
}
myList::~myList()
{
if (NULL != head)
{
cur = head; //从头开始删除结点
while (0 != length)
{
head = cur->next;
delete cur;
cur = head;
length--;
}
}
}
void myList::initList()
{
int val;
while (cin >> val )
{
Node *p = new Node;
p->data = val;
p->next = NULL;
if (NULL == head) //单链表为空
{
head = p;
cur = p;
}
//单链表不为空
else
{
cur->next = p;
cur = p;
}
length++;
}
}
int myList::getData(int pos)
{
if (pos >= length)
{
cerr << "overflow" << endl;
return (-1);
}
else
{
cur = head;
int count = 0;
while(count != pos)
{
cur = cur->next;
count++;
}
cout << cur->data << endl;
return cur->data;
}
}
void myList::insert(int val, int pos)
{
if (pos > length)
{
cerr << "pos error" << endl;
}
else
{
Node *p = new Node;
p->data = val;
p->next = NULL;
if (0 == pos) //插入到最前面
{
cur = head;
head = p;
head->next = cur;
}
//插入到其他位置
else
{
cur = head;
int count = 0;
while (count != pos-1)
{
cur = cur->next;
count++;
}
p->next = cur->next;
cur->next = p;
}
length++;
}
}
int myList::remove(int pos)
{
if (pos > length && pos < 0)
{
cerr << "pos error" << endl;
return (-1);
}
else
{
cur = head;
int count = 0, val;
if (0 == pos) //删除首结点
{
cur = head->next;
val = head->data;
delete head;
head = cur;
}
//删除其他结点
else
{
while (count != pos-1)
{
cur = cur->next;
count++;
}
Node *p = cur->next;
cur->next = cur->next->next;
val = p->data;
delete p;
}
length--;
return val;
}
}
void myList::reverse()
{
Node *p1, *p2, *p3;
if (NULL == head || NULL == head->next)
{
return;
}
else
{
p1 = head, p2 = p1->next;
while(p2)
{
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
}
head->next = NULL;
head = p1;
}
}
int myList::searchValue(int val)
{
if (length <= 0)
{
cerr << val << " is not in list" <<endl;
return (-1);
}
else
{
cur = head;
int count = 0;
while (NULL != cur)
{
if (val == cur->data)
{
cout << val << " is in the list , the pos is " << count << endl;
return count;
}
cur = cur->next;
count++;
}
cerr << val << " is not in list" <<endl;
return (-1);
}
}
inline int myList::getLength()
{
cout << "the length of the list is " << length << endl;
return length;
}
void myList::print()
{
if (length > 0)
{
cur = head;
cout << "The list is:" << endl;
while (NULL != cur)
{
cout << cur->data << " ";
cur = cur->next;
}
cout << endl;
}
else
cout << "The list is empty!" << endl;
}
int main()
{
myList ilist;
ilist.initList();
ilist.reverse();
ilist.print();
ilist.getLength();
ilist.searchValue(5);
ilist.getData(5);
ilist.getData(2);
ilist.insert(31,3);
//cout << ilist.remove(1) <<endl;
ilist.reverse();
ilist.print();
ilist.getLength();
return 0;
}