链表搞了两个星期,说实话让我不用模板我还真写不来
创建链表
结构体
typedef struct library List;
typedef struct library Position;
struct library
{
int offical_number;
char book_name[100];
int book_num;
List *next;
};
链表创建
List *head, *p, *tail;
head = (List*)malloc(sizeof(List));
head->next = NULL;
puts(" 输入的图书序号为'-1'时,则输入结束");
p = head;
while (1)
{
tail = (List*)malloc(sizeof(List));
cin >> tail->offical_number;
getchar();
if (tail->offical_number == -1) break;
cin >> tail->book_name >> tail->book_num;
p->next = tail;
p = tail;
}
p->next = NULL;
return head;
}
链表删除
void delete1()
{
puts(" please intput position");
int pos;
cin >> pos;
Position *P = Findnum(pos);
List *L = Head;
if (P == false)
{
puts(" Delete error,please intput right position");
return;
}
if (L == NULL || pos == NULL)
{
puts(" Delete error,please intput right position");
return;
}
else
{
if (P == L)
{
L->book_num--;
if (!L->book_num)
{
L = L->next;
Head = L;
}
puts(" Delete success");
return;
}
else
{
while (L->next != NULL&&L->next != P)
L = L->next;
if (L->next != P)
{
puts(" Delete error,please intput right position");
return;
}
else
{
L->next->book_num--;
if (!L->next->book_num)
{
L->next = L->next->next;
}
puts(" Delete success");
}
}
{
puts(" please intput position");
int pos;
cin >> pos;
Position *P = Findnum(pos);
List *L = Head;
if (P == false)
{
puts(" Delete error,please intput right position");
return;
}
if (L == NULL || pos == NULL)
{
puts(" Delete error,please intput right position");
return;
}
else
{
if (P == L)
{
L->book_num--;
if (!L->book_num)
{
L = L->next;
Head = L;
}
puts(" Delete success");
return;
}
else
{
while (L->next != NULL&&L->next != P)
L = L->next;
if (L->next != P)
{
puts(" Delete error,please intput right position");
return;
}
else
{
L->next->book_num--;
if (!L->next->book_num)
{
L->next = L->next->next;
}
puts(" Delete success");
}
}
}
}
}
链表排序
void Sort_offical_num()
{
List *p, *q, *min;
for (p = Head; p != NULL; p = p->next)
{
min = p;
for (q = p->next; q != NULL; q = q->next)
{
if (q->offical_number < min->offical_number)
{
min = q;
}
{
List *p, *q, *min;
for (p = Head; p != NULL; p = p->next)
{
min = p;
for (q = p->next; q != NULL; q = q->next)
{
if (q->offical_number < min->offical_number)
{
min = q;
}
}
if (min != p)
{
swap_num(min->book_num, p->book_num);
swap_num(min->offical_number, p->offical_number);
swap_char(min->book_name, p->book_name);
}
}
puts(" 排序成功");
}
if (min != p)
{
swap_num(min->book_num, p->book_num);
swap_num(min->offical_number, p->offical_number);
swap_char(min->book_name, p->book_name);
}
}
puts(" 排序成功");
}
链表查找
Position *Findnum(int x)
{
Position *L = Head;
if (L == NULL)
return false;
else
{
while (L)
{
if (L->offical_number == x)
return L ;
L = L->next;
}
}
return false;
}
{
Position *L = Head;
if (L == NULL)
return false;
else
{
while (L)
{
if (L->offical_number == x)
return L ;
L = L->next;
}
}
return false;
}
链表插入
bool Insert(List L, ElementType X, Position P)//:将X插入在位置P指向的结点之前,返回true。如果参数P指向非法位置,则打印“Wrong Position for Insertion”,返回false;
{
List F = L;
while (L->Next) {
if (L->Next == P)break;
L = L->Next;
}if (L->Next != P) {
printf("Wrong Position for Insertion\n");
return false;
}L->Next = (List)malloc(sizeof(struct LNode));
L->Next->Data = X;
L->Next->Next = P;
return true;
}
{
List F = L;
while (L->Next) {
if (L->Next == P)break;
L = L->Next;
}if (L->Next != P) {
printf("Wrong Position for Insertion\n");
return false;
}L->Next = (List)malloc(sizeof(struct LNode));
L->Next->Data = X;
L->Next->Next = P;
return true;
}