Link list achieve Phone_Contacts

2020-04-16 11:09:09 浏览数 (1)

你是我听过最美的童话。

最近,数据结构可上学习了链表线性表,并且用线性表实现了简单的手机通讯录,并用C语言和python语言都实现了,基本理论是一样的,贵在实践。记录一下~

Pyhthon 实现

代码语言:javascript复制
class Node():
    def __init__(self,name,old,telephone,email):
        self.name = name
        self.old = old
        self.telephone = telephone
        self.email = email
        self.next = None
class phone_list():
    def __init__(self):
        self.head = Node(None,None,None,None)
        self.len = 0
    ##  新建初始化 ##
    def new_(self):
        a = input("请输入姓名:")
        while(len(a) > 4):
            print("输入错误!请重新输入(姓名长度不得超过4)。")
            a = input("请输入姓名:")
        b = input("请输入年龄:")
        while(1):
            if int(b)>=1 and int(b)<=100:
                break
            print("输入错误!请重新输入(年龄范围:1~100)。")
            b = eval(input("请输入年龄:"))
        c = input("请输入电话:")
        while(len(c) != 11):
            print("输入错误!请重新输入:")
            c = input("请输入电话:")
        d = str(input("请输入邮箱:"))
        while (1):
            flag = 0
            for i in range(len(d)):
                if (d[-4:] ==".com") and (d[i] =="@"):
                    if d[i 1] != '.':
                         flag = 1
            if flag==1:
                break
            print("输入错误!请检查邮箱格式是否正确。")
            d = input("请输入邮箱:")
        return a,b,c,d
    def creat_people(self):
        p =self.head
        while 1:
            a,b,c,d= self.new_()
            new_peo = Node(a,b,c,d)
            self.len =1
            p.next = new_peo
            p = p.next
            b = eval(input("是否继续添加?n[1] 是[2] 否nn"))
            if(b==1):
                continue
            elif(b==2):
                break
    ##  遍历  ##
    def scan_people(self):
        if (self.len==0):
            print("空无一人")
            return
        p = self.head
        while(p.next):
            print(f"姓名:{p.next.name},年龄:{p.next.old},电话:{p.next.telephone},邮件:{p.next.email}")
            p = p.next
    ##  尾插法 插入新的节点 ##
    def new_people(self):
        p = self.head
        while(p.next):
            p = p.next
        a,b,c,d = self.new_()
        n_1 = Node(a,b,c,d)
        self.len =1
        p.next = n_1
        p = p.next
    def find_people(self):
        if (self.len==0):
            print("空无一人")
            return
        a = input("请输入你要查询人的名字:")
        p = self.head
        while (p.next):
            if(p.next.name==a):
                break
            else:
                p = p.next
        print("已找到!!!n信息如下:n")
        print(f"姓名:{p.next.name},年龄:{p.next.old},电话:{p.next.telephone},邮箱:{p.next.email}")
    def fix_people(self):
        if  self.len ==0:
            print("空无一人")
            return
        a = input("请输入你要修改人的名字:")
        p = self.head
        while (p.next):
            if(p.next.name==a):
                break
            else:
                p = p.next
        b = eval(input("请输入您要修改选项n[1]姓名[2]年龄[3]电话[4]邮箱n"))
        if(b==1):
            new_name = input("请输入修改后的姓名n")
            p.next.name = new_name
        elif(b==2):
            new_old = input("请输入修改后的年龄n")
            while(1):
                if int(new_old)>=1 and int(new_old)<=100:
                    break
                print("输入错误!请重新输入(年龄范围:1~100)。")
                new_old = eval(input("请重新输入年龄:"))
            p.next.old = new_old
        elif(b==3):
            new_tele = input("请输入电话:")
            while(len(new_tele) != 11):
                print("输入错误!请重新输入:")
                new_tele = input("请输入电话:")
            p.next.old = new_tele
        elif(b==4):
            new_email = str(input("请输入邮箱:"))
            while (1):
                flag = 0
                for i in range(len(d)):
                    if (bew_email[-4:] ==".com") and (new_email[i] =="@"):
                        if d[i 1] != '.':
                             flag = 1
                if flag==1:
                    break
                print("输入错误!请检查邮箱格式是否正确。")
                new_email = input("请输入邮箱:")
            p.next.old = new_email
    def del_people(self):
        if self.len == 0:
            print("空无一人")
            return
        a = input("请输入您要删除人的姓名:")
        if(self.head.name == a):
            self.head = self.head.next
            self.len-=1
        p = self.head
        
        while(p.next):
            if(p.next.name == a):
                p.next = p.next.next
                self.len-=1
                break
            else:
                p = p.next
        if(p.name == a):
            p=None
            self.len-=1
    def length(self):
        print(f"此通讯录共有{self.len}人")
