实现底层代码链表
linklist.h
# include <stdio.h>
# include <stdlib.h>
# include <assert.h>
# include "contact.h"
# include <string.h>
typedef struct contact datatype;
typedef struct linklist
{
datatype data;
struct linklist * next;
} link_node;
link_node* buynode ( datatype x) ;
link_node* find ( link_node* head, int i) ;
link_node* pushBack ( link_node* head, datatype x) ;
link_node* insert ( link_node* head, int i, datatype x) ;
link_node* dele ( link_node* head, link_node* pos) ;
linklist.c
# define _CRT_SECURE_NO_WARNINGS 1
# include "linklist.h"
link_node* buynode ( datatype x)
{
link_node* newnode = ( link_node* ) malloc ( sizeof ( link_node) ) ;
newnode-> data = x;
newnode-> next = NULL ;
return newnode;
}
link_node* find ( link_node* head, int i)
{
int j = 1 ;
if ( i == 0 )
{
return NULL ;
}
link_node* p = head;
while ( p && i != j)
{
p = p-> next;
j++ ;
}
return p;
}
link_node* pushBack ( link_node* head, datatype x) {
link_node* node = buynode ( x) ;
if ( head == NULL ) {
head = node;
return head;
}
link_node* pcur = head;
while ( pcur-> next)
{
pcur = pcur-> next;
}
pcur-> next = node;
return head;
}
link_node* insert ( link_node* head, int i, datatype x)
{
link_node* p = find ( head, i) ;
link_node* q = buynode ( x) ;
if ( i == 0 )
{
q-> next = head;
head = q;
}
else
{
q-> next = p-> next;
p-> next = q;
}
return head;
}
link_node* dele ( link_node* head, link_node* pos) {
assert ( head) ;
assert ( pos) ;
if ( pos == head) {
head = head-> next;
free ( pos) ;
return head;
}
link_node* prev = head;
while ( prev-> next != pos)
{
prev = prev-> next;
}
prev-> next = pos-> next;
free ( pos) ;
pos = NULL ;
return head;
}
实现通讯录
contact.h
typedef struct contact
{
char name[ 100 ] ;
char sex[ 10 ] ;
int age;
char tel[ 15 ] ;
char addr[ 100 ] ;
} contact;
typedef struct linklist link_node;
link_node* contact_init ( ) ;
link_node* contactADD ( link_node* head) ;
void ShowContact ( link_node* head) ;
link_node* contactFindByName ( link_node* head, char name[ ] ) ;
link_node* contactFind ( link_node* head) ;
link_node* contactDele ( link_node* head) ;
contact.c
# define _CRT_SECURE_NO_WARNINGS 1
# include "contact.h"
# include "linklist.h"
link_node* contact_init ( )
{
return NULL ;
}
link_node* contactADD ( link_node* head)
{
contact info;
printf ( "请输入姓名:\n" ) ;
scanf ( "%s" , & info. name) ;
printf ( "请输入性别:\n" ) ;
scanf ( "%s" , & info. sex) ;
printf ( "请输入年龄:\n" ) ;
scanf ( "%d" , & info. age) ;
printf ( "请输入联系电话:\n" ) ;
scanf ( "%s" , & info. tel) ;
printf ( "请输入地址:\n" ) ;
scanf ( "%s" , & info. addr) ;
head = pushBack ( head, info) ;
printf ( "插入成功!\n" ) ;
return head;
}
void ShowContact ( link_node* head) {
printf ( "%-10s %-5s %-5s %-20s %-20s\n" , "姓名" , "性别" , "年龄" , "联系电话" , "地址" ) ;
link_node* cur = head;
while ( cur)
{
printf ( "%-10s %-5s %-5d %-20s %-20s\n" ,
cur-> data. name,
cur-> data. sex,
cur-> data. age,
cur-> data. tel,
cur-> data. addr) ;
cur = cur-> next;
}
}
link_node* contactFindByName ( link_node* head, char name[ ] )
{
link_node* p = head;
while ( p)
{
if ( strcmp ( p-> data. name, name) == 0 )
{
return p;
}
p = p-> next;
}
return NULL ;
}
link_node* contactFind ( link_node* head)
{
printf ( "请输入要查找的用户名称:\n" ) ;
char name[ 100 ] ;
scanf ( "%s" , name) ;
link_node* position = contactFindByName ( head, name) ;
if ( position == NULL )
{
printf ( "联系人不存在!\n" ) ;
return NULL ;
}
printf ( "%-10s %-5s %-5s %-20s %-20s\n" , "姓名" , "性别" , "年龄" , "电话" , "地址" ) ;
printf ( "%-10s %-5s %-5d %-20s %-20s\n" , position-> data. name,
position-> data. sex,
position-> data. age,
position-> data. tel,
position-> data. addr) ;
return head;
}
link_node* contactDele ( link_node* head)
{
printf ( "请输入要删除的用户名称:\n" ) ;
char name[ 100 ] ;
scanf ( "%s" , name) ;
link_node* position = contactFindByName ( head, name) ;
if ( position == NULL )
{
printf ( "联系人不存在!\n" ) ;
return NULL ;
}
head = dele ( head, position) ;
return head;
}
主函数
test.c
# define _CRT_SECURE_NO_WARNINGS 1
# include "linklist.h"
# include "contact.h"
void menu ( )
{
printf ( " 欢迎来到班级通讯录 \n" ) ;
printf ( "##########1.增加联系人##########\n" ) ;
printf ( "##########2.删除联系人##########\n" ) ;
printf ( "##########3.查找联系人##########\n" ) ;
printf ( "##########4.查看通讯录##########\n" ) ;
printf ( "########## 0.退出 ##########\n" ) ;
}
int main ( )
{
int op = - 1 ;
link_node* head;
head = contact_init ( ) ;
do
{
menu ( ) ;
printf ( "请选择你的操作:\n" ) ;
scanf ( "%d" , & op) ;
switch ( op)
{
case 1 :
head = contactADD ( head) ;
printf ( "\n" ) ;
break ;
case 2 :
head = contactDele ( head) ;
printf ( "\n" ) ;
break ;
case 3 :
head = contactFind ( head) ;
printf ( "\n" ) ;
break ;
case 4 :
ShowContact ( head) ;
printf ( "\n" ) ;
break ;
case 0 :
printf ( "退出成功!" ) ;
break ;
default : printf ( "请重新输入:\n" ) ;
}
} while ( op != 0 ) ;
return 0 ;
}