#include<iostream>
using namespace std;
struct Node
{
int date;
struct Node* pNext;
};
class List
{
public:
int len;
int i;
int val;
struct Node* creat_list();
bool is_empty(struct Node* pHead);
int length_list(struct Node*);
bool insert_list(struct Node*,int,int);
bool delete_list(struct Node*, int, int *);
void scort_list(struct Node*);
private:
Node* pHead;
};
struct Node* create_list()
{
if (NULL == pHead)
{
cout << "分配失败" << endl;
}
cout << "请输入需要生成链表结点的个数:len= ";
for (int i = 0; i < List::len; ++i)
{
cout << "请输入第几个结点的值:";
i += 1;
}
};
bool is_empty(struct Node* pHead)
{
if (NULL == pHead->pNext)
return true;
else
return false;
};
int length_list(struct Node*pHead)
{
struct Node* p = pHead->pNext;
int len = 0;
while (p)
{
++len;
p = p->pNext;
}
return len;
};
void soert_list(struct Node*)
{
int i, j, t;
int len = length_list(pHead);
Node* pNext = p, q;
for ( i = 0,p=pHead->pNext; i < List::len - 1; ++i,p=p->pNext)
{
for (j = i + 1,q=p->pNext; j < len; ++j,q=q->pNext)
{
if (p->date > q->date)
{
t = p->date;
p->date = q->date;
q->date = t;
}
}
}
return;
};
bool insert_list(struct Node*, int pos, int val)
{
int i = 0;
struct Node* p = List::pHead;
while (NULL != p && i < pos - 1)
{
p = p->pNext;
++i;
}
if (i > pos - 1 || NULL == p->pNext)
return false;
struct Node* q = p->pNext;
val = q->date;
};
bool delete_list(struct Node*, int, int*)
{
int i = 0;
struct Node* p = List::pHead;
while (NULL != p && i < pos - 1)
{
p = p->pNext;
++i;
}
if (i > pos - 1 || NULL == p->pNext)
return false;
struct Node* q = p->pNext;
*pVal = q->date;
p->pNext = p->pNext->pNext;
free(q);
q = NULL;
};
int main()
{
struct Node* pHead = NULL;
pHead = creak_list();
int len = length_list(pHead);
cout << "链表的长度为" << len << endl;
return 0;
}
|