来来来,把学到的全部输出了!!!望大佬指点指点
首先询问什么是链表?链表就是结构体变量与结构体变量连接在一起,那么通过什么来连接呢?那必定是通过指针来实现了。
主题为:如何建立一个动态的链表?分为两步走:动态内存申请+模块化设计
方法1:表头法插入
1.建立一个链表(创建一个表头表示整个链表)
2.创建结点
3.插入结点
4.删除结点
5.打印遍历链表(程序测试)
请看下列例子:
struct stu
{
int data;//数据区域
struct stu* nextp;//指针区域
};
struct stu* createlist()//建立链表
{
struct stu* headlist = (struct stu*)malloc(sizeof(struct stu));
//此时结构体指针headlist就转变成了结构体变量
//注意变量使用之前必须要被初始化
//headlist->data=1;//也可以省略这一步
headlist->nextp = NULL;
return headlist;
}
//此时创立结点,是和创立链表的时候一样的,只是多了一个参数而已
//主要是为了插入结点的那个过程设计的
struct stu* createcode(int data)
{
struct stu* newheadcode = (struct stu*)malloc(sizeof(struct stu));
newheadcode->data = data;
newheadcode->nextp = NULL;
return newheadcode;
}
//打印链表
void printlist(struct stu* createlist)
{
struct stu* pmove = createlist->nextp;
while (pmove)
{
printf("%d", pmove->data);
pmove = pmove->nextp;
}
printf("\n");
}
//插入结点:参数:插入哪个结点,插入结点的数据是多少,插入哪个链表
void insert(struct stu*headlist,int data)
{ //创建插入的结点
struct stu* newheadcode = createcode(data);
newheadcode->nextp = headlist->nextp;;
headlist->nextp = newheadcode;
}
//在加入删除结点之后,即deletenotebuappoint函数(在指定位置删除结点)后的输出结果为:>
//接下来在此基础上怎么删除结点呢?
//链表位置的删除
void deleteNodebyappoint(struct stu* headlist, int posedata)
{
//那么要怎么样做指定位置的删除呢?这是我们所要思考的
struct stu* posenode = headlist->nextp;
struct stu* posenodefront = headlist;
if (posenode == NULL)
{
printf("无法删除列表");
}
else
{
while (posenode->data != posedata)
{
posenodefront = posenode;
posenode = posenode->nextp;
if (posenode->nextp == NULL)
{
printf("无法找到指定的列表结点");
break;
}
}
posenodefront->nextp = posenode->nextp;
free(posenode);
}
}
int main()
{
struct stu* list = createlist();
insert(list, 1);
insert(list, 2);
insert(list, 3);
printlist(list);
deleteNodebyappoint(list, 2);
printlist(list);
return 0;
}
忽略insert函数那一块那么输出结果为:321
void deleteNodebyappoint(