C语言链表实现电话簿功能


#include "stdafx.h"
#include <stdlib.h>/*为了调用system("PAUSE");*/

#include <stdio.h>
#include <string.h>
#include <windows.h>

typedef struct Info
{
    char name[15];         /*姓名*/
char city[10];         /*城市*/
char province[10];     /*省份*/
char state[10];        /*国家*/
char tel[11];          /*电话*/
};


typedef struct node
{
    struct Info data;
struct node *next;
}Node, *link;

void Stringinput (char *t, int lens, char *notice)
{
    char n[20];
do
{
       printf("%s ", notice);
  scanf("%s", n);
  if(strlen(n)>lens)
  {
            printf("\n exceed the required length!\n");
  }
}while(strlen(n)>lens);
strcpy(t,n);
}


void Enter(link l)
{
    Node *p, *q;
q=l;
while(1)
{
        p = (Node *)malloc(sizeof(Node));
   if(!p)
  {
            printf("memory malloc fail\n");
return;
   }
Stringinput(p->data.name, 15, "enter name:");/*输入姓名*/
if(strcmp(p->data.name, "0")==0)
break;
Stringinput(p->data.city, 10, "enter city:");/*输入城市*/
Stringinput(p->data.province, 10, "enter province:");/*输入省*/
Stringinput(p->data.state, 10, "enter state:");/*输入国家*/
Stringinput(p->data.tel, 10, "enter telphone:");/*输入电话号码*/
p->next = NULL;
q->next = p;
q = p;
}


Sleep(5000); /*让显示结果暂停5秒*/


}


void Display(Node *p)
{
    printf("%15s%10s%10s%10s%15s\n",p->data.name,p->data.city,p->data.province,
p->data.state,p->data.tel);
}
void Search(link l)
{
    Node *p;
char s[15];
p=l->next;
printf("enter name to Search:");
scanf("%s",s);
while(p)
{
        if(strcmp(s,p->data.name)==0)
        {
            Display(p);
Sleep(5000); /*让显示结果暂停5秒*/
return;
}
else
{
           p=p->next;
}
}
printf("Search Empty\n");


}


void Del(link l)
{
    Node *p,*q;
char s[20];
q = l;
p=l->next;
printf("enter name to Del:");
scanf("%s",s);
while(p)
{
if(strcmp(s,p->data.name)==0)
   {   
      q->next = p->next;   
  free(p); /*一定要释放*/
  printf("Del sucess!");
  break;
}
else
{
q=p;
p=p->next;
}
}
    Sleep(5000); /*让显示结果暂停5秒*/

}
void List(link l)
{
    Node *p;
p = l->next;

    while(p!=NULL)
    {
        Display(p);
p=p->next;
}
Sleep(5000); /*让显示结果暂停5秒*/
}


int  menu_select()
{
    int i;
printf("\n\n*************************ADDRESS LIST***********************\n");
printf("\t*      1.Input record         *\n");
printf("\t*      2.Delete record        *\n");
printf("\t*      3.List record          *\n");
printf("\t*      4.Search record        *\n");
printf("\t*      5.Quit                 *\n");
printf("***************************************************************\n");


do
{
printf("\n\t Enter your choice:");
scanf("%d",&i);
}while(i<0 || i>7);
return i;
}




int main(int argc , char* argv[])
{
    link l;
l = (Node *) malloc(sizeof(Node));
if(l==NULL)
{
        printf("Error1!\n");
return C_SYS_ERR;
}
else
   l->next=NULL;
system("cls");/*清空控制台屏幕*/


while(1)
{
        system("cls");
switch(menu_select())
{
            case 1:
Enter(l);
break;
case 2:
Del(l);
break;
            case 3:
List(l);
break;
case 4:
Search(l);
break;
case 5:
exit(0);

}
}


system("PAUSE");
return 0;
}




猜你喜欢

转载自blog.csdn.net/Shayne_Lee/article/details/80343955