链表相关操作

#include

#include

#include

 

typedef struct MyStruct

{

char Lience[10];

char Name[20];

int Money;

int Vip;

struct MyStruct *next;

} Node;

 

typedef struct head

{

int count;

Node *next;

} Link;

 

Link *LinkInit(Link *head)

{

head = (Link *)malloc(sizeof(Link));

 

if (!head)

{

printf("LinkInit : head malloc error!\n");

exit(0);

}

 

head->count = 0;

head->next = NULL;

return head;

}

 

Link *LinkInsert(Link *head)

{

if (!head)

{

printf("LinkInsert : head == NULL\n");

exit(0);

}

 

int i = 0;

scanf("%d", &i);

Node *p = head->next;

while (i)

{

Node *q = (Node *)malloc(sizeof(Node));

if (!q)

{

printf("LinkInsert: q malloc error!\n");

exit(0);

}

 

printf("Please input Name : ");

scanf("%s", q->Name);

printf("Please input Lience : ");

scanf("%s", q->Lience);

q->Money = rand() % 100;

q->Vip = q->Money / 10;

if (head->next == 0)

{

q->next = p;

head->next = q;

p = q;

}

else

{

q->next = p->next;

p->next = q;

}

 

head->count++;

i--;

}

return head;

}

 

Link *LinkDelete(Link *head, char *lience)

{

if (!head)

{

printf("LinkDelete : head is NULL\n");

exit(0);

}

Node *p = head->next;

Node *q = NULL;

while (p != NULL && strcmp(p->Lience, lience) != 0)

{

q = p;

p = p->next;

}

 

if (p == NULL)

{

printf("User do not exsist!\n");

}

else if (strcmp(p->Lience, lience) == 0)

{

q->next = p->next;

free(p);

}

return head;

}

 

void Display(Link *head)

{

if (!head)

{

printf("Display : head is NULL!\n");

exit(0);

}

 

printf("There are %d users in total.\n", head->count);

Node *p = head->next;

printf("head Name : %p\n", p);

while (p != NULL)

{

printf("---------user %s-----------\n", p->Name);

printf("User Name : %s\n", p->Name);

printf("Car Lience : %s\n", p->Lience);

printf("Money : %d\n", p->Money);

printf("Vip Level : %d\n", p->Vip);

printf("---------user %s-----------\n", p->Name);

 

p = p->next;

}

}

 

void LinkSearch(Link *head, char *lience)

{

if (!head)

{

printf("LinkSearch : head is NULL!\n");

exit(0);

}

 

Node *p = head->next;

while (p != NULL && strcmp(p->Lience, lience))

{

p = p->next;

}

if (p == NULL)

{

printf("The Lience you looking for is not exsist!\n");

}

else if (strcmp(p->Lience, lience))

{

printf("---------user %s-----------\n", p->Name);

printf("User Name : %s\n", p->Name);

printf("Car Lience : %s\n", p->Lience);

printf("Money : %d\n", p->Money);

printf("Vip Level : %d\n", p->Vip);

printf("---------user %s-----------\n", p->Name);

}

}

 

int main(int argc, char const *argv[])

{

Link *head = NULL;

head = LinkInit(head);

head = LinkInsert(head);

Display(head);

return 0;

}

你可能感兴趣的:(C语言学习)