怎么用C语言建立一个链表实现一个通讯录?

Python023

怎么用C语言建立一个链表实现一个通讯录?,第1张

#include&ltstdio.h&gt

#include&ltstdlib.h&gt

#include&ltstring.h&gt

#define F-1

#define T 1

struct Address

{

char name[20]

char number[12]

char address[20]

struct Address*next

}

typedef struct Address*node

int init(node*head)

int creat_tail(node head)

int insert_index(node head)

int length(node head)

int query_name(node head)

int delete_address(node head)

void print(node head)

int main()

{

node head

init(&head)

int x

do

{

printf("0:exit\n")

printf("1:creat_tail\n")

printf("2:insert_index\n")

printf("3:query_name\n")

printf("4:delete_address\n")

printf("5:print\n")

printf("please select\n")

scanf("%d",&x)

switch(x)

{

case 0:

exit(0)

case 1:

creat_tail(head)

break

case 2:

insert_index(head)

break

case 3:

query_name(head)

break

case 4:

delete_address(head)

break

case 5:

print(head)

break

default:

exit(0)

}

}

while(1)

return 0

}

int delete_address(node head)

{

char address[20]

printf("please input the address you want to delete\n")

scanf("%s",address)

while(head-&gtnext!=NULL)

{

if(strcmp(head-&gtnext-&gtaddress,address)==0)

{

node temp=head-&gtnext

head-&gtnext=head-&gtnext-&gtnext

free(temp)

}

else

{

head=head-&gtnext

}

}

return T

}

int query_name(node head)

{

char name[20]

int count=0,index=0

printf("please input the name you want\n")

scanf("%s",name)

while(head-&gtnext!=NULL)

{

if(strcmp(head-&gtnext-&gtname,name)==0)

{

count++

printf("%d.name:%s number:%s address:%s\n",index+1,head-&gtnext-&gtname,head-&gtnext-&gtnumber,head-&gtnext-&gtaddress)

}

head=head-&gtnext

index++

}

if(count==0)

{

printf("not found\n")

}

return T

}

int length(node head)

{

int count=0

while(head-&gtnext!=NULL)

{

head=head-&gtnext

count++

}

return count

}

int insert_index(node head)

{

int index

printf("please input the index you want to add\n")

scanf("%d",&index)

if(index&lt0||index&gt=length(head))

{

printf("out of range\n")

return F

}

node newnode=(node)malloc(sizeof(struct Address))

if(NULL==newnode)

{

return F

}

int i

for(i=0i&ltindexi++)

{

head=head-&gtnext

}

printf("please input the name,number,address\n")

printf("when you input 0 0 0,exit\n")

scanf("%s%s%s",newnode-&gtname,newnode-&gtnumber,newnode-&gtaddress)

if(strcmp(newnode-&gtname,"0")!=0)

{

newnode-&gtnext=head-&gtnext

head-&gtnext=newnode

}

return T

}

void print(node head)

{

int count=0

while(head-&gtnext!=NULL)

{

count++

printf("%d.name:%s number:%s address:%s\n",count,head-&gtnext-&gtname,head-&gtnext-&gtnumber,head-&gtnext-&gtaddress)

head=head-&gtnext

}

}

int creat_tail(node head)

{

do

{

node newnode=(node)malloc(sizeof(struct Address))

if(NULL==newnode)

{

return F

}

printf("please input the name,number,address\n")

printf("when you input 0 0 0,exit\n")

scanf("%s%s%s",newnode-&gtname,newnode-&gtnumber,newnode-&gtaddress)

if(strcmp(newnode-&gtname,"0")!=0)

{

newnode-&gtnext=NULL

while(head-&gtnext!=NULL)

{

head=head-&gtnext

}

head-&gtnext=newnode

}

else

{

break

}

}

while(1)

return T

}

int init(node*head)

{

node newnode=(node)malloc(sizeof(struct Address))

if(NULL==newnode)

{

return F

}

strcpy(newnode-&gtname,"0")

strcpy(newnode-&gtnumber,"0")

strcpy(newnode-&gtaddress,"0")

newnode-&gtnext=NULL

(*head)=newnode

return T

}

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <string.h>

typedef unsigned long ulong

typedef struct _list {

char name[16]

char addr[64]

ulong phone

ulong qq

struct _list* next

} *node, list

/* insert a node */

node Insert( node* head, node pos, list* l )

{

node tmp

tmp = ( node )malloc( sizeof( list ) )

strcpy( tmp->name, l->name )

strcpy( tmp->addr, l->addr )

tmp->phone = l->phone

tmp->qq = l->qq

tmp->next = pos ? pos->next : *head

if ( pos ) {

pos->next = tmp

} else {

*head = tmp

}

return tmp

}

/* create a list */

node Create( void )

{

node head, t

list input

head = t = NULL

printf( "请按 [姓名] [地址] [家庭电话] [qq] 的顺序输入\n" )

printf( "每行一组数据,输入空行结束:\n" )

while ( 1 ) {

if ( getchar() == '\n' ) break

scanf( "%s%s%lu%lu", input.name, input.addr, &input.phone, &input.qq )

while ( getchar() != '\n' )

t = Insert( &head, t, &input )

}

return head

}

/* view list */

void Print( node head )

{

while ( head ) {

printf( "%s\t%s\t%lu\t%lu\n", head->name, head->addr, head->phone, head->qq )

head = head->next

}

putchar( '\n' )

}

/* merge sort */

node msort( node* head, int n )

{

int i, m

node l, r, p, *x, *y

if ( n <2 ) return *head

m = n/2

p = l = r = *head

for ( i = mi >0--i )

p = r, r = r->next

p->next = NULL

l = msort( &l, m )

r = msort( &r, n - m )

x = &p

while ( l &&r ) {

*x = l->qq <r->qq ? (y = &l, l) : (y = &r, r)

*y = (*y)->nextx = &(*x)->next

}

l = l ? l : r ? r : NULL

*x = l*head = p

return p

}

/* sort wrapper */

void Sort( node* head )

{

int i

node tmp = *head

for ( i = 0tmp++i, tmp = tmp->next )

msort( head, i )

}

int main( void )

{

node head = Create()

printf( "\n链表内容:\n" )

Print( head )

Sort( &head )

printf( "\n排序之后:\n" )

Print( head )

getch()

return 0

}