(c基礎)上課筆記 12.20

  • 結構體

基本定義:
struct 結構名
{
//成員列表
};
成員列表:有基本數據類型定義的變量或者是構造類型的變量
example:
struct student
{
int grade;
int age;
char name[32];
};
student :結構體名稱
----結構體變量可以直接賦值給相同類型的結構體變量。
struct student :結構數據類型,相當于int,double,char等基本數據類型;
struct student stu; stu為結構體變量
訪問結構體成員:
stu.name;
stu.grade;
stu.age;

  • 結構體的初始化1

struct student
{
char name[32]
char sex;
int age;
};
struct student boy;
strcpy(boy.name,"jeck");
boy.age=24;
boy.sex='m';

  • 初始化2

struct student girl={"jecl"}

#include <stdio.h>
#include <string.h>
struct student 
{
    char name[32];
    char sex;
    int age;
};
int main()
{
    struct student stu;
    strcpy(stu.name,"lisan");
    stu.sex='m';
    stu.age=24;//結構體的初始化
    printf("name %s,sex %c,age %d\n",stu.name,stu.sex,stu.age);
    printf("方法二\n");
    //初始化必須要和成員列表一一對應。
    struct student stu1={"lily",'f',20};//結構體的初始化
    printf("name %s,sex %c,age %d\n",stu1.name,stu1.sex,stu1.age);
    return 0;
}
  • 初始化3(不建議):定義的同時初始化

#include <stdio.h>
#include <string.h>
struct student 
{
    char name[32];
    char sex;
    int age;
}stu,stu1,stu2;//stu={"xiaoming",'m',23};
  • 無名結構體

(一般不使用)

#include <stdio.h>
#include <string.h>
struct
{
    char name[32];
    int age;
}stu;
  • 宏定義結構體

#include <stdio.h>
#include <string.h>
struct student
{
    char name[32];
    int age;
};
#define STU struct student
STU stu;
  • 結構體的嵌套

#include <stdio.h>
#include <string.h>
struct date
{
    int year;
    int month;
    int day;
};
struct sutdent
{
    char name[32];
    int age;
    struct date birthday;
};
int main()
{
    struct student sut;
    strcpy(stu.name,"hello")'\;
    stu.age=34;
    stu.birthday.year=1900;
    stu.birthday.month=5;
    stu.birthday.day=6;
    printf("%s,%d,%d,%d,%d\n",stu.name,sut.birthday.year,sut.birthday.month,stu.birthday.day);
    return 0;
}
  • 嵌套定義結構體

struct sutdent
{
    int a;
    char b;
    struct student stu;//無法確定大小,不能分配空間
};
struct sutdent1
{
    int a;
    char b;
    struct student1 *ps;//所以的指針類型大小都為8個字節
};
  • 結構體數組

#include <stdio.h>
#include <string.h>
struct sutdent
{
        int age;
    char name[32];
};
int main()
{
    struct student stu[3]=
    {
     {24,"hello"},
     {20,"hello"},
     {26,"hello"},
    };
    //結構體數組的訪問
    printf("stu[1]=%d,stu[1]=%s.\n",stu[1].age,stu[1].name);
    return 0;
}
  • 結構體指針

#include <stdio.h>
#include <string.h>
struct date
{
    int year;
    int month;
    int day;
};
struct sutdent
{
    char name[32];
    int age;
    struct date birthday;
};
void main()
{
   struct sutdent stu;
   struct sutdent *p=&stu;//空指針不能訪問其值
   strcpy(p->name,"hello");
   p->age=13;
   p->birthday.year=1990;
   p->birthday.month=2;
   p->birthday.day=13;
   printf("%s,%d,%d,%d,%d\n",p->name,p->age,p->birthday.year,p->birthday.month,p->birthday.day);
}
  • 堆棧

