前些时候,看到辛昕版主发的链表实例,明白了很多啊,自己也写了一个,也不知道算什么东西。
看到qq有许多分组,每个分组又有很多好友,不知道怎么实现的。
自己也构建了一个类似的框架
不多说上代码
#include
#include
#include
typedef struct node{
int data;
struct node *next;
}listnode;
typedef struct listhead{
struct listhead *next;
struct node *head;
char groupname[20];
}mlisthead;
struct listhead* creatlisthead()
{
struct listhead *head = (struct listhead*)malloc(sizeof(struct listhead));
if (head == NULL)
{
printf("head error\n");
exit(0);
}
head->next = NULL;
head->head = NULL;
memset(head->groupname,0,20);
return head;
}
void insertlisthead(struct listhead *head,char *groupname)
{
struct listhead *pthis;
pthis = (struct listhead*)malloc(sizeof(struct listhead));
pthis->head = NULL;
strcpy_s(pthis->groupname,20, groupname);
pthis->next = head->next;
head->next = pthis;
}
void deletelisthead(struct listhead *head, char *groupname)
{
struct listhead *ppre, *pthis;
listnode *pnext,*ppnext;
pthis = head->next;
ppre = head;
while (pthis != NULL)
{
if (strcmp(pthis->groupname, groupname) != 0)
{
ppre = pthis;
pthis = pthis->next;
}
else
{
pnext = pthis->head;
pnext = pnext->next;
while (pnext != NULL)
{
ppnext = pnext->next;
pnext->next = NULL;
free(pnext);
pnext = ppnext;
}
ppre->next = pthis->next;
free(pthis);
}
}
}
struct node* creatlist()
{
struct node *head=(struct node*)malloc(sizeof(struct node));
if(head == NULL)
{
printf("head error\n");
exit(0);
}
head->next=NULL;
return head;
}
void insertlist(struct node *head,int data)
{
struct node *pthis;
if (head->next == NULL)
{
pthis = head;
pthis->data = data;
pthis->next = head;
}
else
{
pthis = (struct node*)malloc(sizeof(struct node));
pthis->data = data;
pthis->next = head->next;
head->next = pthis;
}
}
void deletelist(struct node *head,int data)
{
struct node *ppre,*pthis;
ppre=head;
pthis=head->next;
while(pthis != NULL)
{
if(pthis->data != data)
{
pthis=pthis->next;
ppre=ppre->next;
}
else
{
ppre->next=pthis->next;
pthis->next=NULL;
break;
}
}
free(pthis);
}
mlisthead * creatgroup()
{
mlisthead *head;
head = creatlisthead();
return head;
}
void addagroup(mlisthead *head,char *group)
{
insertlisthead(head, group);
}
mlisthead *searchlisthead(mlisthead *head, char *group)
{
mlisthead *pthis;
pthis = head->next;
while (pthis != NULL)
{
if (strcmp(pthis->groupname, group)!=0)
{
pthis = pthis->next;
}
else
{
return pthis;
}
}
return NULL;
}
void deleteagroup(mlisthead *head, char *group)
{
deletelisthead(head, group);
}
void addfriend(mlisthead *head, int id)
{
listnode *fhead;
if (head->head == NULL)
{
head->head = creatlist();
}
insertlist(head->head, id);
}
void deletefriend(mlisthead *head, int id)
{
listnode *fhead;
fhead = head->head;
deletelist(fhead, id);
}
int main()
{
mlisthead *head,*pthis;
int i;
head = creatgroup();
addagroup(head, "daxue");
addagroup(head, "gaozhong");
addagroup(head, "xiaoxue");
pthis = searchlisthead(head, "daxue");
addfriend(pthis, 1);
addfriend(pthis, 2);
pthis = searchlisthead(head, "gaozhong");
addfriend(pthis, 1);
addfriend(pthis, 2);
pthis = searchlisthead(head, "xiaoxue");
addfriend(pthis, 1);
addfriend(pthis, 2);
}
先收藏了 以前自己也写过类似的 看看这个是不是更先进