#include <bits/stdc++.h>
using namespace std;
typedef struct LNode
{
int data;
struct LNode *next;
} * LinkList, LNode;
LinkList Init(vector<int> &data)
{
LNode *head = (LNode *)malloc(sizeof(LNode));
LNode *p = head;
p->data = data[0];
p->next = NULL;
for (int i = 1; i < data.size(); ++i)
{
LNode *is = (LNode *)malloc(sizeof(LNode));
is->data = data[i];
is->next = NULL;
p->next = is;
p = p->next;
}
return head;
}
void Print(LinkList L)
{
LNode *p = L;
while (p)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int deleteX(LinkList L, int x)
{
int flag = 0;
LNode *p = L;
while (p != NULL)
{
if (p->data == x)
{
flag = 1;
p->data = p->next->data;
p->next = p->next->next;
}
else
{
p = p->next;
}
}
return flag;
}
int main()
{
vector<int> a, b;
a.resize(8);
a[0] = 1, a[1] = 3, a[2] = 5, a[3] = 7, a[4] = 7, a[5] = 11, a[6] = 12, a[7] = 15;
LinkList A = Init(a);
Print(A);
if (deleteX(A, 7))
{
cout << "success\n";
}
else
{
cout << "error\n";
}
Print(A);
return 0;
}
单链表删除指定元素(不带头结点)
最新推荐文章于 2024-04-21 12:08:44 发布