单链表按照元素删除和查找按照位置插入和查找
#include using namespace std; typedef int ElemType; typedef int Status; #define OK 1
#define ERROR 0
typedef struct Lnode {
ElemType data;
struct Lnode *next; }Lnode,*LinkList;
void Creat_List(LinkList L)//创建单链表并输入元素 {
LinkList p;
ElemType ch;
cout<<"请输入链表元素,并且以输入-1表示结束!"<>ch&&ch!=-1)
{
p=(LinkList)malloc(sizeof(Lnode));
if(!p)
{
cout<<"获取内存失败"<data=ch;//尾插法
L->next=p;
L=p;
}
L->next=NULL;
}
Status output_List(LinkList L)//遍历单链表(输出单链表元素)
{
LinkList p;
p=L->next;
if(p==NULL)
{
cout<<"该链表是空链表!"<data<<" ";
p=p->next;
}
cout<next==NULL)
{
cout<<"该链表为空"<next!=NULL)
{
if(L->next->data==k)
{
q=L->next;
L->next=q->next;
free(q);
continue;//如果只需删除一个那么就把 continue 换成return OK; 即输入的
元素有相同时是不是把多个都删除
}
L=L->next;
}
return OK;
}
void List_Inster(LinkList L,int i,ElemType e)//在指定位置插入元素
{
LinkList s;
int j=0;
while(L!=NULL&&jnext;
j++;
}
if(!L||j>i-1)
{
cout<<"插入位置非法!"<data=e;
s->next=L->next;
L->next=s;
}
Status seek(LinkList L,ElemType e1,ElemType e2,int i)
{
int k=0;
bool temp1=true,temp2=true,temp3=true;
LinkList p;
p=L->next;
if(p==NULL)
{
cout<<"该链表是空链表!"<data==e1)
{
temp1=false;
cout<<"元素"<data==e2)
{
temp2=false;
cout<<"元素"<data<next;
}
if(temp1)
cout<<"元素"<next=NULL;
Creat_List(H);
cout<<"单链表中的元素为:"<>e1>>e2>>n;
seek(H,e1,e2,n);
cout<<"请输入插入的位置和元素!"<>n>>m;
List_Inster(H,n,m);
cout<<"插入后的链表元素为:"<>e;
delete_List(H,e);
cout<<"删除元素后的链表为:"<