#include"stadx.h"
#include<iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
class LinkList
{
public:
LinkList(int a[], int n);
~LinkList() ;
int Length();
Node *Locate(int x);
void Insert(int i, int x);
int Delete(int i);
int Search(int x);
void Output();
private:
Node *first;
};
LinkList::LinkList(int a[], int n)
{
int i;
first = new Node;
Node *rear = first;
rear->next = NULL;
Node *s;
for (i = 0; i < n; i++)
{
s = new Node;
s->data = a[i];
rear->next = s;
rear = s;
}
}
LinkList::~LinkList()
{}
int LinkList::Length()
{
Node* rear = first;
int i = 0;
while (rear->next != NULL)
{
i++;
rear = rear->next;
}
return i;
}Node *LinkList::Locate(int i)
{
int k = 0;
if (i <0)
{
cout << "输入错误" << endl;
return NULL;
}
Node* current = first;
while (current->next != NULL&&k < i)
{
k++;
current = current->next;
}
return current;
}
void LinkList::Insert(int i, int x)
{
if (i > 0 && i <= this->Length())
{
Node *w;
Node *current = Locate(i);
w = new Node;
w->data = x;
w->next = current->next;
current->next = w;
}
}
int LinkList::Delete(int i)
{
int x = 0;
Node *p = Locate(i - 1);
if (!p || !p->next)
throw"位置异常";
else {
Node *q = p->next;
p->next = q->next;
x = q->data;
delete q;
return x;
}
}
int LinkList::Search(int x)
{
int i = 0;
Node *current = first->next;
while (current != NULL)
{
i++;
if (current->data == x)
{
return 1;
}
current = current->next;
}
return 0;
}
void LinkList::Output()
{
Node* p = first->next;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int system()
{
system("cls");
int a;
cout << "1.显示链表长度" << endl;
cout << "2.插入结点" << endl;
cout << "3.删除所有指定值的结点" << endl;
cout << "4.查找有无指定值的结点" << endl;
cout << "5.显示链表" << endl;
cout << "6.退出" << endl;
cin >> a;
if (a <= 6 && a > 0)
return a;
else
return 0;
}
int main()
{
int i, j, n;
int a, b, c, d;
LinkList L;
Node *first;
switch (system())
{
case 1:
cout << L.Length() << endl; system("pause"); break;
case 2:
cout << "插入位置:";
cin >> a;
cout << "插入值:";
cin >> b;
L.Insert(a, b); system("pause"); break;
case 3:
cout << "删除第几个数据:";
cin >> c;
L.Delete(c); system("pause");
cout << "删除成功" << endl; break;
case 4:
cout << "查找的值:";
cin >> d;
if (L.Search(d))
cout << "在列表里第" << L.Search(d) << "个" << endl;
else
cout << "寻找失败" << endl; system("pause"); break;
case 5:
exit(0); break;
}
return 0;
}
#include<iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
class LinkList
{
public:
LinkList(int a[], int n);
~LinkList() ;
int Length();
Node *Locate(int x);
void Insert(int i, int x);
int Delete(int i);
int Search(int x);
void Output();
private:
Node *first;
};
LinkList::LinkList(int a[], int n)
{
int i;
first = new Node;
Node *rear = first;
rear->next = NULL;
Node *s;
for (i = 0; i < n; i++)
{
s = new Node;
s->data = a[i];
rear->next = s;
rear = s;
}
}
LinkList::~LinkList()
{}
int LinkList::Length()
{
Node* rear = first;
int i = 0;
while (rear->next != NULL)
{
i++;
rear = rear->next;
}
return i;
}Node *LinkList::Locate(int i)
{
int k = 0;
if (i <0)
{
cout << "输入错误" << endl;
return NULL;
}
Node* current = first;
while (current->next != NULL&&k < i)
{
k++;
current = current->next;
}
return current;
}
void LinkList::Insert(int i, int x)
{
if (i > 0 && i <= this->Length())
{
Node *w;
Node *current = Locate(i);
w = new Node;
w->data = x;
w->next = current->next;
current->next = w;
}
}
int LinkList::Delete(int i)
{
int x = 0;
Node *p = Locate(i - 1);
if (!p || !p->next)
throw"位置异常";
else {
Node *q = p->next;
p->next = q->next;
x = q->data;
delete q;
return x;
}
}
int LinkList::Search(int x)
{
int i = 0;
Node *current = first->next;
while (current != NULL)
{
i++;
if (current->data == x)
{
return 1;
}
current = current->next;
}
return 0;
}
void LinkList::Output()
{
Node* p = first->next;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int system()
{
system("cls");
int a;
cout << "1.显示链表长度" << endl;
cout << "2.插入结点" << endl;
cout << "3.删除所有指定值的结点" << endl;
cout << "4.查找有无指定值的结点" << endl;
cout << "5.显示链表" << endl;
cout << "6.退出" << endl;
cin >> a;
if (a <= 6 && a > 0)
return a;
else
return 0;
}
int main()
{
int i, j, n;
int a, b, c, d;
LinkList L;
Node *first;
switch (system())
{
case 1:
cout << L.Length() << endl; system("pause"); break;
case 2:
cout << "插入位置:";
cin >> a;
cout << "插入值:";
cin >> b;
L.Insert(a, b); system("pause"); break;
case 3:
cout << "删除第几个数据:";
cin >> c;
L.Delete(c); system("pause");
cout << "删除成功" << endl; break;
case 4:
cout << "查找的值:";
cin >> d;
if (L.Search(d))
cout << "在列表里第" << L.Search(d) << "个" << endl;
else
cout << "寻找失败" << endl; system("pause"); break;
case 5:
exit(0); break;
}
return 0;
}