指向结构体变量的指针是结构体指针。
结构体的变量名不是指向结构体的地址,所以给指针赋值时,需要加上取址运算符&
int a[] = {1, 2, 3};
struct Example{
int a;
char b;
} example;
example = { 2, 'b'};
int *p1 = NULL;
int *p2 = NULL;
p1 = a;
p2 = &example;
调用结构体中的变量
(*p2).a = 3;
p2->a = 3;
使用结构体实现单链表添加操作
头插法
#include <stdio.h>
#include <stdlib.h>
//定义单链表一个节点
struct Book
{
char name[10];
char author[10];
struct Book *next;
};
//声明添加一个节点方法
void addBook(struct Book **libary);
//声明录入一本书的方法
void getInputBook(struct Book *book);
//声明输出链表方法
void printLibrary(struct Book *library);
//声明释放动态内存
void releaseLibrary(struct Book *library);
int main(void)
{
struct Book *library = NULL;
char flag;
while(1){
printf("是否需要录入图书数据,Y是,N否\n");
do{
flag = getchar();
}while(flag != 'Y' && flag != 'N');
if(flag == 'Y'){
addBook(&library);
}else{
break;
}
}
printf("是否需要打印图书数据,Y是,N否\n");
do{
flag = getchar();
}while(flag != 'Y' && flag != 'N');
if(flag == 'Y'){
printLibrary(library);
}
releaseLibrary(library);
return 0;
}
void addBook(struct Book **libary){
struct Book *book, *temp;
book = (struct Book *)malloc(sizeof(struct Book));
if(NULL == book){
printf("内存分配失败\n");
exit(1);
}
getInputBook(book);
if(libary != NULL){
temp = *libary;
*libary = book;
book->next = temp;
}else{
*libary = book;
book->next = NULL;
}
}
void getInputBook(struct Book *book){
printf("请输入书名\n");
scanf("%s", book->name);
printf("请输入作者\n");
scanf("%s", book->author);
}
void printLibrary(struct Book *library){
int count = 1;
while(library!= NULL){
printf("book%d\n", count);
printf("书名:%s\n", library->name);
printf("作者:%s\n", library->author);
printf("========\n");
count++;
library = library->next;
}
}
void releaseLibrary(struct Book **library){
struct Book *temp;
while(*library!=NULL){
temp = library;
*library = (*library)->next;
free(temp);
}
}
尾插法 ,只需要修改上面的addBook方法
void addBook(struct Book **libary){
struct Book *book, *temp;
static struct Book *tail;
book = (struct Book *)malloc(sizeof(struct Book));
if(NULL == book){
printf("内存分配失败\n");
exit(1);
}
getInputBook(book);
if(libary != NULL){
tail->next = book;
book->next = NULL;
}else{
*libary = book;
book->next = NULL;
}
tail = book;
}
对链表进行添加和删除操作
#include <stdio.h>
#include <stdlib.h>
//定义单链表一个节点
struct Num
{
int a;
struct Num *next;
};
//声明添加一个节点方法
void addNum(struct Num **libary, int newNum);
//声明删除一个节点方法
void delNum(struct Num **library, int newNum);
//声明输出链表方法
void printLibrary(struct Num *library);
//声明释放动态内存
void releaseLibrary(struct Num *library);
int main(void)
{
struct Num *library = NULL;
int flag;
do{
printf("请输入一条数据,(-1代表终止):");
scanf("%d", &flag);
if(flag == -1){
break;
}
addNum(&library, flag);
printLibrary(library);
}while(flag != -1);
printf("下面一个一个删除数据\n");
do{
printf("请输入一条数据,(-1代表终止):");
scanf("%d", &flag);
if(flag == -1 || library == NULL){
break;
}
delNum(&library, flag);
printLibrary(library);
}while(flag != -1);
releaseLibrary(library);
return 0;
}
void addNum(struct Num **library, int newNum){
struct Num *num, *previous, *current;
current = *library;
previous = NULL;
while(current!= NULL && (current->a < newNum)){
previous = current;
current = current->next;
}
num = (struct Num *)malloc(sizeof(struct Num));
if(NULL == num){
printf("内存分配失败\n");
exit(1);
}
num->a = newNum;
num->next = current;
if(previous != NULL){
previous->next = num;
}else{
*library = num;
}
}
void delNum(struct Num **library, int newNum){
struct Num *num, *previous, *current;
current = *library;
previous = NULL;
while(current != NULL && current->a != newNum){
previous = current;
current = current->next;
}
if(current == NULL){
printf("没有找到 %d\n", newNum);
}else{
//第一个就是找到的值
if(previous==NULL){
*library = current->next;
}else{
previous->next = current->next;
}
}
}
void printLibrary(struct Num *library){
while(library!= NULL){
printf("%d ", library->a);
library = library->next;
}
putchar('\n');
}
void releaseLibrary(struct Num *library){
struct Num *temp = library;
while(temp!=NULL){
free(library);
library = library->next;
temp = library;
}
}