昨天忘记写了,今天写吧,今天主要写了数据结构的作业--单链表基本操作,源代码如下:
include
define MAX_MN 100000
using namespace std;
typedef int ElementType;
typedef struct Node* PtrToNode;
struct Node {
ElementType Data;
PtrToNode Next;
};
typedef PtrToNode List;
int Read(List &L)
{
int number;
cin >> number;
if (number > MAX_MN)
{
cout << "ERROR" << endl;
return 0;
}
L = new Node;
L->Next = NULL;
List r = L;
for (int i = 0; i < number; i++)
{
List p = new Node;
cin >> p->Data;
p->Next = NULL;
r->Next = p;
r = p;
}
return 1;
}
void Print(List L)
{
List p = L->Next;
while (p)
{
cout << p->Data<<" ";
p = p->Next;
}
cout << endl;
}
void Delete(List &L,int k)
{
if (k < 1)
{
return;
}
List p = L;
int count = 0;
while ((p->Next)&&(count<k-1))
{
p = p->Next;
++count;
}
List q = p->Next;
p->Next = q->Next;
delete q;
}
void Add(List& L, int k, ElementType d)
{
if (k > sizeof(L))
return;
List p = L;
int count = 0;
while ((p->Next) && (count < k))
{
p = p->Next;
++count;
}
List q = new Node;
q->Data = d;
q->Next = p->Next;
p->Next = q;
}
int main()
{
List L;
int operateNum;
if (Read(L))
{
cin >> operateNum;
if (operateNum > MAX_MN)
{
cout << "ERROR" << endl;
}
else
{
for (int i = 0; i < operateNum; i++)
{
int m, k, d;
cin >> m;
switch (m)
{
case 0:
cin >> k;
cin >> d;
Add(L, k, d);
break;
case 1:
cin >> k;
Delete(L, k);
break;
default:
break;
}
}
Print(L);
}
}
return 0;
}
但是不知道为啥还有一个功能没实现,找不到了,算了