電商專業(yè)學(xué)習(xí)嵌入式軟件開(kāi)發(fā)第三十六天

  • C第十天

今天一天都在講鏈表,下午講了單項(xiàng)循環(huán)鏈表,而且被同樣的題型虐了4遍,這4道題目都是相似,看著也不是很難,畢竟知道解題思路,但就是不能轉(zhuǎn)化成代碼表達(dá)出來(lái)巩剖。仿照例題也寫(xiě)不出答案,真的需要好好消化一下鏈表的內(nèi)容者甲。

#include <stdio.h>
#include <stdlib.h>
struct student
{
    int data;
    struct student *next;
};
struct student *Create()
{
    int n,i;
    printf("要?jiǎng)?chuàng)建幾個(gè)節(jié)點(diǎn):");
    scanf("%d",&n);getchar();
    struct student *head=(struct student *)malloc(sizeof(struct student));
    struct student *p=(struct student *)malloc(sizeof(struct student));
    printf("請(qǐng)輸入數(shù)據(jù):");
    scanf("%d",&p->data);getchar();
    head->next=p;
    for(i=1;i<n;i++)
    {
        struct student *q=(struct student *)malloc(sizeof(struct student));
        printf("請(qǐng)輸入數(shù)據(jù):");
        scanf("%d",&q->data);getchar();
        p->next=q;
        p=q;
    }
    p->next=NULL;
    return head;
}
struct student *T_insert(struct student *head)
{
    struct student *q=(struct student *)malloc(sizeof(struct student));
    printf("請(qǐng)輸入數(shù)據(jù):");
    scanf("%d",&q->data);
    q->next=head->next;
    head->next=q;
    return head;
}
struct student *W_insert(struct student *head)
{
    struct student *q=(struct student *)malloc(sizeof(struct student));
    printf("請(qǐng)輸入數(shù)據(jù):");
    scanf("%d",&q->data);
    struct student *p=head->next;
    while(p->next)
    {
        p=p->next;
    }
    p->next=q;
    q->next=NULL;
    return head;
}
void Print(struct student *head)
{
    struct student *p=head->next;
    while(p)
    {
        printf("%d\n",p->data);
        p=p->next;
    }
}
struct student *Find2(struct student *head)
{
    int data;
    struct student *p=head;
    printf("請(qǐng)輸入要查找的值:");
    scanf("%d",&data);
    while(p->next && p->data!=data)
    {
        p=p->next;
    }
    if(p->data==data)
        return p;
    else
    {
        printf("沒(méi)有找到球切!\n");
        return NULL;
    }
}
struct student *Delete(struct student *head)
{   struct student *p=head;
    struct student *q=Find2(head);
    if(head->next==q)
    {   head->next=q->next;free(q);
        return head;
    }
    else if(NULL==q->next)
    {   while(p)
        {   p=p->next;
            if(p->next==q)
            {   p->next=NULL;free(q);
                return head;}
            }
    }
    else
    {
        while(p)
        {
            p=p->next;
            if(p->next==q)
            {
                p->next=q->next;
                free(q);
                return head;
            }
        }
    }
}
void main()
{
    struct student *head=Create();
    Print(head);
    head=Delete(head);
    Print(head);
}

練習(xí):輸入一行字符串弃舒,并以此建立一條鏈表,一個(gè)字符占一個(gè)節(jié)點(diǎn)堤尾,并對(duì)該字符串逆序輸出
如:abcde
edcba

#include <stdio.h>
#include <stdlib.h>
//確定鏈節(jié)的類型
struct node
{
    char info;
    struct node *next;
};
void main()
{   //創(chuàng)建一條鏈表
    struct node *head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    char c;
    while((c=getchar())!='\n')
    {//新建節(jié)點(diǎn)
        struct node *q=(struct node *)malloc(sizeof(struct node));
        q->info=c;
        //頭插
        q->next=head->next;
        head->next=q;
    }
    //打印鏈表的數(shù)據(jù)
    struct node *p=head->next;
    while(p)
    {
        putchar(p->info);
        p=p->next;
    }
    putchar('\n');
}

練習(xí):從鍵盤(pán)輸入2行字符服赎,并分別按輸入時(shí)的逆序建立2條鏈表并分別,然后合并2條鏈表為1條鏈表并輸出合并后的鏈表梁沧。

#include <stdio.h>
#include <stdlib.h>
//確定鏈節(jié)的類型
struct node
{
    char info;
    struct node *next;
};
struct node *Create()
{   //創(chuàng)建一條鏈表
    struct node *head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    char c;
    while((c=getchar())!='\n')
    {//新建節(jié)點(diǎn)
        struct node *q=(struct node *)malloc(sizeof(struct node));
        q->info=c;
        //頭插
        q->next=head->next;
        head->next=q;
    }
    return head;
}
    //打印鏈表的數(shù)據(jù)
