//链表初始化函数
node *List_Init(int n)
{
node *head = NULL, *pr;
while(n--)
{
node *p = new node;
int data_;
cin>>data_;
p->next = NULL;
p->data = data_;
if(head == NULL)
head = p;
else
pr->next = p;
pr = p;
}
return head;
}
//链表打印函数
void List_Print(node *head)
{
while(head->next != NULL)
{
cout<<head->data<<" ";
head = head->next;
}
cout<<head->data;
cout<<endl;
return;
}
//销毁线性表
void List_Destory(node *head)
{
head = NULL;
return;
}
//清空线性表
void List_clear(node *head)
{
while(head->next != NULL)
{
head->data = 0;
head = head->next;
}
head->data = 0;
return;
}
//判断线性表是否为空
bool If_List_Empty(node *head)
{
if(head = NULL)
return true;
else
return false;
}
//判断线性表长度
void List_Size(node *head)
{
int n = 0;
while(head->next != NULL)
n++;
n++;
cout<<n;
}
//提取线性表特定节点的数据
int *List_Retrival(node* head, int n)
{
n -= 1;
while(n--)
head = head->next;
int *p_data = &(head->data);
return p_data;
}
//返回线性表指定数据元素的位置
int List_Locate(node *head, int num)
{
int n = 1;
while(head->data != num)
{
n++;
head = head->next;
}
return n;
}
//返回指定位置的直接前驱数据
int List_Prior(node *head, int n)
{
n -= 2;
while(n--)
head = head->next;
return head->data;
}
//在线性表指定位置插入元素
node *List_Insert(node *head, int n)
{
node *pr = head;
n -= 2;
while(n--)
head = head->next;
node *p = new node;
int data_;
cout<<"请输入节点数据:";
cin>>data_;
p->data = data_;
p->next = head->next;
head->next = p;
return pr;
}
//删除线性表指定位置的元素
node *List_Delete(node *head, int n)
{
n -= 2;
node *pr = head;
while(n--)
head = head->next;
head->next = head->next->next;
return pr;
}
//测试函数
#include <iostream>
using namespace std;
struct node
{
int data;
node *next = NULL;
};
int main()
{
int n;
cout<<"请输入节点数:";
cin>>n;
cout<<"测试链表初始化函数:";
node *head = List_Init(n);
cout<<"测试链表打印函数:";
List_Print(head);
cout<<"返回直接前驱函数测试:";
int m;
cin>>m;
cout<<"其直接前驱为:"<<List_Prior(head, m)<<endl;
cout<<"测试在指定位置插入元素函数:";
int o;
cin>>o;
List_Print(List_Insert(head, o));
cout<<"测试在指定位置删除元素函数:";
int p;
cin>>p;
List_Print(List_Delete(head, p));
return 0;
}
谢谢观看,错误之处恳求各位指正。