相关数据结构:链表
根据实验的要求,编写代码单独实现各问题的需求,不包含整合
输入要求: 总计n+1行,(姓名、职业、电话号码、电子邮件、通讯地址),每个联系人信息占一行,姓名、职业、电话号码、电子邮件、通讯地址用空格分隔。第1行是输入你要创建的联系人条目数n。
输出要求: 总计n行,每个联系人信息占一行,姓名、职业、电话号码、电子邮件、通讯地址用空格分隔。
输入样例 1
3
mary student 13765121111 [email protected] baoshan#road
tom student 18765656655 [email protected] baoli#road
john teacher 12655556666 [email protected] kenli#road
输出样例 1
mary student 13765121111 [email protected] baoshan#road
tom student 18765656655 [email protected] baoli#road
john teacher 12655556666 [email protected] kenli#road
#include
#include //为malloc()提供原型
typedef struct
{
char name[20]; //姓名
char job[20]; //职业
char phone[11]; //电话号码
char email[20]; //电子邮件
char location[20]; //通讯地址
}Data;
//设计一个结构体,每一个节点
typedef struct Node
{
Data data; //节点的数据域
struct Node *next; //节点的指针域
}Node,List; //Node代表节点;List代表链表
//创建链表
List* creatList()
{
List * head;
//动态内存分配
head = (List*)malloc(1*sizeof(List));
head->next = NULL;
if ( head == NULL ) {
puts("Create List failed~\n");
return NULL;
}
return head;
}
//创建节点
Node* creatNode (Data data)
{
Node* newNode = (Node*)malloc(1*sizeof(Node));
if ( newNode == NULL ) {
puts("Create newNode failed~\n");
return NULL;
}
newNode->data = data; //初始化数据
return newNode;
}
//添加元素(把数据插入链表尾部)
void push_back(List* list,Data data)
{
Node* newNode = creatNode(data);
Node* curNode = list;
while ( curNode->next != NULL ) {
curNode = curNode->next;
}
//把新结点连接到尾部
curNode->next = newNode;
}
//遍历函数
void printList (List * list)
{
Node* curNode = list->next;
while ( curNode ) {
printf("%s %s %s %s %s\n",
curNode->data.name, curNode->data.job, curNode->data.phone, curNode->data.email, curNode->data.location);
curNode = curNode->next;
}
}
int main(void)
{
int i, num,n;
Data data;
List * list = creatList();
printf("How many people to create?\n");
scanf("%d", &num);
for (i=0; i
输入要求:(1)首先输入通讯录数据,总计n+1行,(姓名、职业、电话号码、电子邮件、通讯地址),每个联系人信息占一行,姓名、职业、电话号码、电子邮件、通讯地址用空格分隔。第1行是输入你要创建的联系人条目数n。(2)待输出通讯录后,再输入需要查询的姓名。
输出要求:(1)输出通讯录中所有联系人的信息,总计n行,每个联系人信息占一行,姓名、职业、电话号码、电子邮件、通讯地址用空格分隔。(2)待输入需查询的姓名后,返回查询结果。
输入样例 1
3
mary student 13765121111 [email protected] baoshan#road
tom student 18765656655 [email protected] baoli#road
john teacher 12655556666 [email protected] kenli#road
tom
输出样例 1
mary student 13765121111 [email protected] baoshan#road
tom student 18765656655 [email protected] baoli#road
john teacher 12655556666 [email protected] kenli#road
tom student 18765656655 [email protected] baoli#road
输入样例 2
3
mary student 13765121111 [email protected] baoshan#road
tom student 18765656655 [email protected] baoli#road
john teacher 12655556666 [email protected] kenli#road
kitty
输出样例 2
mary student 13765121111 [email protected] baoshan#road
tom student 18765656655 [email protected] baoli#road
john teacher 12655556666 [email protected] kenli#road
no result!
#include
#include //为malloc()提供原型
#include //为strcmp()提供原型
typedef struct
{
char name[20]; //姓名
char job[20]; //职业
char phone[11]; //电话号码
char email[20]; //电子邮件
char location[20]; //通讯地址
}Data;
//设计一个结构体,每一个节点
typedef struct Node
{
Data data; //节点的数据域
struct Node *next; //节点的指针域
}Node,List; //Node代表节点;List代表链表
//创建链表
List* creatList()
{
List * head;
//动态内存分配
head = (List*)malloc(1*sizeof(List));
head->next = NULL;
if ( head == NULL ) {
puts("Create List failed~\n");
return NULL;
}
return head;
}
//创建节点
Node* creatNode (Data data)
{
Node* newNode = (Node*)malloc(1*sizeof(Node));
if ( newNode == NULL ) {
puts("Create newNode failed~\n");
return NULL;
}
newNode->data = data; //初始化数据
return newNode;
}
//添加元素(把数据插入链表尾部)
void push_back(List* list,Data data)
{
Node* newNode = creatNode(data);
Node* curNode = list;
while ( curNode->next != NULL ) {
curNode = curNode->next;
}
//把新结点连接到尾部
curNode->next = newNode;
}
//查找函数
void searchName(List* list,char *name)
{
if ( list == NULL ) {
printf("This List is empty!\n");
return;
}
Node* curNode = list;
// Node* endNode = NULL;
int i,temp=0;
while ( curNode ) {
if ( strcmp(curNode->data.name,name) ) {
curNode = curNode->next;
}else{
temp = 1;
break;
}
}
if ( temp == 0 ) {
printf("no result!\n");
} else {
printf("%s %s %s %s %s\n",
curNode->data.name, curNode->data.job, curNode->data.phone, curNode->data.email, curNode->data.location);
}
}
//遍历函数
void printList (List * list)
{
Node* curNode = list->next;
while ( curNode ) {
printf("%s %s %s %s %s\n",
curNode->data.name, curNode->data.job, curNode->data.phone, curNode->data.email, curNode->data.location);
curNode = curNode->next;
}
}
int main(void)
{
int i, num,n;
char name[20];
Data data;
List * list = creatList();
printf("How many people to create?\n");
scanf("%d", &num);
for (i=0; i<num; i++ ) {
scanf("%s %s %s %s %s", data.name, data.job, data.phone, data.email, data.location);
push_back(list,data);
}
printList(list);
printf("Please enter the name you want to query:\n");
scanf("%s",name);
searchName(list,name);
return 0;
}
输入要求:(1)首先输入通讯录数据,总计n+1行,(姓名、职业、电话号码、电子邮件、通讯地址),每个联系人信息占一行,姓名、职业、电话号码、电子邮件、通讯地址用空格分隔。第1行是输入你要创建的联系人条目数n。(2)输入新的数据:总共2行,第一行输入的位置,第二行输入联系人的信息(姓名、职业、电话号码、电子邮件、通讯地址),用空格分割。
输出要求:(1)输出插入新数据前的通讯录(2)输出插入新数据后的通讯录
输出插入数据后的通讯录中所有联系人的信息,总计n行,每个联系人信息占一行,姓名、职业、电话号码、电子邮件、通讯地址用空格分隔。
输入样例 1
3
mary student 13765121111 [email protected] baoshan#road
tom student 18765656655 [email protected] baoli#road
john teacher 12655556666 [email protected] kenli#road
2
kitty teacher 18766655463 [email protected] kenli#road
输出样例 1
database is:
mary student 13765121111 [email protected] baoshan#road
tom student 18765656655 [email protected] baoli#road
john teacher 12655556666 [email protected] kenli#road
after:
mary student 13765121111 [email protected] baoshan#road
kitty teacher 18766655463 [email protected] kenli#road
tom student 18765656655 [email protected] baoli#road
john teacher 12655556666 [email protected] kenli#road
输入样例2:
3
mary student 13765121111 [email protected] baoshan#road
tom student 18765656655 [email protected] baoli#road
john teacher 12655556666 [email protected] kenli#road
4
输出样例2:
database is:
mary student 13765121111 [email protected] baoshan#road
tom student 18765656655 [email protected] baoli#road
john teacher 12655556666 [email protected] kenli#road
illegal location!
#include
#include //为malloc()提供原型
#include
typedef struct
{
char name[20]; //姓名
char job[20]; //职业
char phone[11]; //电话号码
char email[20]; //电子邮件
char location[20]; //通讯地址
}Data;
//设计一个结构体,每一个节点
typedef struct Node
{
Data data; //节点的数据域
struct Node *next; //节点的指针域
}Node,List; //Node代表节点;List代表链表
//创建链表
List* creatList()
{
List * head;
//动态内存分配
head = (List*)malloc(1*sizeof(List));
head->next = NULL;
if ( head == NULL ) {
puts("Create List failed~\n");
return NULL;
}
return head;
}
//创建节点
Node* creatNode (Data data)
{
Node* newNode = (Node*)malloc(1*sizeof(Node));
if ( newNode == NULL ) {
puts("Create newNode failed~\n");
return NULL;
}
newNode->data = data; //初始化数据
return newNode;
}
//初始构建元素(把数据插入链表尾部)
void push_back(List* list,Data data)
{
Node* newNode = creatNode(data);
Node* curNode = list;
while ( curNode->next != NULL ) {
curNode = curNode->next;
}
//把新结点连接到尾部
curNode->next = newNode;
}
//遍历函数
void printList (List * list)
{
Node* curNode = list->next;
while ( curNode ) {
printf("%s %s %s %s %s\n",
curNode->data.name, curNode->data.job, curNode->data.phone, curNode->data.email, curNode->data.location);
curNode = curNode->next;
}
}
//添加元素(把数据插入指定位置) 并输出
void InsertNode(List* list,int n)
{
Data tempdata;
Node* curNode=list;
int count=0,j;
while ( curNode&&count<n ) { //循环结束时,currNode的位置便是需要插入元素的位置:n
curNode = curNode->next;
count ++;
}
if ( !curNode||count>j ) {
printf("database is:\n");
printList(list);
printf("illegal location!\n");
return;
} else {
scanf("%s %s %s %s %s",
tempdata.name, tempdata.job, tempdata.phone, tempdata.email, tempdata.location);
printf("database is:\n");
printList(list);
Node* tempNode = creatNode(tempdata);
tempNode->next = curNode->next; //将tempNode插到curNode后一位:n+1,
curNode->next = tempNode; // 但curNode的位置才是需要插入的位置,
tempNode->data = curNode->data; //所以交换两节点的数据(可以画图理解)
curNode->data = tempdata;
printf("after:\n");
printList(list);
}
}
int main(void)
{
int i, num,n;
Data data;
List * list = creatList();
// printf("How many people to create?\n");
scanf("%d", &num);
for (i=0; i<num; i++ ) {
scanf("%s %s %s %s %s", data.name, data.job, data.phone, data.email, data.location);
push_back(list,data);
}
// printf("Please enter where you want to insert:\n");
scanf("%d", &n);
InsertNode(list,n);
return 0;
}
输入要求:(1)首先输入通讯录数据,总计n+1行,(姓名、职业、电话号码、电子邮件、通讯地址),每个联系人信息占一行,姓名、职业、电话号码、电子邮件、通讯地址用空格分隔。第1行是输入你要创建的联系人条目数n。(2)输入待删除的记录号。
输出要求:(1)输出删除数据前的通讯录(2)输出删除数据后的通讯录
输出插入数据后的通讯录中所有联系人的信息,总计n行,每个联系人信息占一行,姓名、职业、电话号码、电子邮件、通讯地址用空格分隔。
输入样例 1
3
mary student 13765121111 [email protected] baoshan#road
tom student 18765656655 [email protected] baoli#road
john teacher 12655556666 [email protected] kenli#road
2
输出样例 1
database is:
mary student 13765121111 [email protected] baoshan#road
tom student 18765656655 [email protected] baoli#road
john teacher 12655556666 [email protected] kenli#road
after:
mary student 13765121111 [email protected] baoshan#road
john teacher 12655556666 [email protected] kenli#road
#include
#include //为malloc()提供原型
typedef struct
{
char name[20]; //姓名
char job[20]; //职业
char phone[11]; //电话号码
char email[20]; //电子邮件
char location[20]; //通讯地址
}Data;
//设计一个结构体,每一个节点
typedef struct Node
{
Data data; //节点的数据域
struct Node *next; //节点的指针域
}Node,List; //Node代表节点;List代表链表
//创建链表
List* creatList()
{
List * head;
//动态内存分配
head = (List*)malloc(1*sizeof(List));
head->next = NULL;
if ( head == NULL ) {
puts("Create List failed~\n");
return NULL;
}
return head;
}
//创建节点
Node* creatNode (Data data)
{
Node* newNode = (Node*)malloc(1*sizeof(Node));
if ( newNode == NULL ) {
puts("Create newNode failed~\n");
return NULL;
}
newNode->data = data; //初始化数据
return newNode;
}
//添加元素(把数据插入链表尾部)
void push_back(List* list,Data data)
{
Node* newNode = creatNode(data);
Node* curNode = list;
while ( curNode->next != NULL ) {
curNode = curNode->next;
}
//把新结点连接到尾部
curNode->next = newNode;
}
//删除元素
void deleteOne(List*list,int n)
{
//1.找到要删除的数据
int i;
Node* curNode = list; //当前节点
Node* prevNode = NULL; //上级节点
for ( i=1; i<=n&&curNode; i++) {
prevNode = curNode; //移动之前保存一下
curNode = curNode->next; //移动指针
}
//判断有没有找到要删除的元素
if ( curNode != NULL ) {
//首先让删除节点的前驱节点和后继节点相连
prevNode->next = curNode->next;
//删除当前节点
free(curNode);
}
}
//遍历函数
void printList (List * list)
{
Node* curNode = list->next;
while ( curNode ) {
printf("%s %s %s %s %s\n",
curNode->data.name, curNode->data.job, curNode->data.phone, curNode->data.email, curNode->data.location);
curNode = curNode->next;
}
}
int main(void)
{
int i, num,n;
Data data;
List * list = creatList();
printf("How many people to create?\n");
scanf("%d", &num);
for (i=0; i<num; i++ ) {
scanf("%s %s %s %s %s", data.name, data.job, data.phone, data.email, data.location);
push_back(list,data);
}
printf("Please enter the serial number you want to delete:\n");
scanf("%d", &n);
printf("database is:\n");
printList(list);
deleteOne(list,n);
printf("after:\n");
printList(list);
return 0;
}
输入要求: 总计n+1行,(姓名、职业、电话号码、电子邮件、通讯地址),每个联系人信息占一行,姓名、职业、电话号码、电子邮件、通讯地址用空格分隔。第1行是输入你要创建的联系人条目数n。
输出要求: 按姓名的字母顺序排列,总计n行,每个联系人信息占一行,姓名、职业、电话号码、电子邮件、通讯地址用空格分隔。
输入样例 1
3
mary student 13765121111 [email protected] baoshan#road
tom student 18765656655 [email protected] baoli#road
john teacher 12655556666 [email protected] kenli#road
输出样例 1
database is:
mary student 13765121111 [email protected] baoshan#road
tom student 18765656655 [email protected] baoli#road
john teacher 12655556666 [email protected] kenli#road
after:
john teacher 12655556666 [email protected] kenli#road
mary student 13765121111 [email protected] baoshan#road
tom student 18765656655 [email protected] baoli#road
#include
#include //为malloc()提供原型
#include
typedef struct
{
char name[20]; //姓名
char job[20]; //职业
char phone[11]; //电话号码
char email[20]; //电子邮件
char location[20]; //通讯地址
}Data;
//设计一个结构体,每一个节点
typedef struct Node
{
Data data; //节点的数据域
struct Node *next; //节点的指针域
}Node,List; //Node代表节点;List代表链表
//创建链表
List* creatList()
{
List * head;
//动态内存分配
head = (List*)malloc(1*sizeof(List));
head->next = NULL;
if ( head == NULL ) {
puts("Create List failed~\n");
return NULL;
}
return head;
}
//创建节点
Node* creatNode (Data data)
{
Node* newNode = (Node*)malloc(1*sizeof(Node));
if ( newNode == NULL ) {
puts("Create newNode failed~\n");
return NULL;
}
newNode->data = data; //初始化数据
return newNode;
}
//添加元素(把数据插入链表尾部)
void push_back(List* list,Data data)
{
Node* newNode = creatNode(data);
Node* curNode = list;
while ( curNode->next != NULL ) {
curNode = curNode->next;
}
//把新结点连接到尾部
curNode->next = newNode;
}
//遍历函数
void printList (List * list)
{
Node* curNode = list->next;
while ( curNode ) {
printf("%s %s %s %s %s\n",
curNode->data.name, curNode->data.job, curNode->data.phone, curNode->data.email, curNode->data.location);
curNode = curNode->next;
}
}
//排序(按姓名首字母)
void sortName(List* list)
{
Node* curNode, *laterNode, *tailNode; //curNode和laterNode用来作为比较的两个节点,tailNode始终作为尾节点
curNode=list->next->next;
list->next->next=NULL;
while(curNode!=NULL) //排序方法可以画图理解
{
laterNode=curNode->next;
tailNode=list;
while(tailNode->next!=NULL&&tailNode->next->data.name[0] < curNode->data.name[0])
{
tailNode=tailNode->next;
}
curNode->next=tailNode->next;
tailNode->next=curNode;
curNode=laterNode;
}
}
int main(void)
{
int i, num;
Data data;
List * list = creatList();
printf("How many people to create?\n");
scanf("%d", &num);
for (i=0; i<num; i++ ) {
scanf("%s %s %s %s %s", data.name, data.job, data.phone, data.email, data.location);
push_back(list,data);
}
printf("database is:\n");
printList(list);
sortName(list);
printf("after:\n");
printList(list);
return 0;
}
只是按照问题描述而编写的基础代码,仍待改进