void Print(struct node *head)
{
    struct node *p=head->next;
    while(p)
    {
        putchar(p->info);
        p=p->next;
    }
    putchar('\n');
}
struct node *Sum(struct node *head1,struct node *head2)
{
    struct node *p=head1->next;
    while(p->next)
    {
        p=p->next;
    }
    p->next=head2->next;
    return head1;
}
void main()
{
    struct node *head1=Create();
    Print(head1);
    struct node *head2=Create();
    Print(head2);
    struct node *head=Sum(head1,head2);
    Print(head);
}

練習(xí):從鍵盤(pán)輸入一串字符檀何,并以此建立一條正序的單項(xiàng)循環(huán)鏈表并輸出。

#include <stdio.h>
#include <stdlib.h>
//確定鏈節(jié)類型
struct node
{
    char data;
    struct node *next;
};
struct node *Create()
{   //創(chuàng)建一條單向循環(huán)鏈表
    struct node *head=(struct node *)malloc(sizeof(struct node));head->next=head;
    struct node *last=head;//last指向鏈表的最后一個(gè)節(jié)點(diǎn)
    char c;
    while((c=getchar())!='\n')
    {
        struct node *q=(struct node *)malloc(sizeof(struct node));
        q->data=c;  last->next=q;   last=q;
    }
    last->next=head;
    return last;
}
void Print(struct node *tail)
{   struct node *p=tail->next->next;
    while(p!=tail->next) //p!=head
    {
        putchar(p->data);p=p->next;
    }
    putchar('\n');
}
void main()
{
    struct node *tail=Create();
    Print(tail);
}

練習(xí):從鍵盤(pán)輸入2串字符串廷支,分別創(chuàng)建單向循環(huán)鏈表频鉴,然后把2個(gè)環(huán)合并成一個(gè)環(huán)并輸出。

#include <stdio.h>
#include <stdlib.h>
//確定鏈節(jié)類型
struct node
{
    char data;
    struct node *next;
};
struct node *Create()
{   //創(chuàng)建一條單向循環(huán)鏈表
    struct node *head=(struct node *)malloc(sizeof(struct node));head->next=head;
    struct node *last=head;//last指向鏈表的最后一個(gè)節(jié)點(diǎn)
    char c;
    while((c=getchar())!='\n')
    {
        struct node *q=(struct node *)malloc(sizeof(struct node));
        q->data=c;  last->next=q;   last=q;
    }
    last->next=head;
    return last;
}
void Print(struct node *tail)
{   struct node *p=tail->next->next;
    while(p!=tail->next) //p!=head
    {
        putchar(p->data);p=p->next;
    }
    putchar('\n');
}
struct node *Sum(struct node *tail1,struct node *tail2)
{
    struct node *head=tail1->next;
    tail1->next=tail2->next->next;
    tail2->next=head;
    return tail2;
}
void main()
{
    struct node *tail1=Create();
    Print(tail1);
    struct node *tail2=Create();
    Print(tail2);
    struct node *tail=Sum(tail1,tail2);
    Print(tail);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末恋拍,一起剝皮案震驚了整個(gè)濱河市垛孔,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌施敢,老刑警劉巖周荐,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異僵娃,居然都是意外死亡概作,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)默怨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)讯榕,“玉大人,你說(shuō)我怎么就攤上這事匙睹∮奁ǎ” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵痕檬,是天一觀的道長(zhǎng)霎槐。 經(jīng)常有香客問(wèn)我,道長(zhǎng)梦谜,這世上最難降的妖魔是什么丘跌? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮改淑,結(jié)果婚禮上碍岔,老公的妹妹穿的比我還像新娘。我一直安慰自己朵夏,他們只是感情好蔼啦,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著仰猖,像睡著了一般捏肢。 火紅的嫁衣襯著肌膚如雪奈籽。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,268評(píng)論 1 309
  • 那天鸵赫,我揣著相機(jī)與錄音衣屏,去河邊找鬼。 笑死辩棒,一個(gè)胖子當(dāng)著我的面吹牛狼忱,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播一睁,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼钻弄,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了者吁?” 一聲冷哼從身側(cè)響起窘俺,我...
    開(kāi)封第一講書(shū)人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎复凳,沒(méi)想到半個(gè)月后瘤泪,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡育八,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年对途,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片单鹿。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡掀宋,死狀恐怖深纲,靈堂內(nèi)的尸體忽然破棺而出仲锄,到底是詐尸還是另有隱情,我是刑警寧澤湃鹊,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布儒喊,位于F島的核電站,受9級(jí)特大地震影響币呵,放射性物質(zhì)發(fā)生泄漏怀愧。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一余赢、第九天 我趴在偏房一處隱蔽的房頂上張望芯义。 院中可真熱鬧,春花似錦妻柒、人聲如沸扛拨。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)绑警。三九已至求泰,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間计盒,已是汗流浹背渴频。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留北启,地道東北人卜朗。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像咕村,于是被迫代替她去往敵國(guó)和親聊替。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359

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