题目描述
本题只需要提交填写部分的代码
已知长度为 nnn 的线性表 AAA 采用链式存储结构,请写一时间复杂度为 O(n)O(n)O(n)、空间复杂度为 O(1)O(1)O(1) 的算法,该算法删除线性表中所有值为 item
的数据元素。
O(1)O(1)O(1) 表示算法的辅助空间为常量。
代码:
#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
};
node *createlist(node *head,int n)
{
node *previous; //前驱结点
node *current; //当前结点
if(n<1)
{
return NULL; //建立空链表
}
previous=head=new node; //建立首结点
cin>>head->data; //输入数据
while(--n)
{
current=new node; //建立新结点
cin>>current->data; //输入数据
previous->next=current; //新节点挂在表尾
previous=current;
}
previous->next=NULL; //置表尾结束标志
return head; //返回首结点指针
}
node *listdel(node *head,int item)
{
node *temp;
/* 从头找到第一个