C 语言 单链表
#include <stdio.h>#include <stdlib.h>
//头插法单链表
struct Book
{
char title;
char author;
struct Book* next;
};
void getInput(struct Book* book)
{
printf("请输入书名:");
scanf("%s", book->title);
printf("请输入作者:");
scanf("%s", book->author);
}
void addBook(struct Book** library)
{
struct Book* book, * temp;
book = (struct Book*)malloc(sizeof(struct Book));
if (book == NULL)
{
printf("内存分配失败了!");
exit(1);
}
getInput(book); //输入一个结构体节点的地址,好在里面改值
if (*library != NULL) //单链表头插法的两种情况,相应的操作
{
temp = *library;
*library = book;
book->next = temp;
}
else
{
*library = book;
book->next = NULL;
}
}
void printBook(struct Book** library)
{
struct Book* book = library;
while (book != NULL)
{
printf("书名是:%s\n", book->title);
printf("作者是:%s\n", book->author);
book = book->next; //遍历整个单链表,打印出所有的书籍!
}
}
void releaseBook(struct Book** library) //想问一下这里用两个*号到底是什么作用呢,听了好几遍还是不理解,有大佬可以解答一下吗?
{ //遍历整个单链表进行逐一的释放空间
struct Book* temp;
while (*library != NULL)
{
temp = *library;
*library = temp->next;
free(temp);
}
}
int main()
{
struct Book* library = NULL; //library是头指针,指向单链表第一个元素,即存储的是单链表第一个元素的地址
char ch;
/*addBook(&library); //若是通过library直接改值改不了,因为实参与形参单项传递,形参的改变不能影响实参,但如果传的是地址就可以改了
printf("添加书籍成功!现在打印:\n");
printf("书名是:%s\n作者是:%s\n",(*library).title,library->author); //两种形式进行操作! */
while (1)
{
printf("是否添加书籍(Y/N):");
do
{
ch = getchar();
} while (ch != 'Y' && ch != 'N' && ch != 'y' && ch != 'n');
if (ch == 'Y' || ch == 'y')
{
addBook(&library);
}
else
{
break;
}
}
printf("是否打印书籍信息(Y/N):");
do
{
ch = getchar();
} while (ch != 'Y' && ch != 'N' && ch != 'y' && ch != 'n');
if (ch == 'Y' || ch == 'y')
{
printBook(library);
}
releaseBook(&library);
return 0;
} 两个是为了在子程序里能改变传入的指针的指针所指向的指针的值
jhq999 发表于 2022-3-10 07:26
两个是为了在子程序里能改变传入的指针的指针所指向的指针的值
#include <stdio.h>
#include <stdlib.h>
struct Book
{
char title;
char author;
struct Book* next;
};
void getInput(struct Book* book)
{
printf("请输入书名:");
scanf("%s", book->title);
printf("请输入作者:");
scanf("%s", book->author);
}
void addBook(struct Book** library) 我想问一下这里如果在传值的时候不传入它的地址,那只用一个*不应该是一样的吗
{
struct Book* book, * temp;
book = (struct Book*)malloc(sizeof(struct Book));
if (book == NULL)
{
printf("内存分配失败了!");
exit(1);
}
getInput(book);
if (*library != NULL)
{
temp = *library;
*library = book;
book->next = temp;
}
else
{
*library = book;
book->next = NULL;
}
}
void printBook(struct Book** library)
{
struct Book* book = library;
while (book != NULL)
{
printf("书名是:%s\n", book->title);
printf("作者是:%s\n", book->author);
book = book->next;
}
}
void releaseBook(struct Book** library)
{
struct Book* temp;
while (*library != NULL)
{
temp = *library;
*library = temp->next;
free(temp);
}
}
int main()
{
struct Book* library = NULL;
char ch;
/*addBook(&library);
printf("添加书籍成功!现在打印:\n");
printf("书名是:%s\n作者是:%s\n",(*library).title,library->author);
while (1)
{
printf("是否添加书籍(Y/N):");
do
{
ch = getchar();
} while (ch != 'Y' && ch != 'N' && ch != 'y' && ch != 'n');
if (ch == 'Y' || ch == 'y')
{
addBook(&library);
}
else
{
break;
}
}
printf("是否打印书籍信息(Y/N):");
do
{
ch = getchar();
} while (ch != 'Y' && ch != 'N' && ch != 'y' && ch != 'n');
if (ch == 'Y' || ch == 'y')
{
printBook(library);
}
releaseBook(&library);
return 0;
} 本帖最后由 jhq999 于 2022-3-10 12:04 编辑
投入就放过 发表于 2022-3-10 09:13
#include
#include
不一样,因为实参不随形参改变的原因,只传入结构体指针,当在子程序中指针类型形参改变时,实参不改变,子程序返回后,实参还是进入子程序之前的值
int main()
{
struct Book* library = NULL;
char ch;
//addBook(library); //如果这样,addBook函数结束后, library 还是等于 NULL;
addBook(&library);//如果这样,在子函数 addBook里的library就是指向主函数里的这个指针变量library的内存地址,
//解引用子函数里的 library让它改变就相当于改变了主函数里的library这个指针变量所在的内存里的值
int *a=NULL;//实参
int *b=a;//形参
b=(int*)0x12345678;//a还是NULL
printf("%p\n",a);
int **c=&a;
*c=(int*)0x12345678;//a=0x12345678,你试试
printf("%p\n",a);
jhq999 发表于 2022-3-10 11:43
不一样,因为实参不随形参改变的原因,只传入结构体指针,当在子程序中指针类型形参改变时,实参不改变 ...
我明白了,那如果我用addBook函数返回地址那是不是就可以不用这这么做了呢 投入就放过 发表于 2022-3-10 12:05
我明白了,那如果我用addBook函数返回地址那是不是就可以不用这这么做了呢
对 jhq999 发表于 2022-3-10 12:13
对
大哥,我还想问你个问题,不是说不能返回函数的指针吗,如果这么做的话不是返回了函数的指针吗
本帖最后由 jhq999 于 2022-3-10 12:30 编辑
投入就放过 发表于 2022-3-10 12:15
大哥,我还想问你个问题,不是说不能返回函数的指针吗,如果这么做的话不是返回了函数的指针吗
你返回函数的指针干啥,而且函数指针可以返回
struct Book* addBook(struct Book* library) 我想问一下这里如果在传值的时候不传入它的地址,那只用一个*不应该是一样的吗
{
struct Book* book, * temp;
book = (struct Book*)malloc(sizeof(struct Book));
if (book == NULL)
{
printf("内存分配失败了!");
exit(1);
}
getInput(book);
if (library != NULL)
{
temp = library;
library = book;
book->next = temp;
}
else
{
library = book;
book->next = NULL;
}
return library;
}
typedef int (*addpoint)(int, int);
int add(int a,int b)
{
return a+b;
}
addpoint test()
{
return add;
}
int main(int argc,char *argv[])
{
addpoint fun=test();
printf("%d",fun(1,3));
return 0;
}
jhq999 发表于 2022-3-10 12:22
你返回函数的指针干啥,而且函数指针可以返回
不好意思,表达错了,是函数参数的指针
页:
[1]