联合体
- 联合体:多个成员变量公用同一块空间,一个时间段只能用其中的一个成员.
- 如果成员变量都是基本数据类型,那么这个联合体的所占空间是最大成员变量所占空间的大小
- 收尾结果,这个结构体所占空间能容纳最大成员变量所占的空间,还必须是单个最大成员变量字节数的倍数
- 如果不是基本数据类型,struct Person{int num;double score};最后收尾是按成员变量里最大字节数的成员变量的最小倍数.
struct Person
{
int num;
double score;
}
union Student
{
int num;
char ch[17];-->24
struct person stu;
}
*/
union Student
{
int num;
char sex;
double score;
};
int main()
{
long size=sizeof(union Student);
printf("size=%ld\n",size);//size=8,因为是共用空间.
union Student stu;
stu.ch='c';
stu.num=97;
printf("%c",stu.ch);//结果为a.
}
预处理
/*
- 条件编译
1.
#if N
#endif
2.
#if N
#elif M
#else
#endif
4.
#ifdef MM
#endif
分析:#ifdef:如果上面对MM进行过宏定义,就编译ifdef下面的语句
5.
#ifndef MM
#endif
意义与上相反
- 使用效果如下:
#define N 1
#define M 1
#include <stdio.h>
int main()
{
#if N
int a=8;
printf("a=%d\n",a);
#elif M
int b=9;
printf("b=%d\n",b);
#endif
return 0;
}
队列
- 队列的意义在于定义一个结构体指向的是链表的头与尾。
#include <stdio.h>
#include <stdlib.h>
typedef struct Queen
{
int num;
struct Queen *next;
}Queen;
typedef struct pQueen
{
Queen *first;
Queen *tail;
}pQueen;
int getNum()
{
int num;
printf("请输入一个数\n");
scanf("%d",&num);
return num;
}
pQueen *createQueen(pQueen *queenHead)
{
if(queenHead==NULL)
{
queenHead=(pQueen *)malloc(sizeof(pQueen));
queenHead->first=queenHead->tail=NULL;
return queenHead;
}
return queenHead;
}
void tailInsertData(pQueen *queenHead)
{
if(queenHead==NULL)
{
printf("队列未创建\n");
return;
}
if(queenHead->first==NULL)
{
Queen *p=(Queen *)malloc(sizeof(Queen));
p->num=getNum();
queenHead->first=p;
queenHead->tail=p;
p->next=NULL;
return;
}
Queen *p=(Queen *)malloc(sizeof(Queen));
p->num=getNum();
queenHead->tail->next=p;
queenHead->tail=p;
p->next=NULL;
}
void headDeleteData(pQueen *queenHead)
{
if(queenHead==NULL&&queenHead->tail==NULL)
{
printf("无信息删除\n");
return;
}
if(queenHead->first->next==NULL)
{
free(queenHead->tail);
queenHead->first=queenHead->tail=NULL;
return;
}
Queen *p=queenHead->first;
queenHead->first=p->next;
free(p);
p=NULL;
}
void printData(pQueen *queenHead)
{
if(queenHead==NULL||queenHead->tail==NULL)
{
printf("无信息打印\n");
return;
}
Queen *temp;
printf("head-->");
for(temp=queenHead->first;temp!=NULL;temp=temp->next)
{
printf("[%d]-->",temp->num);
}
printf("NULL\n");
return;
}
int main()
{
pQueen *queenHead=NULL;
int select;
while (1)
{
printf("==============\n");
printf("1.创建队列\n");
printf("2.删除数据[头]\n");
printf("3.插入数据[尾]\n");
printf("4.打印队列\n");
printf("5.退出\n");
printf("==============\n");
scanf("%d",&select);
switch(select)
{
case 1: queenHead=createQueen(queenHead);break;
case 2: headDeleteData(queenHead);break;
case 3: tailInsertData(queenHead);break;
case 4: printData(queenHead);break;
case 5: return;
default: break;
}
}
}
无头链表
- 无头链表的含义为head头结点所对应的部分就包含有第一个信息。
#include <stdio.h>
#include <stdlib.h>
typedef struct LINK
{
int num;
struct LINK *next;
}LINK,*pLINK;
int getNum()
{
int num;
printf("请输入一个数值\n");
scanf("%d",&num);
return num;
}
pLINK headInsertData(pLINK head,pLINK *tail)
{
if(head==NULL)
{
head=(pLINK)malloc(sizeof(LINK));
head->num=getNum();
head->next=NULL;
*tail=head;
return head;
}
//头已创建;
pLINK p=(pLINK)malloc(sizeof(LINK));
p->num=getNum();
p->next=head;
head=p;
printf("头插成功\n");
return head;
}
pLINK tailInsertData(pLINK head,pLINK *tail)
{
if(head==NULL)
{
head=(pLINK)malloc(sizeof(LINK));
head->num=getNum();
head->next=NULL;
*tail=head;
return head;
}
//头已创建;
/* pLINK p=(pLINK)malloc(sizeof(LINK));
pLINK temp;
for(temp=head;temp->next!=NULL;temp=temp->next);
p->num=getNum();
temp->next=p;
p->next=NULL;*/
pLINK p=(pLINK)malloc(sizeof(LINK));
p->num=getNum();
(*tail)->next=p;
p->next=NULL;
*tail=p;
printf("尾插成功\n");
return head;
}
pLINK headDeleteData(pLINK head,pLINK *tail)
{
if(head==NULL)
{
printf("无信息可删\n");
return NULL;
}
if(head->next==NULL)
{
free(head);
head=NULL;
*tail=NULL;
return head;
}
pLINK p=head;
head=head->next;
free(p);
p=NULL;
return head;
}
pLINK tailDeleteData(pLINK head,pLINK *tail)
{
if(head==NULL)
{
printf("无信息可删\n");
return NULL;
}
if(head->next==NULL)
{
free(head);
head=NULL;
*tail=NULL;
return head;
}
pLINK temp;
for(temp=head;temp->next!=*tail;temp=temp->next);
free(*tail);
*tail=temp;
temp->next=NULL;
return head;
}
void printData(pLINK head)
{
if(head==NULL)
{
printf("无信息打印\n");
return;
}
pLINK temp;
for(temp=head;temp!=NULL;temp=temp->next)
{
printf("[%d]-->",temp->num);
}
printf("NULL\n");
}
int main()
{
int select;
pLINK tail=NULL;
pLINK head=NULL;
while(1)
{
printf("==========\n");
printf("1.头插数据\n");
printf("2.尾插数据\n");
printf("3.头删数据\n");
printf("4.尾删数据\n");
printf("5.打印数据\n");
printf("6.返回\n");
printf("==========\n");
scanf("%d",&select);
switch(select)
{
case 1:head=headInsertData(head,&tail);break;
case 2:head=tailInsertData(head,&tail);break;
case 3:head=headDeleteData(head,&tail);break;
case 4:head=tailDeleteData(head,&tail);break;
case 5:printData(head);break;
case 6: return 0;
default:break;
}
}
return 0;
}
双链表
- 双链表的突出点是一个结构体除了可以指向下一个next,还能指向上一个数据。
#include <stdio.h>
#include <stdlib.h>
typedef struct LINK
{
int num;
struct LINK *pre;
struct LINK *next;
}LINK,*pLINK;
int getNum()
{
printf("请输入数字\n");
int num;
scanf("%d",&num);
return num;
}
pLINK createDoubleList(pLINK head)
{
if(head==NULL)
{
head=(pLINK)malloc(sizeof(LINK));
head->pre=NULL;
head->next=NULL;
}
printf("双链表创建成功\n");
return head;
}
void headInsertData(pLINK head)
{
if(head==NULL)
{
printf("双链表没有创建\n");
return;
}
if(head->next==NULL)
{
pLINK p=(pLINK)malloc(sizeof(LINK));
p->next=NULL;
head->next=p;
p->pre=head;
p->num=getNum();
return;
}
pLINK p=(pLINK)malloc(sizeof(LINK));
p->num=getNum();
p->next=head->next;
head->next=p;
p->next->pre=p;
}
void tailInsertData(pLINK head)
{
if(head==NULL)
{
printf("双链表没有创建\n");
return;
}
pLINK temp;
for(temp=head;temp->next!=NULL;temp=temp->next);
pLINK p=(pLINK)malloc(sizeof(LINK));
p->num=getNum();
p->next=NULL;
temp->next=p;
p->pre=temp;
return;
}
void headDeleteData(pLINK head)
{
if(head==NULL||head->next==NULL)
{
printf("无信息可删\n");
return;
}
if(head->next->next==NULL)
{
pLINK p=head->next;
head->next=NULL;
free(p);
p=NULL;
printf("头删成功\n");
return;
}
pLINK p=head->next;
head->next=p->next;
p->next->pre=head;
free(p);
p=NULL;
printf("头删成功\n");
return;
}
void tailDeleteData(pLINK head)
{
if(head==NULL||head->next==NULL)
{
printf("无信息可删\n");
return;
}
if(head->next->next==NULL)
{
pLINK p=head->next;
head->next=NULL;
free(p);
p=NULL;
return;
}
pLINK temp;
for(temp=head;temp->next!=NULL;temp=temp->next);
pLINK p=temp->pre;
p->next=NULL;
free(temp);
temp=NULL;
return;
}
/*
void tailDeleteData(pLINK head)
{
if(head==NULL||head->next==NULL)
{
printf("无信息可删\n");
return;
}
pLINK temp;
for(temp=head;temp->next!=NULL;temp=temp->next);
pLINK p=temp->pre;
p->next=NULL;
free(temp);
temp=NULL;
printf("尾删成功\n");
}
*/
void printData(pLINK head)
{
if(head==NULL||head->next==NULL)
{
printf("无信息访问\n");
return;
}
printf("head-->");
pLINK temp;
for(temp=head->next;temp!=NULL;temp=temp->next)
{
printf("[%d]-->",temp->num);
}
printf("NULL\n");
}
int main()
{
int select;
pLINK head;
while(1)
{
printf("==========\n");
printf("0.创建链表\n");
printf("1.头插数据\n");
printf("2.尾插数据\n");
printf("3.头删数据\n");
printf("4.尾删数据\n");
printf("5.打印数据\n");
printf("6.返回\n");
printf("==========\n");
scanf("%d",&select);
switch(select)
{
case 0:head=createDoubleList(head);break;
case 1:headInsertData(head);break;
case 2:tailInsertData(head);break;
case 3:headDeleteData(head);break;
case 4:tailDeleteData(head);break;
case 5:printData(head);break;
case 6: return 0;
default:break;
}
}
}