周三,19号的上机题。
题目,如图:
不多废话,直接源代码:
#include<iostream>
#include<stdlib.h>
int a[100];
using namespace std;
class node {
public:
node * next;
int data;
node() { next = NULL; data = 0; }
};
class lnode{
node* head;
public:
lnode() { head = new node; }//1
~lnode() {}
void create();//2
void create2(int n,int a[]);
void Print();//3
int del();//4
void delAll();//5
void rangedelte();//6
void f1();//7
void f2();//8
};
void lnode::create2(int n,int a[])
{
node *p;
for (int i = 0; i < n; i++)
{
p = new node;
p->data=a[i];
p->next = head->next;
head->next = p;
}
}
void lnode::f2()// 上机题第二个;
{
cout << "删除所有重复的数字:"<<endl;
bool hashTable[1000] = { false };
node *q;
node *p = head->next;
while (p)
{
if (hashTable[p->data] == false)
{
hashTable[p->data] = true;
q = p;
p = p->next;
}
else
{
q->next = p->next;
delete(p);
p = q->next;
}
}
}
void lnode::f1()//上机题第一个;
{
cout << "分裂开奇偶数";
int i = 0;
node *q = head;
node *p = head->next;
while (p)
{
if ((p->data) % 2 != 0)
{
a[i] = p->data;
i++;
q->next = p->next;
delete(p);
p = q->next;
}
else
{
q = p;
p = p->next;
}
}
lnode B;
B.create2(i, a);
cout << "偶数:"<<endl;
Print();
cout << "奇数:"<<endl;
B.Print();
}
void lnode::rangedelte()
{
int min, max;
cout << "在按值递减的链表中进行区域删除(删除的值不包含min和max本身),依次输入min与max的值:";
cin >> min >> max;
node *pr = head, *p = head->next;
while (p&&p->data <= min)
{
pr = p;
p = p->next;
}
while (p&&p->data < max)
{
pr->next = p->next;
delete(p);
p = pr->next;
}
}
void lnode::delAll()
{
int n;
cout << "查找是否存在该数,并删除链表中所有的该数:";
cin >> n;
node* q = head;
node* p = q->next;
while (p)
{
if (p->data != n)
{
q = p;
p = p->next;
}
else
{
q->next = p->next;
delete(p);
p = q->next;
}
}
}
void lnode::create()
{
node *p;
int n;
cout << "输入总个数:";
cin >> n;
cout << "输入值,请保持递减:";
for (int i = 0; i < n; i++)
{
p = new node;
cin >> p->data;
p->next = head->next;
head->next = p;
}
}
void lnode::Print()
{
cout << "打印链表:";
node *p=head->next;
while (p)
{
cout << p->data << ' ';
p = p->next;
}
cout << endl;
}
int lnode::del()
{
int n;
cout << "查找是否存在该数,并删除第一次出现的该数:";
cin >> n;
int x=0;
node* q = head;
node* p = q->next;
while (p)
{
if (p->data != n)
{
q = p;
p = p->next;
}
else
{
q->next = p->next;
delete(p);
p = q->next;
x = 1;
goto loop;
}
}
loop:;
return x;
}
int main(void)
{
lnode A;
A.create();
cout<<A.del()<<endl;
A.Print();
A.delAll();
A.Print();
A.rangedelte();
A.Print();
A.f2();
A.Print();
A.f1();
system("pause");
return 0;
}
下面是运行结果:
看完了,关注一下呗qwq