def main():
    PC = phone_list()
    while True :
        print("nn")
        print('tttHPU计算机18实验班通讯录管理程序')
        print('ttt*t python 类( 链表)实现t      *')
        print("ttt*ttt      *")
        print("ttt*t功能查询:t      *")
        print("ttt*t[1]:新建初始化t      *")
        print("ttt*t[2]:新建t      *")
        print("ttt*t[3]:查询t      *")
        print("ttt*t[4]:删除t      *")
        print("ttt*t[5]:显示组员t      *")
        print("ttt*t[6]:修改信息t      *")
        
        print("ttt*t[0]:退出t      *")
        print("ttt***  tt            ***")
        a =(input("请输入您的选择:"))
        if a.isdigit():
            a = int(a)
            if(a>=0 and a<=6):
                if (a==0):
                    print("谢谢您的使用!!!")
                    break
                if (a==1):
                    PC.creat_people()
                if (a==2):
                    PC.new_people()
                if (a==3):
                    PC.find_people()
                if (a==4):
                    PC.del_people()
                if (a==5):
                    PC.scan_people()
                if (a==6):
                    PC.fix_people()
               
        x=eval(input("是否继续?n【1】继续【2】退出"))
        if x == 1 :
               continue
        if x==2 :
               break
main()

C 语言实现

代码语言:javascript复制
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct stu_link
{
	int id;
	char name[10];
 	int age;
 	char telephone[20];
 	char email[20];
	int len;
	struct stu_link *next,*pre;
}stu;//定义结构体内变量 
stu *head,*tail,*head_1,*tail_1,*head_2,*tail_2;//定义3个链表的头指针和尾指针 
stu * creat_stu(stu *tail)//新建联系人 
{
	stu *p;
	p = (stu *)calloc(sizeof(stu),1);
	printf("请输入姓名:n");
	getchar();
    gets(p->name);
    while(strlen(p->name)>8)
    {
     	printf("输入错误!n请重新输入(名字长度小于等于4):");
     	gets(p->name);
	}
	printf("请输入年龄:n");
	scanf("%d",&p->age);
	while(p->age>100||p->age<1)
    {
     	printf("输入错误!n请重新输入(1-100): ");
     	scanf("%d",&p->age);
	}
	printf("n电话号码:");
    getchar();
    gets(p->telephone);
    while(strlen(p->telephone)!=11)
    {
     	printf("输入错误!n请重新输入:");
     	gets(p->telephone);
	}
	printf("n电子邮箱:");
    getchar();
    gets(p->email);
    int i=0;
    while(1)
    {
    	int flag = 0;
     	int len;
     	for( i=0;i<20;i  )
     	{
     		len = strlen(p->email);
     		if(p->email[i]=='@'&&p->email[len-1]=='m'&&p->email[len-2]=='o'&&p->email[len-3]=='c'&&p->email[len-4]=='.'&&p->email[len-5]!='@')
     			flag=1;
		}
		if(flag==1)
		 	break;
     	printf("输入错误!n请重新输入:");
     	gets(p->email);
	}
	tail->next = p;
	tail = p;
	return tail;
}
stu * find_stu(stu *head,stu *tail,char a[10])//查找联系人并打印信息 
{
	stu *p;
	p = head;
	if(strcmp(tail->name,a)==0)
		return tail;
	int flag=0;
	while(p->next != tail)
	{
		if(strcmp(p->name,a)==0)
		{
			flag = 1;
			return p;
		}
		else 
			p = p->next;
	}
	if(!flag)
		return NULL;
	
 }
 