#include <stdio.h>
#include <stdlib.h>
malloc;//申請堆空間
free;//釋放空間
pa=(struct date *)malloc(sizeof(struct date));//申請一個大小為sizeof(struct date)的堆空間。
free(pa);//釋放申請的堆空間
  • typedef

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
   typedef int I;//相當于把int重新定義了名稱為I.
   I a=4;
   int b=5;
   I c=a+b;
   printf("a=%d,b=%d,c=%d.",a,b,c);
   return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct sutdent
{
   int age;
   char name[23];
}STU;
/*
struct sutdent
{
   int age;
   char name[23];
};
typedef struct sutdent STU;//這樣定義是一樣的
*/
int main()
{
   STU stu;
   strcpy(stu.name,"wang");
   stu.age=34;
   printf("name=%s,age=%d.\n",stu.name,stu.age);
   return 0;
}

---- 和宏定義的區別
typedef struct student STU;
#define STU struct student

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct sutdent
{
   int age;
   char name[23];
}STU;
typedef struct student* STT;
#define STD struct student*
int main()
{
   STT stu1,stu2;//stu1和stu2都是結構體指針
   STD stu3,stu4;//struct student *stu3,stu4  stu3是指針,stu4是變量
   printf("name=%s,age=%d.\n",stu.name,stu.age);
   return 0;
}
  • 結構體大小

內存對其:
Linux: 4字節
Windows: 8字節
默認從偏移量為0的位置開始儲存,每個成員

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct A    //8
{           //[0-3]
    char a; //[0]
    int b;  //[4-7]
};
struct B    //12
{
    char a; //[0]
    int c;  //[4-7]
    char b; //[8]
};
struct C
{
    int a;
    char b;
};
struct D    //8
{
    char a; //[0]
    char b; //[1]
    int c; //[4-7]
};
struct E   //8
{
    char a; //[0]
    int c; //[4-7]
    char b[23]; //[1]
};
struct F    //6
{
    char a; //[0]
    short c //[2-3]
    char b; //[4]
};
struct G    //8
{
    char a; //[0]
    char b; //[1]
    short c; //[2-3]
};
int main()
{
   printf("A:%ld\n",sizeof(struct A));
   printf("B:%ld\n",sizeof(struct B));
   printf("C:%ld\n",sizeof(struct C));
   printf("D:%ld\n",sizeof(struct D));
}
  • 聯合體union untype

{
int a;
long b;
int arr[4];//聯合體的內存大小以最大的為準
};
特點,每次只能操作一個成員變量。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
union untype
{
   int a;
   double b;
   char c;
};//最多只能操作一個變量
int main()
{
   union untype un;
   un.a=123;
   un.b=3.14;
   un.c='f';
   printf("a=%d,b=%lf,c=%c.\n",un.a,un.b,un.c);//此次錯誤,不管是賦值還是輸出同一時間只能訪問一個值。
   return 0;
}
  • 枚舉類型

#include <stdio.h>
enum entype//成員一般用大寫
{
   A,   //0
   B,   //1
   C=12,
   D,   //13
   E    //14 
};
int main()
{
   printf("A=%d\n",A);
   printf("B=%d\n",B);
   printf("C=%d\n",C);
   printf("D=%d\n",D);
   printf("E=%d\n",E);
   return 0;
}

----與switch的連用

#include <stdio.h>
enum entype//成員一般用大寫
{
   FIRST=1,   //0
   SEC,   //1
   THIRD
};
int main()
{
   int op=0;
   printf("input op:");
   scanf("%d",&op);
   switch(op)
   {
      case FIRST:printf("第一名!\n");break;
      case SEC:printf("第二名!\n");break;
      case THIRD:printf("第三名!\n");break;
      default :printf("謝謝參與!\n");break;
   }
   return 0;
}

----定義enum型變量

#include <stdio.h>
enum entype//成員一般用大寫
{
   A,
   B,
   C
};
int main()
{
   enum type en;//定義變量
   en=A;
   return 0;
}
  • 鏈表

----鏈式存儲結構,線性存儲結構,其大小可動態改變,鏈表是由一個個節點串起來的數據鏈。
----節點:由數據域和指針域組成。
----數據域:存放數據。
----指針域:存放下一個節點的地址

  • 創建鏈表

