本周学习了数据结构的单链表知识,现将有关单链表的几种算法分析和代码记录于本文:
链表常用操作:
p =l; // 头节点
p=l->next ; // 首元结点
p = p->next;
q=p->next;
p = q ;
p!=NULL;
p->next!=NULL:
1、构建结点
一个结点里包括:数据;指针(还是该结点类型的指针)
struct node // 结点结构体 data|*next
{
int data;
node* next;
};
2、链表初始化(用于链表类的默认构造)
算法要求:构造一个带头结点的空链表L,如下图:
算法分析:
1、构造一个头指针;
2、分配一块堆区内存地址(new);
3、指针中的data=0;指针中的*next=NULL;
void initlink(node* &l) // 初始化列表,构造头结点
{
l = new node; //要存入一个数据时,一定要开辟一个空间
l->data = 0;
l->next = NULL;
l->length = 0;
}
3、销毁链表(用于链表类中的析构函数)
算法要求:表L彻底不存在(!!注意区分清空链表)
算法分析:
4、链表是否为空
算法分析:判断表头指针L->next是否为空即可;
5、头插法
6、尾插法
7、清空链表
8、找到链表中的某元素,并返回其索引号/指针
算法要求:找到了即返回索引号或者指针,找不到返回-1;
9、 在第i个结点插入一个新元素
算法要求:插入位置合理,正常插入;插入位置不合理,返回-1;
10、删除第i个位置上的元素
11、程序:
整理成了链表类,
#define ok 1
#define error -1
struct node // 结点结构体 data|*next
{
int data;
node* next;
};
class linkList
{
public:
void init_list(); //初始化结点
int list_Empty(); //判断链表是否为空
void push_top(int val); //头插法
void push_back(int val); //尾插法
void destorylink(); //销毁链表
void clearlist(); //清空链表
int get_length(); //获取表长
int locate_val(int val); //找到val的位置
node* locate_add(int val); //找到val所在结点
int insert_val(int val, int i); //在第i个位置插入val
int delete_val(int i); // 删除位置i上的元素
linkList() //默认构造
{
init_list();
}
~linkList() //析构函数
{
destorylink();
}
node* head; //头结点
int length; //表长
};
void linkList::init_list() //构造头结点
{
head = new node; //分配一块内存空间
head->data = 0;
head->next = NULL;
this->length = 0;
}
int linkList::list_Empty()
{
if (this->head->next == NULL)
{
return 1;
}
else
{
return -1;
}
}
void linkList::push_top(int val)
{
node* p;
p = new node; //插入新的值,需要开辟新的空间
p->data = val;
p->next = head->next;
head->next = p;
this->length++;
}
void linkList::push_back(int val)
{
node* p;
p = head; // 找首元更合适还是头结点更合适
while (p->next != NULL)
{
p = p->next;
}//找到了当前最后一个结点
node* new_item =new node;
new_item->data = val;
new_item->next = NULL;
p->next = new_item;
this->length++;
}
void linkList::destorylink() //链表将不存在
{
node* p;
while (head==NULL)
{
p = head;
head = head->next;
delete p;
}
}
void linkList::clearlist()
{
node* p;
node* q;
p = head->next;
while (p != NULL)
{
q = p->next;
delete p;
//node* p; //不能再构造node*p了
p = q;
}
head->next = NULL;
this->length = 0;
}
int linkList::get_length()
{
return this->length;
}
int linkList::locate_val(int val)
{
node* p;
p = head->next; //首元结点
int j = 1;
while (p != NULL && p->data!=val ) //顺序不能写反
{
p = p->next;
j++;
}
if (p == NULL)
{
return error;
}
return j;
}
node* linkList::locate_add(int val)
{
node* p;
p = head->next; //首元结点
int j = 1;
while (p != NULL && p->data != val)
{
p = p->next;
j++;
}
//找到了当前结点
if (p == NULL)
{
return NULL;
}
return p;
}
int linkList::insert_val(int val, int i)
{
node* p;
p = head;
int j = 0;
while (j < i - 1 && p!=NULL)
{
p = p->next;
j++;
}
if (p == NULL || j > i - 1)
{
return error;
}
node* new_item = new node;
new_item->data = val;
new_item->next = p->next;
p->next = new_item;
this->length++;
return ok;
}
int linkList::delete_val(int i)
{
node* p;
p = head;
int j = 0;
while (j < i - 1 && p->next!=NULL)
{
p = p->next;
j++;
}
if (j > i - 1 || p->next == NULL)
{
return error;
}
node* q;
q = p->next; // 要被删除的结点
p->next = p->next->next;
delete q;
this->length--;
return ok;
}