C高級-聯(lián)合體,預(yù)處理,隊列,無頭鏈表,雙鏈表

聯(lián)合體

  • 聯(lián)合體:多個成員變量公用同一塊空間,一個時間段只能用其中的一個成員.
    1. 如果成員變量都是基本數(shù)據(jù)類型,那么這個聯(lián)合體的所占空間是最大成員變量所占空間的大小
    2. 收尾結(jié)果,這個結(jié)構(gòu)體所占空間能容納最大成員變量所占的空間,還必須是單個最大成員變量字節(jié)數(shù)的倍數(shù)
    3. 如果不是基本數(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;
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末粒竖,一起剝皮案震驚了整個濱河市照捡,隨后出現(xiàn)的幾起案子叮贩,更是在濱河造成了極大的恐慌击狮,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件妇汗,死亡現(xiàn)場離奇詭異帘不,居然都是意外死亡,警方通過查閱死者的電腦和手機杨箭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進店門寞焙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人互婿,你說我怎么就攤上這事捣郊。” “怎么了慈参?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵呛牲,是天一觀的道長。 經(jīng)常有香客問我驮配,道長娘扩,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任壮锻,我火速辦了婚禮琐旁,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘猜绣。我一直安慰自己灰殴,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布掰邢。 她就那樣靜靜地躺著牺陶,像睡著了一般伟阔。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上掰伸,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天皱炉,我揣著相機與錄音,去河邊找鬼碱工。 笑死娃承,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的怕篷。 我是一名探鬼主播历筝,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼廊谓!你這毒婦竟也來了梳猪?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤蒸痹,失蹤者是張志新(化名)和其女友劉穎春弥,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體叠荠,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡匿沛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了榛鼎。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片逃呼。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖者娱,靈堂內(nèi)的尸體忽然破棺而出抡笼,到底是詐尸還是另有隱情,我是刑警寧澤黄鳍,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布推姻,位于F島的核電站,受9級特大地震影響框沟,放射性物質(zhì)發(fā)生泄漏藏古。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一忍燥、第九天 我趴在偏房一處隱蔽的房頂上張望校翔。 院中可真熱鬧,春花似錦灾前、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蔫敲。三九已至,卻和暖如春炭玫,著一層夾襖步出監(jiān)牢的瞬間奈嘿,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工吞加, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留裙犹,地道東北人。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓衔憨,卻偏偏與公主長得像叶圃,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子践图,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容