1.结构类型定义
结构类型将不同的数据类型组合起来,结构类型定义并没有说明任何实际的变量,它仅仅是定义一种
特殊的数据类型;
结构类型定义本身为一条语句,以分号终止;
定义的一般形式:
struct 结构名
{
类型成员 1;
类型成员 2;
.....
}
; ----------->这里有个分号,一定不能少。
例如: struct person {
char name[20];
char address[30];
int phone;
} ;
2.结构类型变量的定义
共有三种方式:
(1)先定义结构类型再定义变量名
比如: struct person pers1 , pers2; 若在程序开头预定义一个符号常量代表一个结构类型,
即: #define PERSON struct person (后面没有分号)
则PERSON 与struct person 完全等效。可以定义结构变量 : PERSON per1, per2;也可以定义结构类型。
(2)在定义类型的同时定义变量,这是一种双重定义:
struct score
{
char grade[20];
long int number;
char sex;
float database;
} stu1, stu2; ------------->定义结构类型同时定义了两个结构变量stu1,stu2。
(3)可以将结构名省略:
struct
{
......
}结构变量名表;
3.结构变量的引用
对结构变量中各个成员的访问,用操作符“.”表示,具有最高优先级,其格式:
结构变量名 . 成员名,如: per1.number = 12354;
4.结构类型数组
一个结构变量只能存放表格中的一个记录,若需要很多的时候,可以定义结构类型数组;
例如: struct person stu[35] ;初始化 stu[2] = {{“aaa”,“bbb”,......},{"dfdf",.......},.......} ;
5.结构类型指针
一个结构变量的指针指向该变量所占用的内存段的起始地址,该指针变量的值是结构变量的起始地址。
定义:
struct 结构名 *p;切记随后一定要将定义的指针指向结构类型变量,p = &结构变量;
通过p引用结构变量成员的形式:(*指针变量). 成员 或 指针变量 ->成员 ;
6.例子
(1)创建一个链表
代码:
#include <stdio.h>
#include <stdlib.h>
//定义结构体
struct node {
int num;
struct node *next;
};
int main(){
struct node *creat(struct node *head);
void print(struct node *head);
struct node *head;
head = NULL;
head = creat(head);
print(head);
return 0;
}
//创建链表,插入变量,返回一个头结点的首地址
struct node *creat(struct node *head){
int n;
struct node *p1 , *p2;
printf("please input a number, if the number <0 then exit :");
scanf("%d",&n);
while (n>0)
{
p1 = (struct node*)malloc(sizeof(struct node));
p1->num = n;
p1->next = NULL;
if (head == NULL)
head = p1;
else
p2->next = p1;
p2 = p1;
scanf("%d",&n);
}
return head;
}
//输出函数
void print(struct node *head){
struct node *temp;
temp = head;
while (temp != NULL)
{
printf("%d ", temp->num);
temp = temp->next;
}
}
(2)以学生结构体为例,实现链表的创建,元素插入和删除。
-------链表中的创建、插入、删除,以学生学号为例---------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
int num;
char name[20];
struct node *next;
};
//------------主函数---------------
int main(){
//------函数声明---------
struct node *creat(struct node *head);
struct node *insert(struct node *head ,struct node *insertnode);
struct node *Delete(struct node *head , char *pstr) ;
void print(struct node *head);
struct node *head;
head = NULL;
char str[20];
int num;
//------创建链表----------
head = creat(head);
print(head);
//------插入链表----------
struct node *insp1;
insp1 = (struct node *)malloc(sizeof(struct node));
printf("please input inserted num ,name:\n");
scanf("%d%s", &num, insp1->name );
insp1->num = num;
insp1->next = NULL;
head = insert(head, insp1);
print(head);
//-------删除某节点------
printf("please input delete student name:\n");
scanf("%s", str);
head = Delete(head, str);
print(head);
return 0;
}
//---------创建链表-----------
struct node *creat(struct node *head){
char strname[20];
int num;
struct node *p1, *p2;
//---申请节点空间,返回首地址。
p1 = p2 =(struct node *) malloc(sizeof(struct node));
printf("please input num , name :\n");
scanf("%d%s", &num, p1->name );
p1->num = num;
p1->next = NULL;
while (num > 0)
{
if(head == NULL)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 =(struct node *) malloc(sizeof(struct node));
printf("input next student num ,name:\n");
scanf("%d%s", &num, p1->name );
p1->num = num;
p1->next = NULL;
}
return head;
}
//---------插入函数,保持学生学号升序----------------
struct node *insert(struct node *head ,struct node *insertnode){
struct node *pre, *scanner;
scanner = head;
if (head == NULL)
{
head = insertnode;
insertnode->next = NULL;
}
else //链表不空的情况
{
//---找到插入位置---
while (insertnode->num > scanner->num && scanner->next != NULL)
{
pre = scanner;
scanner = scanner->next;
}
//-----判断该位置是不是在表尾-------
if (insertnode->num <= scanner->num)
{
//-----插入位置在表头------
if (head == scanner)
{
insertnode->next = scanner;
head = insertnode;
}
else //------位置在表中--------
{
insertnode->next = scanner;
pre->next = insertnode;
}
}
//------插入位置在表尾--------
else
{
scanner->next = insertnode;
insertnode->next = NULL;
}
}
return head;
}
//----------删除链表节点------
struct node *Delete(struct node *head, char *pstr){
struct node *temp , *p;
temp = head;
if(head == NULL)
return head;
while (strcmp(temp->name , pstr)!= 0 && temp->next != NULL)
{
p = temp;
temp = temp->next;
}
if (strcmp(temp->name, pstr) == 0)
{
if (temp == head)
{
head = head->next;
free(temp);
}
else
{
p->next = temp->next;
printf("already delete %s \n", temp->name);
free(temp);
}
}
else
{
printf("can't find this student !!\n");
}
return head;
}
//----------输出函数-----------
void print(struct node *head){
struct node *temp;
temp = head;
while (temp != NULL)
{
printf("%d , %s\n", temp->num, temp->name);
temp = temp->next;
}
}
最后声明:本人以学习为目的,希望通过交流获取更多的知识,如有错误,还请大家批评指正,积极交流!!!