链表相关操作

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

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;

}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值