C语言示例八(Linuc C链表实现):写一个通讯录,可以实现添加、删除、查找、列表展示、修改等功能
程序员文章站
2024-02-29 11:17:46
...
函数声明
#ifndef LINK_H_
#define LINK_H_
typedef struct {
char username[10];
char tel[11];
struct Node *prev;
struct Node *next;
}Node;
typedef struct {
int size;
Node *head;
Node *tail;
}Link;
void addFirst(Link* lp,char username[10],char tel[11]);
void addlast(Link* lp,char username[10],char tel[11]);
void travel(Link* lp);
char Find(Link* lp,char username[],char tel[]);
void sort(Link* lp);
Node* get(Link* lp,int index);
Node* getBuyUserName(Link* lp,char username[10]);
void removeUser(Link * list);
char Revise(Link* lp,char username[10]);
#endif
函数实现
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"link.h"
void menu(){
printf("|========= 1.Add =========|\n");
printf("|========= 2.Revise =========|\n");
printf("|========= 3.Delete =========|\n");
printf("|========= 4.List =========|\n");
printf("|========= 5.Find =========|\n");
printf("|========= 6.Exit =========|\n");
}
void addFirst(Link * lp,char username[10],char tel[11]){
printf("请输入用户名字:\n");
scanf("%s",username);
printf("请输入用户电话号码:\n");
scanf("%s",tel);
Node* ptr=(Node*)malloc(sizeof(Node));
if(ptr == NULL){
perror("malloc error!");
exit(1);
}
strcpy(ptr->username,username);
strcpy(ptr->tel,tel);
if(lp->size == 0){
lp->tail=ptr;
}else{
ptr->next=lp->head;
lp->head->prev=ptr;
}
lp->head=ptr;
lp->size++;
printf("添加成功!\n");
}
void addLast(Link * lp,char username[10],char tel[11]){
Node* ptr=(Node*)malloc(sizeof(Node));
if(ptr==NULL){
perror("malloc error!");
exit(1);
}
strcpy(ptr->username,username);
strcpy(ptr->tel,tel);
if(lp->size==0){
lp->head=ptr;
}else{
lp->tail->next=ptr;
ptr->prev=lp->tail;
}
lp->tail=ptr;
lp->size++;
}
void travel(Link * lp){
printf(":=============\n");
printf("size:%d\n",lp->size);
Node * temp =lp->head;
while(temp){
printf("%x:%s:%s\n",temp,temp->username,temp->tel);
temp=temp->next;
}
printf("\n=============;\n");
}
Node* get(Link* lp,int index){
if(index<0 || index>lp->size-1){
printf("index is invalid!%d\n",index);
exit(1);
}
Node* np=lp->head;
int i;
for(i=0;i<index;i++){
np=np->next;
}
return np;
}
Node* getByUserName(Link* lp,char username[]){
Node* tp=lp->head;
while(tp){
if(strcmp(tp->username,username)==0){
return tp;
}
tp=tp->next;
}
return NULL;
}
void removeUser(Link * list){
char username[32];
printf("please input delete name:");
scanf("%s",username);
Node * node = list->head;
Node * pre = node;
while(node){
if(strcmp(node->username,username) == 0){
if(node == list->head){
Node * tmp = node;
list->head = node->next;
free(tmp);
break;
}
Node * temp = node;
pre->next = node->next;
free(temp);
break;
}
pre = node;
node = node->next;
}
if(node == NULL){
printf("No search this person\n");
return;
}
else{
printf("delete success\n");
list->size--;
}
}
char Find(Link* lp,char username[],char tel[]){
Node* fp=lp->head;
printf("请输入要查找的姓名\n");
scanf("%s",username);
int tmp = 0;
int i;
for(i=0;i<lp->size;i++){
if(strcmp(fp->username,username)==0){
printf("username:%s\n",fp->username);
printf("tel:%s\n",fp->tel);
printf("find successful!\n");
tmp =1;
break;
}
fp=fp->next;
}
if(tmp==0){
printf("未找到\n");
}
}
char Revise(Link* lp,char username[]){
printf("请输入需要修改的名字:\n");
scanf("%s",username);
Node* rp=lp->head;
char a[10];
char b[10];
int tmp = 0;
int i;
for(i=0;i<lp->size;i++){
if(strcmp(rp->username,username)==0){
printf("请输入修改后姓名:\n");
scanf("%s",&a);
strcpy(rp->username,a);
printf("请输入修改后的电话号码:\n");
scanf("%s",&b);
strcpy(rp->tel,b);
tmp =1;
printf("Revise successful!\n");
break;
}
rp=rp->next;
}
if(tmp==0){
printf("没有该姓名\n");
}
}
功能测试
#include<stdio.h>
#include<stdlib.h>
#include "link.h"
Link link ={0,NULL,NULL};
int main(int argc,char* argv){
printf(" welcome to contest list:\n");
char choise;
char username[10];
char tel[11];
while(1){
menu();
scanf("%d",&choise);
switch(choise){
case 1:
addFirst(&link,username,tel);
break;
case 2:
Revise(&link,username);
break;
case 3:
removeUser(&link);
break;
case 4:
travel(&link);
break;
case 5:
Find(&link,username,tel);
break;
case 6:
printf("game over\n");
exit(0);
}
}
}
上一篇: 通过Sqoop将关系型数据库表(MySQL)导入Hive
下一篇: java对象转型实例分析