郝斌版《数据结构》学习笔记
时间:2015.08.31 - 2015.09.05
# include <stdio.h>
/*
typedef struct Student //typedef等价
{
int sid;
char name[100];
char sex;
} ST;
*/
typedef struct Student //typedef定义后,两种方法同时可用
{
int sid;
char name[100];
char sex;
} * PSTU, STU; //PSTU等价于struct Student * ,STU等价于struct Student
int main(void)
{
// struct Student st;
// struct Student * ps = &st;
struct Student st;
PST ps = &st;
ps->sid = 99;
printf("%d\n", ps->sid);
return 0;
}
# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
typedef struct Node //节点
{
int data; //数据域
struct Node * pNext; //指针域
} NODE, * PNODE;
//函数声明
PNODE create_list(void);
void traverse_list(PNODE pHead);
bool is_empty(PNODE pHead);
int length_list(PNODE pHead);
bool insert_list(PNODE, int, int);
bool delete_list(PNODE, int, int *); //删除的节点放到第三个参数中
int main(void)
{
PNODE pHead = NULL;
pHead = create_list(); //create_list(),创建一个非循环单链表,并将该链表的头结点的地址付给pHead
traverse_list(pHead); //链表遍历
int len = length_list(pHead);
printf("链表的长度是:%d\n", len);
/* if (is_empty(pHead))
printf("链表为空!\n");
else
printg("链表有内容!\n");
*/
return 0;
}
PNODE create_list(void)
{
int len;
int i;
int val; //用来临时存放用户输入的借结点的值
PNODE pHead = (PNODE)malloc(sizeof(NODE));
if (NULL == pHead)
{
printf("分配失败,程序终止!\n");
exit(-1);
}
PNODE pTail = pHead;
pTail->pNext = NULL;
printf("请输入您需要生成的链表节点的个数:len = ");
scanf("%d", &len);
for (i=0; i<len; ++i)
{
printf("请输入第%d个节点的值:", i+1);
scanf("%d", &val);
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if (NULL == pNew)
{
printf("分配失败\n");
exit(-1);
}
pNew->data = val;
pTail->pNext = pNew; //挂到尾节点后面
pNew->pNext = NULL;
pTail = pNew;
}
return pHead;
}
void traverse_list(PNODE pHead) //数据结构用指针
{
PNODE p = pHead->pNext; //链表为空时,至少有一个头节点。
while (NULL != p)
{
printf("%d ", p->data);
p = p->pNext;
}
printf("\n");
return;
}
bool is_empty(PNODE pHead)
{
if (NULL == pHead->pNext)
return true;
else
return false;
}
int length_list(PNODE pHead)
{
PNODE p = pHead->pNext;
int len = 0;
while (NULL != p)
{
++len;
p = p->pNext;
}
return len;
}