聯(lián)合體
- 聯(lián)合體:多個成員變量公用同一塊空間,一個時間段只能用其中的一個成員.
- 如果成員變量都是基本數(shù)據(jù)類型,那么這個聯(lián)合體的所占空間是最大成員變量所占空間的大小
- 收尾結(jié)果,這個結(jié)構(gòu)體所占空間能容納最大成員變量所占的空間,還必須是單個最大成員變量字節(jié)數(shù)的倍數(shù)
- 如果不是基本數(shù)據(jù)類型,struct Person{int num;double score};最后收尾是按成員變量里最大字節(jié)數(shù)的成員變量的最小倍數(shù).
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);//結(jié)果為a.
}
預(yù)處理
/*
- 條件編譯
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;
}
隊列
- 隊列的意義在于定義一個結(jié)構(gòu)體指向的是鏈表的頭與尾驱显。
#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("請輸入一個數(shù)\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("隊列未創(chuàng)建\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.創(chuàng)建隊列\(zhòng)n");
printf("2.刪除數(shù)據(jù)[頭]\n");
printf("3.插入數(shù)據(jù)[尾]\n");
printf("4.打印隊列\(zhòng)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頭結(jié)點所對應(yīng)的部分就包含有第一個信息。
#include <stdio.h>
#include <stdlib.h>
typedef struct LINK
{
int num;
struct LINK *next;
}LINK,*pLINK;
int getNum()
{
int num;
printf("請輸入一個數(shù)值\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;
}
//頭已創(chuàng)建;
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;
}
//頭已創(chuàng)建;
/* 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.頭插數(shù)據(jù)\n");
printf("2.尾插數(shù)據(jù)\n");
printf("3.頭刪數(shù)據(jù)\n");
printf("4.尾刪數(shù)據(jù)\n");
printf("5.打印數(shù)據(jù)\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;
}
雙鏈表
- 雙鏈表的突出點是一個結(jié)構(gòu)體除了可以指向下一個next刁绒,還能指向上一個數(shù)據(jù)绳匀。
#include <stdio.h>
#include <stdlib.h>
typedef struct LINK
{
int num;
struct LINK *pre;
struct LINK *next;
}LINK,*pLINK;
int getNum()
{
printf("請輸入數(shù)字\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("雙鏈表創(chuàng)建成功\n");
return head;
}
void headInsertData(pLINK head)
{
if(head==NULL)
{
printf("雙鏈表沒有創(chuàng)建\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("雙鏈表沒有創(chuàng)建\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.創(chuàng)建鏈表\n");
printf("1.頭插數(shù)據(jù)\n");
printf("2.尾插數(shù)據(jù)\n");
printf("3.頭刪數(shù)據(jù)\n");
printf("4.尾刪數(shù)據(jù)\n");
printf("5.打印數(shù)據(jù)\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;
}
}
}