#include<iostream>
#include<ctime>
using namespace std;

struct list
{
int number;
list *next;
};

int FindLinkList(list *t, int i)
{
int j = 0;
list *p = t;
while(p&&j < i)
{
p = p->next;
j++;
}
if (p)
return p->number;
return -1;
}

void AddLinkList(list *t, int i, int num)
{
int j = 0;
list *p = t;
while (p&&j < i)
{
p = p->next;
j++;
}
if (p)
{
list *s = new list;
s->next = p->next;
p->next = s;
s->number = num;
}
}

void deleteLinkList(list *t, int i)
{
int j = 0;
list *p = t;
while (p&&j<i-1)
{
p = p->next;
j++;
}
if (p&&p->next)
{
list *q = p->next;
if (q->next)
p->next = q->next;
else p->next = NULL;
delete q;
}
}
//头插法
void createLinkList(list *l, int n)
{
srand(time(0));
list *p;
for (int i = 0; i < n; i++)
{
p = new list;
p->number = rand() % 100 + 1;
//cout << p->number << endl;
p->next = l->next;
l->next = p;
}
}

//尾插法
void createlinklist(list *l, int i)
{
srand(time(0));
list *r, *p;
p = l;
for (int j = 1; j <= i; j++)
{
r = new list;
r->number = rand() % 100 + 1;
r->next = p->next;
p->next = r;
p = r;
}
}

void clearlinklist(list *t)
{
list *p, *q;
p = t;
while (p->next)
{
q = p->next;
delete p;
p = q;
}
t->next = NULL;
}


void main()
{

list *linklist=new list;
linklist->next = NULL;
linklist->number = 0;
/*createLinkList(linklist, 10);*/
createlinklist(linklist, 10);
for (int i = 1; i <= 10; i++)
cout << FindLinkList(linklist, i)<<endl;
cout << endl;
deleteLinkList(linklist, 1);
clearlinklist(linklist);
for (int i = 1; i <= 9; i++)
cout << FindLinkList(linklist, i) << endl;


}

posted on 2017-02-23 20:25  郑哲  阅读(108)  评论(0编辑  收藏  举报