數據結構筆記第2章

結構體 動態內存的分配和釋放

  • 爲什麼會出現結構體
    • 爲了表達一些複雜的數據,而普通的基本類型變量無法滿足要求
  • 什麼叫結構體
    • 結構體是用戶根據實際需要自己定義的複合數據類型
  • 如何使用結構體
    • 兩種方式:
      • struct Student st = {1000,”zhangsan”, 20};
      • struct Student * pst = &st;
      • 1.st.sid
      • 2.pst->sid
        pst所指向的結構體變量中的sid這個成員
  • 注意事項
    • 結構體變量不能加減乘除,但能相互賦值
    • 普通結構體變量和結構體指針變量作爲函數傳參的問題

結構體練習代碼

#include<stdio.h>
#include<string.h>
struct Student {
    int sid,age;
    char name[256];
};
void f(struct Student *pst);
void g(struct Student *pst);
void h(struct Student st);
int main() {
    struct Student st;
    f(&st);
    g(&st);
    h(st);
    printf("3.%d %s %d\n",st.sid,st.name,st.age);
}
void f(struct Student *pst){
    (*pst).sid=99;
    strcpy(pst->name,"lisi");
    pst->age=22;
}
void g(struct Student *pst){
    printf("1.%d %s %d\n",pst->sid,pst->name,pst->age);
}
void h(struct Student st){
    printf("2.%d %s %d\n",st.sid,st.name,st.age);
}

動態內存的分配和釋放:

  • 動態構造一維數組
    • 假設動態構造一個int型數組
      • p=(int )malloc(int len);
      • 1.malloc只有一個int型的形參,表示要求系統分配的字節數
      • 2.malloc函數的功能是請求系統len個字節的內存空間,如果請求分配成功,則返回第一個字節的地址(俗稱乾地址)轉化爲一個有實際意義的地址,因此,malloc前面必須加(數據類型*),表示吧這個舞實際意義的第一個字節的地址,轉化爲響應的地址。如:
        • int p=(int )malloc(50);
          • 表示將系統分配好的50個字節的第一個字節的地址轉化爲int *型的地址,更準確的說是把第一個字節的地址轉化爲四個字節的地址,這樣p就指向了第i+1個的4個字節。p[0]就是第一個元素,p[i]就是第i+1個元素
#include<stdio.h>
#include<malloc.h>
int main(){
    int a[5]={4,10,2,8,6};
    int len;
    printf("請輸入你需要分配的數組的長度:len = ");
    scanf("%d",&len);
    int * pArr = (int *)malloc(sizeof(int) * len);
    for(int i=0;i<len;i++)
        scanf("%d",&pArr[i]);
    for(int i=0;i<len;i++)
        printf("%d ",pArr[i]);
    free(pArr);
}

跨函數使用內存代碼

代碼1

#include<stdio.h>
#include<stdlib.h>
struct Student{
    int sid;
    int age;
}; 
struct Student * CreateStudent(void);
void ShowStudent(struct Student*pst);
int main() {
    struct Student * ps;
    ps = CreateStudent();
    ShowStudent(ps);
}
struct Student * CreateStudent(void){
    struct Student *p=(struct Student*)malloc(sizeof(struct Student)); 
    p->sid=99;
    p->age=88;
    return p;
}
void ShowStudent(struct Student*pst){
    printf("%d %d",pst->sid,pst->age);
}

代碼2

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node{
  int data;
  struct node *next;
};
struct node *CreateList(void){
    struct node *pH=NULL;
    int len;
    int i;
    int temp;//存放用戶暫時存入的結點的值域的值 
    struct node *pNew=NULL;//存放臨時分配好的結點本身的內容 
    struct node *pTail=NULL;

    pH=(struct node *)malloc(sizeof(struct node));
    if(NULL==pH){
        puts("動態內存分配失敗!");
        exit(-1); 
    }
    pH->next=NULL;
    pTail=pH;
    printf("請輸入結點個數:len = ");
    scanf("%d",&len);
    for(i=0;i<len;i++){
        printf("請輸入第%d個結點的值:",i+1);
        scanf("%d",&temp);

        //臨時構造一個結點
        pNew=(struct node *)malloc(sizeof(struct node));
        if(NULL==pNew)
            exit(-1);
        pNew->data=temp;
        pNew->next=NULL;

        pTail->next=pNew;
        pTail=pNew;

    }
    return pH;
}
bool TraversList(struct node *pH){
    struct node *p=pH->next;
    while(NULL!=p){
        printf("%d ",p->data);
        p=p->next;
    }
    printf("\n");
    return true;
}
int main() {
    struct node *pH=NULL;
    pH=CreateList();
    TraversList(pH);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章