#include <stdio.h>
struct student
{
    int id;
    struct student *next;
}
struct student *head;

malloc():申請空間
free():關閉空間
創建一個頭節點:

struct student *head;
head=(struct student *)/*強制類型轉換*/malloc(sizeof(struct student));

頭節點標識一個鏈表,即鏈表名稱。
頭節點的數據域不存放數據,指針域存放

#include <stdio.h>
#include <stdlib.h>//malloc();,free();
struct student 
{
   int ID;
   char name[32];
   struct student *next;
};
#define LEN sizeof(struct student)
struct student *add_link(struct student *head)
{
  struct student *temp=(struct student *)malloc(LEN);
  printf("input ID:");
  scanf("%d",&temp->ID);
  printf("input name:");
  scanf("%s",temp->name);
  temp->next=head->next;
  head->next=temp;
  temp=NULL;
  return head;
}
void show_link(struct student *head)
{
   struct student *p=head->next;
   while(p!=NULL)
   {
    printf("\t%d\t%s\n",p->ID,p->name);
    p=p->next;
   }
}
int main()
{
   struct student *head;//創建鏈表
   head=(struct student *)malloc(LEN);
   head->next=NULL;//需要將next指針置空,防止野指針
   head=add_link(head);
   show_link(head);
   return 0;
}
  • 作業

1.尾增

#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
    int id;
    char name[32];
    int num;
    struct student *next;
}STU,*pSTU;

pSTU add(pSTU head)
{
    pSTU temp=(pSTU)malloc(sizeof(STU));
    printf("in put ID:");
    scanf("%d",&temp->id);
    printf("in put name:");
    scanf("%s",temp->name);
    printf("in put num:");
    scanf("%d",&temp->num);
    temp->next=head->next;
    head->next=temp;
    temp=NULL;
    return head;
}
void show_add(pSTU head)
{
    pSTU p=head->next;
    while(1)
    {
    if(p!=NULL)
    {
       printf("\t%d\t%s\t%d\n",p->id,p->name,p->num);
       p=p->next;
    }
       else break;
    }
}
void show_del(pSTU head)
{
    pSTU p=head->next;
    head->next=p->next;
    free(p);
    p=NULL;
}
void main()
{
    int i,n;
    pSTU head=(pSTU)malloc(sizeof(STU));
    head->next=NULL;
    for(i=0;i<3;i++)
    head=add(head);
    show_add(head);
    printf("\n");
    show_del(head);
    show_add(head);
}

2.尾減

#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
   int ID;
   char name[10];
   struct student *next;
}STU,*pSTU;
pSTU add_STU(pSTU head)
{
    pSTU temp=(pSTU)malloc(sizeof(STU)),p=head;
    temp->next=NULL;
    printf("in put(ID,name) :");
    scanf("%d,%s",&temp->ID,temp->name);
    while(p->next!= NULL)
    {
     p=p->next;
    }
     p->next=temp;
     temp=NULL;
    return head;
}
void show_del_STU(pSTU head)
{
  pSTU p=head,p1=head->next->next;
  if(head->next!=NULL)
  {
  while(p1!=NULL)
  {
  p=p->next;
  p1=p1->next;
  }
  free(p->next);
  p->next=NULL;
  }
}
void show_add_STU(pSTU head)
{
    pSTU p=head->next;
    while(1)
    {
       if(p!=NULL)
       {
       printf("%5d,%s\n",p->ID,p->name);
       p=p->next;
       }
       else break;
    }
}
void main()
{
    pSTU head=(pSTU)malloc(sizeof(STU));
    head->next=NULL;
    int i;
    for(i=0;i<5;i++)
    head=add_STU(head);
    show_add_STU(head);
    printf("\n");
    show_del_STU(head);
    show_add_STU(head);
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,739評論 6 534
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,634評論 3 419
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,653評論 0 377
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,063評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,835評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,235評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,315評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,459評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,000評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,819評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,004評論 1 370
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,560評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,257評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,676評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,937評論 1 288
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,717評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,003評論 2 374

推薦閱讀更多精彩內容