.h
#include
#include
#include
using namespace std;
typedef char elemtype; // 元素类型便于修改
typedef struct NODE
{
elemtype date; // 数据域
struct NODE *next; // 指针域
}Node;
void initList(Node **head); // 链表的初始化
void createList(Node *head); // 创建链表
void show(Node *head); // 显示链表的数据
// 查找
void findP(Node *head); // 按元素位置查找元素
void findE(Node *head); // 按元素查找元素
// 插入、增加
void insertP(Node *head); // 插入节点 ,按位置
void insertE(Node *head); // 插入节点 ,在某个元素之后
//删除
void deleteP(Node *head); // 删除节点 ,按位置
void deleteE(Node *head); // 删除节点 ,在某个元素之后
// 反转
void reaverse(Node *head); // 链表的反转
.cpp
// 带头结点(头结点数据域有值) 头结点初始化
// 要修改head 的值,传入的是二级指针 带头结点
void initList(Node **head) // 链表的初始化
{
*head = (Node *)malloc(sizeof(Node));
(*head)->next = NULL;
}
void createList(Node *head) // 创建链表 带头节点 尾插法
{
elemtype inputDate;
Node *inputN = (Node *)malloc(sizeof(Node));
Node *tempN = NULL;
head->next = inputN;
cout << endl;
cout << "单链表,输入节点(以#结束):";
while (1)
{
cin >> inputDate;
if (inputDate != '#')
{
tempN = inputN; // 如果是最后一个
inputN->date = inputDate;
// 如果将节点分为当前,前一个,和后一个,等价于前一个
inputN = (Node *)malloc(sizeof(Node)); // 开辟接下来的节点空间
tempN->next = inputN; // inputN 等价于后一个
}
else
{
if (inputDate == '#' && tempN == NULL) // 第一个元素输入的是#,即空链表
{
cout << "该链表为空。";
head->next = NULL;
break;
}
tempN->next = NULL;
free(inputN);
break;
}
}
cout << endl;
}
void show(Node *head) // 显示链表的数据
{
int flag = 0;
head = head->next; // 带头结点 没有值 因此是下一个
cout << "链表为";
while (head != NULL)
{
cout << head->date;
head = head->next;
if (head != NULL)
cout << "->";
flag = 1;
}
if (flag == 0)
{
cout << "空。";
return;
}
cout << endl;
}
void findP(Node *head) // 按元素位置,查找元素
{
int point;
int count = 1;
cout << "查找元素的位置:";
cin >> point;
head = head->next;
while (head != NULL)
{
if (count == point)
{
cout << "元素已经找到,该位置的元素为:";
cout << head->date << endl;
break;
}
head = head->next;
count++;
}
}
void findE(Node *head) // 按元素,查找元素
{
elemtype element;
int count = 1, flag = 0;
cout << "要查找的元素:";
cin >> element;
head = head->next;
while (1)
{
if (head->date == element)
{
cout << "该元素的位置为:" << count << endl;
flag = 1;
}
head = head->next;
count++;
if (head == NULL)
{
if (flag == 0)
cout << "该元素不存在。" << endl;
break;
}
}
}
void insertP(Node *head) // 插入节点,按位置
{
int point, count = 1;
//Node *insertEle = NULL;
Node *insertEle = (Node *)malloc(sizeof(Node));
elemtype ele;
cout << "插入的位置:";
cin >> point;
cout << "插入的元素:";
cin >> ele;
// Node *insertEle = NULL; insertEle -> date = ele;
insertEle->date = ele;
while (head != NULL)
{
if (count == point)
{
insertEle->next = head->next;
head->next = insertEle;
break;
}
head = head->next;
count++;
}
if (head == NULL)
{
cout << "输入位置不不正确" << endl;
}
}
void insertE(Node *head) // 插入节点 ,在某个元素之后
{
int count = 1;
Node *insertEle = (Node *)malloc(sizeof(Node));
elemtype ele;
elemtype insEle;
cout << "输入在那个元素后面插入:";
cin >> ele;
cout << endl;
cout << "插入的元素为:";
cin >> insEle;
insertEle->date = insEle;
while (head != NULL)
{
if (head->date == ele)
{
insertEle->next = head->next;
head->next = insertEle;
break;
}
head = head->next;
count++;
}
if (head == NULL)
{
cout << "该元素不存在。" << endl;
}
}
void deleteP(Node *head) // 删除节点 ,按位置
{
int point = 0;
int count = 1;
cout << "删除元素的位置:";
cin >> point;
Node *deleteN = (Node *)malloc(sizeof(Node));
while (head != NULL)
{
if (count == point)
{
cout << "删除的元素已经找到。" << endl;
deleteN = head->next;
head->next = head->next->next;
delete(deleteN);
point = -1;
break;
}
head = head->next;
count++;
}
if (point != -1)
{
cout << "位置输入错误" << endl;
}
}
void deleteE(Node *head) // 删除节点 ,在某个元素之后
{
int flag = 0;
elemtype deleteEle;
cout << "删除的元素:";
cin >> deleteEle;
Node *deleteN = (Node *)malloc(sizeof(Node)); // 删除的节点,即为当前节点
Node *tempN = (Node *)malloc(sizeof(Node)); // 临时节点存贮的是前一个节点
deleteN->date = deleteEle;
while (head != NULL)
{
deleteN = head;
if (flag == 1)
{
tempN->next = deleteN->next;
delete(deleteN);
break;
}
if (head->date == deleteEle)
{
cout << "删除的元素已经找到." << endl;
flag = 1;
}
else
{
tempN = head; // 保存当节点,对于下一句语句来说 是上一个节点
head = head->next;
}
}
if (flag != 1)
{
cout << "删除的元素不存在" << endl;
}
}
void reaverse(Node *head) // 链表的反转
{
if (head->next == NULL || head->next->next == NULL)
{
cout << "链表为空" << endl;
return;
}
Node *tailN = head->next;
Node *currentN = tailN->next;
Node *previousN = NULL;
while (currentN != NULL)
{
previousN = currentN->next;
currentN->next = tailN;
tailN = currentN;
currentN = previousN;
}
head->next->next = NULL;
head->next = tailN;
}
main.cpp
int main()
{
Node *head = NULL;
initList(&head); // 传的参数为二级指针因此(&head)
createList(head);
show(head);
reaverse(head); // 链表的反转
cout << "反转后";
show(head);
#if 0
findP(head); // 按元素位置查找元素
//findE(head); // 按元素查找元素
show(head);
insertP(head);
//insertE(head);
show(head);
deleteP(head); // 删除节点 ,按位置
//deleteE(head); // 删除节点 ,在某个元素之后
show(head);
//show(head);
#endif
getchar();
return 0;
}
写了下单链表的基本操作,带头节点的,还有点问题,但是不想改了 ,还需要学习别的.
所有代码
注:只是学习记录,仅供参考,如有雷同,不胜荣幸
2016.4.27