void scan_stu(stu *head,stu *tail)//浏览联系人信息 
{
	stu *p;
	p = head->next;
	while(p != tail)
	{
		printf("姓名: %s  年龄: %d  电话:%s  邮箱:%sn",p->name,p->age,p->telephone,p->email);
		p = p->next;
	}
	printf("姓名:%s  年龄: %d  电话:%s  邮箱:%sn",tail->name,tail->age,tail->telephone,p->email);
}
stu * del_stu(stu *head,stu *tail)//删除指定联系人 
{
	stu *p,*q;
	p = head->next;
	printf("请输入要删除人的姓名n");
	char a[10];
	scanf("%s",&a);
	if(strcmp(p->name,a)==0)
	{
		head->next = p->next;
		head->len--;
	}
	else if(strcmp(tail->name,a)==0)
	{
		while(p->next != tail)
			p = p->next;
		
		tail = p;
		free(tail->next);
		head->len--;
		return tail;
	}
	else
	{
		while(p!=tail)
		{
			if(strcmp(p->name,a)==0)
				break;
			p = p->next;	
		}
		q =p->next;
		p->next = p->next->next;
		head->len--;
		free(q);
	}
	return tail;
}
void fix_stu(stu *head,stu *tail)//修改联系人信息 
{
	char a[10],aa[10],telephone1[20],email1[20];
	int old;
	stu *p;
	printf("请输入要修改的姓名:n");
	scanf("%s",&a);
	p = find_stu(head,tail,a);
	printf("请输入要修改的信息n[1]:姓名[2]:年龄[3]:电话[4]:邮箱n");
	int choice;
	scanf("%d",&choice);
	switch(choice)
	{
		case 1:{
			printf("请输入修改后的姓名:n");
			getchar();
			gets(aa); 
   			while(strlen(aa)>8)
    		{
     			printf("输入错误!n请重新输入(名字长度小于等于4):");
     			gets(aa);
			}
			strcpy(p->name,aa);
			break;
		}
		case 2:{
			printf("请输入修改后的年龄:n");
			scanf("%d",&old);
			while(old>100||old<1)
    		{
     			printf("输入错误!n请重新输入(1-100): ");
     			scanf("%d",&old);
			}
			p->age = old;
			break;
		}
		case 3:{
			printf("请输入修改后的电话:n");
			getchar();
    		gets(telephone1);
    		while(strlen(telephone1)!=11)
    		{
     			printf("输入错误!n请重新输入:");
     			gets(telephone1);
			}
			strcpy(p->telephone,telephone1);
			break;
		}
		case 4:{
			printf("请输入修改后的邮箱:n");
			getchar();
    		gets(email1);
    		int i=0;
    		while(1)
    		{
    			int flag = 0;
     			int len;
     			for( i=0;i<20;i  )
     			{
     				len = strlen(email1);
     				if(email1[i]=='@'&&email1[len-1]=='m'&&email1[len-2]=='o'&&email1[len-3]=='c'&&email1[len-4]=='.'&&email1[len-5]!='@')
     					flag=1;
				}
				if(flag==1)
		 			break;
     			printf("输入错误!n请重新输入:");
     			gets(email1);
			}
			strcpy(p->email,email1);
			break;
		}
	}
	printf("修改成功!!!n"); 
	
}
int main()
{
	stu *head,*tail,*s,*head_1,*tail_1,*head_2,*tail_2;
	head = (stu *)calloc(sizeof(stu),1);
	head->len=0;
	tail = head;
	head_1 = (stu *)calloc(sizeof(stu),1);
	head_1->len=0;
	tail_1 = head_1;
	head_2 = (stu *)calloc(sizeof(stu),1);
	head_2->len=0;
	tail_2 = head_2;
	int choice;
	while(1)
	{
		system("cls");
   		printf("nnn");
   		printf("ttttt*****HPU计算机18实验班通讯录管理程序*****n");
   		printf("ttttt t***C语言单向链表实现***tt n");
   		printf("ttttt ttttt n");
   		printf("ttttt ttttt nttttt*t     功能列表:ttt n");
   		printf("ttttt t==========================t n"); 
   		printf("ttttt tt[1]:t新建tt n");
   		printf("ttttt tt[2]:t查询tt n");
   		printf("ttttt tt[3]:t删除tt n");
   		printf("ttttt tt[4]:t显示组员t n");
   		printf("ttttt tt[5]:t修改信息t n");
   		printf("ttttt tt[6]:t总人数tt n");
   		printf("ttttt tt[0]:t退出tt n");
	  	printf("ttttt   t==========================       n");
		printf("n选择你要的功能:n");
   		scanf("%d",&choice);
   		if(choice==0)
   		{
   			printf("谢谢您的使用!!!n"); 
   			break;
   		}
   		switch(choice)
		{
			case 1:
			{
				int choice1;
				printf("是否进行分组?n[1]:分组[2]:不分组n");
				scanf("%d",&choice1);
				switch(choice1)
				{
					case 1:{
						int choice2;
						printf("请选择分组:n[1]:家人[2]:朋友n");
						scanf("%d",&choice2);
						switch(choice2)
						{
							case 1:{
								tail_1 = creat_stu(tail_1);
								head_1->len  ;
								break;
							}
							case 2:{
								tail_2 = creat_stu(tail_2);
								head_2->len  ;
								break;
							}
						}
						break;
					}
					case 2:{
						tail = creat_stu(tail);
						head->len  ;
						break;
					}
				}
				break;
			}
			case 2:
			{
				if(head->len==0&&head_1->len==0&&head_2->len==0)
				{
					printf("空无一人n");
					break;
				}
				char a[10];
				int b;
				printf("请输入要查找人的名字n");
				scanf("%s",&a);
				printf("请选择您要查找的人位于哪个分组:n[1]:家人[2]:朋友[3]:未分组");
				scanf("%d",&b);
				if(b==1)
				{
					s = find_stu(head_1,tail_1,a);
				}
				else if(b==2)
				{
					s = find_stu(head_2,tail_2,a);
				}
				else if(b==3)
				{
					s = find_stu(head,tail,a);
				}
				//s = find_stu(head,tail,a);
				if(s != NULL)
					printf("姓名:%s  年龄:%d  电话:%s  邮箱:%sn",s->name,s->age,s->telephone,s->email); 
				else if(s==NULL) 
					printf("没有此人!n");
				break;
			}
			case 3:
			{
					if(head->len==0&&head_1->len==0&&head_2->len==0)
					{
						printf("空无一人n");
						break;
					}
					if(head->len==0)
						printf("未分组空无一人n");
					else if(head_1==0)
						printf("家人分组空无一人n");
					else if(head_2==0)
						printf("朋友分组空无一人n");
					int b;
					printf("请选择您要删除的人位于哪个分组:n[1]:家人[2]:朋友[3]:未分组");
					scanf("%d",&b);
					if(b==1)
					{
						del_stu(head_1,tail_1);
						head_1--;
					}
					else if(b==2)
					{
						del_stu(head_2,tail_2);
						head_2--;
					}
					else if(b==3)
					{
						del_stu(head,tail);
						head--;	
					}
					break;
			}
			case 4:{
				if(head->len==0&&head_1->len==0&&head_2->len==0)
				{
					printf("空无一人n");
					break;
				}
				if(head->len==0)
					printf("未分组空无一人n");
				else if(head_1==0)
					printf("家人分组空无一人n");
				else if(head_2==0)
					printf("朋友分组空无一人n");
				if(head->len!=0)
					scan_stu(head,tail);
				if(head_1->len!=0)
					scan_stu(head_1,tail_1);
				if(head_2->len!=0)
					scan_stu(head_2,tail_2);
				break;
			}
			case 5:{
				if(head->len==0&&head_1->len==0&&head_2->len==0)
				{
					printf("空无一人n");
					break;
				}
				if(head->len==0)
					printf("未分组空无一人n");
				else if(head_1==0)
					printf("家人分组空无一人n");
				else if(head_2==0)
					printf("朋友分组空无一人n");
				int b;
				printf("请选择您要查找的人位于哪个分组:n[1]:家人[2]:朋友[3]:未分组n");
				scanf("%d",&b);
				if(b==1)
					fix_stu(head_1,tail_1);
				else if(b==2)
					fix_stu(head_2,tail_2);
				else if(b==3)
					fix_stu(head,tail);
				break;
			}
			case 6:{
				printf("此通讯录共有%d人n",head->len head_2->len head_1->len);
				break;
			}
			 	
		} 
		printf("ENTER键继续(可能不止两次优~~)"); 
		getchar();
		getchar();
	}
	return 0;
 }

路漫漫其修远兮,代码还要一行一行敲!?!

0 人点赞