[Leetcode]单链表回顾
一.链表结构
二 .初始化链表
三.建立链表
3.1头插法建立单链表(输入的顺序为倒序)
void createListInsertionMethod(LinkList& head)
{
node* s;
int flag = 1;
char ch;
while(flag)
{
c = getchar();
if(c != '$')
{
s = new node;
s->ch = c;
s->next = head->next;
head->next = s;
}
else
{
flag = 0;
}
}
}
3.2尾插法建立单链表(输入的顺序为顺序)
//尾插法建立单链表
void creatListTailInsertionMethod(LinkList& head)
{
char ch;
node* p,*q;
int flag = 1;
p = head;
q = NULL;
while (flag)
{
ch = getchar();
if (ch != '$')
{
q = new node;
p->next = q;
q->ch = ch;
p = q;
q->next = NULL;
}
else
{
flag = 0;
}
}
}
四.求链表的长度
//求出链表的长度
int sizeofList(LinkList head)
{
if (head != NULL)
{
int size = 1;
node* p = head->next;
while (p != NULL)
{
size++;
p = p->next;
}
return size - 1;
}
return 0;
}
五.输出链表的内容
//输出链表的内容
void outputListContent(LinkList head)
{
cout << "The output of List is: ";
node* p = head->next; // 跳过头节点,从第一个实际节点开始
if (p == NULL)
{
cout << "The list is empty." << endl;
return;
}
while (p != NULL)
{
cout << p->ch << " ";
p = p->next;
}
cout << endl; // 在输出结束后换行
}
六.查找链表中的第i个内容
//查找单链表中的第i个节点
void searchByIndex(LinkList head, int index)
{
if (head != NULL)
{
int size = sizeofList(head);
if (index > size)
{
cout << "输入的index不合法。" << endl;
return;
}
node* p = new node;
p = head->next;
for (int i = 1;i < index;i++)
{
p = p->next;
}
cout << "The content of " << index << " is :" << p->ch << endl;
}
else
{
cout << "链表为NULL" << endl;
return;
}
}
七.按照值查找
//按照值查找
void searchByContent(const LinkList head, char ch,node*& aim)
{
if (head != NULL)
{
node* p = head->next;
while (p->ch != ch)
{
if (p == NULL)
{
cout << "找不到ch" << endl;
return;
}
p = p->next;
}
aim = p;
}
else
{
cout << "链表为NULL" << endl;
return;
}
}
八.单链表插入操作
//单链表插入操作
void insert(LinkList& head, int index, char ch)
{
if (head != NULL)
{
int len = sizeofList(head);
if (index > len)
{
cout << "插入操作的index输入不合法" << endl;
return;
}
else
{
int i = 1;
node* p = head->next;
while (i != index - 1)
{
p = p->next;
i++;
}
node* s = new node;
s->next = p->next;
p->next = s;
s->ch = ch;
}
}
}
九.单链表删除操作
//单链表删除操作
bool delList(LinkList& head,int index)
{
if (head != NULL)
{
int len = sizeofList(head);
if (len < index)
{
cout << "删除操作的Index输入不合法" << endl;
return false;
}
int i = 1;
node* p = head->next;
while (i != index - 1)
{
p = p->next;
i++;
}
node* q = p->next;
p->next = q->next;
return true;
}
else
{
cout << "删除操作的输入链表为NULL" << endl;
return false;
}
}
十.Coding
#include<iostream>
using namespace std;
typedef struct node
{
char ch;
node* next;
}*LinkList,Node;
//初始化单链表
void initList(LinkList& head)
{
head = new node;
head->next = NULL;
}
/*头插法建立单链表
void createListInsertionMethod(LinkList& head)
{
node* s;
int flag = 1;
char ch;
while(flag)
{
c = getchar();
if(c != '$')
{
s = new node;
s->ch = ch;
s->next = head->next;
head->next = s;
}
else
{
flag = 0;
}
}
}
*/
//尾插法建立单链表
void creatListTailInsertionMethod(LinkList& head)
{
char ch;
node* p,*q;
int flag = 1;
p = head;
q = NULL;
while (flag)
{
ch = getchar();
if (ch != '$')
{
q = new node;
p->next = q;
q->ch = ch;
p = q;
q->next = NULL;
}
else
{
flag = 0;
}
}
}
//输出链表的内容
void outputListContent(LinkList head)
{
cout << "The output of List is: ";
node* p = head->next; // 跳过头节点,从第一个实际节点开始
if (p == NULL)
{
cout << "The list is empty." << endl;
return;
}
while (p != NULL)
{
cout << p->ch << " ";
p = p->next;
}
cout << endl; // 在输出结束后换行
}
//求出链表的长度
int sizeofList(LinkList head)
{
if (head != NULL)
{
int size = 1;
node* p = head->next;
while (p != NULL)
{
size++;
p = p->next;
}
return size - 1;
}
return 0;
}
//查找单链表中的第i个节点
void searchByIndex(LinkList head, int index)
{
if (head != NULL)
{
int size = sizeofList(head);
if (index > size)
{
cout << "输入的index不合法。" << endl;
return;
}
node* p = new node;
p = head->next;
for (int i = 1;i < index;i++)
{
p = p->next;
}
cout << "The content of " << index << " is :" << p->ch << endl;
}
else
{
cout << "链表为NULL" << endl;
return;
}
}
//按照值查找
void searchByContent(const LinkList head, char ch,node*& aim)
{
if (head != NULL)
{
node* p = head->next;
while (p->ch != ch)
{
if (p == NULL)
{
cout << "找不到ch" << endl;
return;
}
p = p->next;
}
aim = p;
}
else
{
cout << "链表为NULL" << endl;
return;
}
}
//单链表插入操作
void insert(LinkList& head, int index, char ch)
{
if (head != NULL)
{
int len = sizeofList(head);
if (index > len)
{
cout << "插入操作的index输入不合法" << endl;
return;
}
else
{
int i = 1;
node* p = head->next;
while (i != index - 1)
{
p = p->next;
i++;
}
node* s = new node;
s->next = p->next;
p->next = s;
s->ch = ch;
}
}
}
//单链表删除操作
bool delList(LinkList& head,int index)
{
if (head != NULL)
{
int len = sizeofList(head);
if (len < index)
{
cout << "删除操作的Index输入不合法" << endl;
return false;
}
int i = 1;
node* p = head->next;
while (i != index - 1)
{
p = p->next;
i++;
}
node* q = p->next;
p->next = q->next;
return true;
}
else
{
cout << "删除操作的输入链表为NULL" << endl;
return false;
}
}
int main()
{
//input:
//dsaf$
LinkList head;
initList(head);
creatListTailInsertionMethod(head);
outputListContent(head);
cout << "The size of List is : " << sizeofList(head) << endl;
int index = 3;
cout << "链表第" << index << "个的内容是 : " << endl;
searchByIndex(head, index);
node* aim = NULL;
searchByContent(head, 'a', aim);
cout << "The aimed content of node is : " << aim->ch << endl;
insert(head, 3, '%');
outputListContent(head);
cout << "The result of delete function is : " << delList(head, 3) << endl;
outputListContent(head);
return 0;
}