(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);
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容