马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1.静态链表是怎么定义的,我看了书上的代码 很迷茫,书上定义静态链表的节点的时候,也是用结构体来定义的,一个结构体里面存放节点的值和游标
2.但是如果是类似单链表的形式,用结构体来定义每一个节点,那么访问每一个节点的时候,必然是用结构体指针来访问,而书上的访问形式是数组以及结构体指针混用来访问,例如定义了结构体people,结构体里面游标是cur,访问元素的时候就是people[i].cur
3.还是说静态链表的定义其实是两个一维数组,一个数组存对应的元素,一个数组存对应的游标//用C++新建一个类,实现静态表的初始化, 获取元素的值,插入,删除操作
//要求:
//1.数据类型可以存储字符串
//2.插入数据,是插入数据后,链表的数据顺序如下:
//"ZHAO"
//"QIAN"
//"SUN"
//"LI"
//"ZHOU"
//"WU"
//"ZHENG"
//"WANG"
#include<winuser.inl>
#include<stdio.h>
#include <cstdlib>
#include<string.h>
#define MAXSIZE 100;
struct People
{
char name[20];
int cur;
struct People* next;
};
void InitialList(struct People *head)
{
struct People* people;
people = head;
int i;
for(i = 0; i < MAXSIZE - 1 ; i++)
{
people[i].cur = i + 1;
}
people[MAXSIZE - 1].cur = 0;
}
void getInput(struct People* people)
{
printf("enter name:");
scanf_s("%s", people->name, 20);
}
void printfInfo(struct People* head)
{
struct People* people;
people = head;
printf("name: %s \n", people->name);
}
void tail_add(struct People** head)
{
struct People* people, * temp;
people = (struct People*)(malloc(sizeof(struct People))); //动态申请结构体
if (people == NULL)
{
printf("memory failed");
exit(1);
}
getInput(people, 0);
//找尾节点
if (*head == NULL)
{
*head = people;
people->next = NULL;
}
else
{
temp = *head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = people;
people->next = NULL;
}
}
void find(struct People** head, char find_name[])
{
struct People* people;
people = *head;
//判断书否为空链表,为空,直接结束
if (people == NULL)
{
printf("no contact people!!!!\n");
}
else
{
while (people != NULL)
{
if (!strcmp(people->name, find_name))
{
printfInfo(people);
break;
}
else
{
people = people->next;
}
}
}
//while循环查找是否有符合条件的人或者到达链表的末端,跳出循环,
//找到相应的人,打印信息
}
void releaseMemory(struct People** head)
{
struct People* people, * temp;
people = *head;
while (people->next != NULL)
{
temp = people;
people = people->next;
free(temp);
}
}
void chang_info(struct People** head, char find_name[])
{
struct People* people;
people = *head;
//遍历链表,找到姓名相同的联系人,修改其信息
if (people == NULL)
{
printf("no contact people!!!!\n");
}
else
{
while (people != NULL)
{
if (!strcmp(people->name, find_name))
{
getInput(people, 1);
break;
}
else
{
people = people->next;
}
}
}
}
void delete_info(struct People** head, char delete_name[])
{
struct People* previous, * current;
//直接给previous赋值null,current赋值头节点,后面就不需要再用中间变量来存储之后,再进行交换地址了
//current此时就是一个空的结构体,头指针给他之后,他指向了链表的头节点
current = *head;
previous = NULL;
//找到删除值的位置的前后节点的位置
while (current != NULL && !strcmp(current->name, delete_name))
{
previous = current;
current = current->next;
}
//改变链表节点的指向,删除节点
if (previous == NULL) //空链表,直接将头指针指向他
{
printf("not found anything\n");
}
else
{
if (previous == NULL) //链表只有一个节点
{
*head = current->next;
}
else
{
previous->next = current->next;//前面用的是current 来判断有没有找到要删除的节点的位置,所以删除就是currennt的节点指向下一个
}
free(current);
}
}
void show_all(struct People** head)
{
struct People* people;
people = *head;
printf("all people in the phonebook is:\n");
while (people != NULL)
{
printfInfo(people);
people = people->next;
}
printf("\n");
}
int main()
{
}
|