1 #include<iostream> 2 #include<ctime> 3 using namespace std; 4 5 struct list 6 { 7 int number; 8 list *prior; 9 list *next; 10 }; 11 12 13 int FindLinkList(list *t, int i) 14 { 15 if (!t) 16 return -1; 17 int j = 0; 18 list *p = t; 19 for (; j < i; j++) 20 { 21 p = p->next; 22 if (p == t) 23 return -1; 24 } 25 return p->number; 26 } 27 28 29 void addlinklist(list *t, int i) 30 { 31 int j = 0; 32 list *p = t; 33 list *s; 34 while (p&&j < i) 35 { 36 p = p->next; 37 j++; 38 } 39 if (p) 40 { 41 s = new list; 42 s->number = i; 43 s->prior = p; 44 s->next = p->next; 45 p->next = s; 46 if (p->next) 47 { 48 p->next->prior = s; 49 } 50 } 51 } 52 53 54 55 int deletelinklist(list *t, int i) 56 { 57 list *p=t; 58 int j = 0; 59 for (; j < i; j++) 60 { 61 p = p->next; 62 if (p == t) 63 return -1; 64 } 65 p->prior->next = p->next; 66 p->next->prior = p->prior; 67 j = p->number; 68 delete p; 69 return j; 70 } 71 72 void createlinklist(list *t, int i) 73 { 74 srand(time(0)); 75 list *p; 76 for (int j = 1; j <= i; j++) 77 { 78 p = new list; 79 p->number = rand() % 100 + 1; 80 //cout << p->number << endl; 81 p->prior = t; 82 p->next = t->next; 83 t->next->prior = p; 84 t->next = p; 85 } 86 87 } 88 89 90 void main() 91 { 92 list *linklist=new list; 93 linklist->next = linklist; 94 linklist->prior = linklist; 95 linklist->number = 10; 96 createlinklist(linklist, 10); 97 for (int i = 1; i <= 10; i++) 98 cout << FindLinkList(linklist, i) << endl; 99 deletelinklist(linklist, 2); 100 cout << endl; 101 for (int i = 1; i <= 9; i++) 102 cout << FindLinkList(linklist, i) << endl; 103 }