实现了单链表的以下功能
头部插入节点尾部插入节点
n-th位置插入节点
n-th位置删除节点
清空
获取长度
查找某个值
判断是否为空
#include <iostream>
using namespace std;
struct LinkedListNode
{
int val;
LinkedListNode *next;
};
void AddAtHead(LinkedListNode *&head, int data)//head can be NULL
{
LinkedListNode *node = new LinkedListNode;
node->val = data;
node->next = head;
head = node;
}
//传进来的head不是引用的指针或指针的指针
//所以该函数内出现head = .. 是不能改变实参的head的(head = new..也不行)
//而head->next = .. 是可以的
void AddAtTail(LinkedListNode *head, int data)//head can not be NULL unless using *&head or **head
{
if(head == NULL) return;
LinkedListNode *node = new LinkedListNode;
node->val = data;
node->next = NULL;
while (head->next != NULL)
{
head = head->next;
}
head->next = node;
//can not be like this below
//using **head or *&head in the parameter list can solve the problem
/*while(head!=NULL)
head = head->next;
head = node;*/
}
void BuildLinkedList(LinkedListNode *&head, int a[], int n)
{
AddAtHead(head,a[0]);
for (int i = 1; i < n; i++)
{
AddAtTail(head,a[i]);
}
}
void Traverse(LinkedListNode *head)
{
while(head != NULL)
{
cout << head->val << " ";
head = head->next;
}
cout << "\n";
}
int GetSize(LinkedListNode *head)
{
int size = 0;
while(head != NULL)
{
head = head->next;
size++;
}
return size;
}
bool IsEmpty(LinkedListNode *head)
{
if(head == NULL)
return true;
return false;
}
//递归
void MakeEmpty(LinkedListNode *&head)
{
if (head!=NULL)
{
MakeEmpty(head->next);
delete head;
}
head = NULL;
}
//非递归
void MakeEmpty1(LinkedListNode *&head)
{
while(head != NULL)
{
LinkedListNode *p = head;
head = head->next;
delete p;
p = NULL;
}
}
bool Find(LinkedListNode *head, int data)
{
while(head!=NULL)
{
if(head->val == data) return true;
head = head->next;
}
return false;//including head is NULL
}
//insert参数pos的范围,[1,元素个数+1],如,xyz,pos为4时仍可以插入
//要点:移动指针到待插入元素的前一个元素,如,xyz,pos为2时,移动到x
//p最多移动到链表的最后一个元素(不为空),如果p移动到了最后一个元素的下一个元素(空),则pos不合法
void Insert(LinkedListNode *&head, int pos, int data)// pos >= 1
{
if(pos < 1) return;
LinkedListNode *p = head;
LinkedListNode *node = new LinkedListNode;
node->val = data;
node->next = NULL;
if(pos == 1)//链表为空时也可以成功插入
{
node->next = head;// can not be node->next = p;
head = node;//can not be p = node;
return;
}
int cnt = 0;
while(cnt < pos-2 && p!=NULL)//(条件与remove不同)移动到待插入点的前一个点,如xyz,pos为2时,移动到x
{
p = p->next;
cnt++;
}
if(p == NULL)//与remove不同
return;
node->next = p->next;
p->next = node;
}
//remove参数pos的范围[1,元素个数]
//要点:移动指针到待删除元素的上一个元素,如xyz,pos为2时,移动到x
//p最多移动到倒数第二个元素(不为空),如果移动到了最后一个元素(p->next为空),则pos不合法
void Remove(LinkedListNode *&head, int pos) //pos >= 1
{
if(pos < 1 || head == NULL) return;
LinkedListNode *p = head;
if(pos == 1)
{
head = p->next;//can not be p = p->next;
delete p;
return;
}
int cnt = 0;
while(cnt < pos-2 && p->next!=NULL)//(条件与insert不同)移动到待删除点的前一个点,如xyz,pos为2时,移动到x
{
p = p->next;
cnt++;
}
if(p->next == NULL)//与insert不同
return;
LinkedListNode *tmp = p->next;
p->next = p->next->next;
delete tmp;
}
int main(void)
{
int a[] = {4,2,6,1,3,5,7};
LinkedListNode *head = NULL;
//Insert(head,1,11);
AddAtHead(head,0);
BuildLinkedList(head,a,sizeof(a)/sizeof(a[0]));
Insert(head,1,8);
Traverse(head);
Remove(head,1);
AddAtHead(head,9);
//AddAtTail(head,10);
Traverse(head);
MakeEmpty(head);
return 0;
}