linux學習總結(資料結構)

來源:互聯網
上載者:User

學習總結:

linux核心用到很多資料結構的知識,雖然linux是C語言編寫,但是裡面眾多內容是物件導向的思想。所以資料結構的知識很基礎,很重要。

資料結構指的是資料的邏輯結構和儲存結構及其操作:

  • 資料的邏輯結構

    線性結構      :1.線性表 2.棧 3.隊列

    非線性結構   :1.順序儲存 2.圖形結構

  • 資料的儲存結構

    順序儲存

    鏈式儲存

  • 資料的運算:檢索、排序、插入、刪除、修改等

先自己寫個線性表的操作。以後留著自己看看。

順序儲存結構:(其實函數的傳回值一定要判斷,那麼就比較完美啦,linux核心中的函數一般都有傳回值,一般的話錯誤返回-1,正確的如果只是判斷的話

返回0,所以我覺得自己定義的函數也要按照這個規矩來。如果判斷語句,迴圈語句後面只有一句的話,最好不能要{},linux核心會警示。)

#include <stdio.h>#include <stdlib.h>#define N 10typedef int datatype;typedef struct {    datatype data[N];    int last;}sqlist;sqlist * create_sqlist(){    sqlist *L;    if((L = (sqlist *)malloc(sizeof(sqlist)))<0)    {        printf("malloc error!");        return NULL;    }    L->last = -1;    return L;}int insert(sqlist * L,datatype data,int i){    int j;    if((L->last >= N-1)||(i < 0)||(i > L->last+1))        return -1;    for(j = L->last+1;j>i;j--)    {        L->data[j-1] = L->data[j];    }    L->last++;    L->data[i] = data;    return 0;}int isempty(sqlist *L){    return (L->last == -1) ;}int delete(sqlist *L,int i){    int j;    if((L->last == -1)||(i < 0)||(i > L->last))        return -1;    for(j = i; j<= L->last; j++)    {        L->data[j] = L->data[j+1];    }    L->last--;    L->data[j+1] = 0;    return 0;}int show(sqlist * L){    int i;    if(L->last ==-1)    {        printf("empty!\n");        return -1;    }    for(i = 0;i <= L->last;i++)    {        printf("data[%d] = %d\n",i,L->data[i]);    }    return 0;}int main(int argc,char * argv[]){    int i;    int ret;    sqlist * L;    L = create_sqlist();    for(i = 0;i<10;i++)    {        ret = insert(L,i,i);        if(ret == -1)        {            printf("insert error\n");            return -1;        }    }    ret = show(L);    if(ret == -1)    printf("show error\n");    printf("****************\n");    ret = delete(L,4);    if(ret == -1)        printf("delete error\n");    ret = show(L);    if(ret == -1)        printf("show error\n");    return 0;}

鏈式儲存結構:

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#define N 10
typedef int datatype;
typedef struct node {

 datatype data;
 struct node * next;
}linknode ,*linklist;

linklist create()
{
 linklist H;
 if((H = (linklist)malloc(sizeof(linknode)))==NULL)
 {
  perror("malloc");
  exit(-1);
 }
 H->next = NULL;
 printf("create\n");
 return H;
}
int lenth(linklist H)
{
 int i = 0;
 while((H->next)!=NULL)
 {
  H = H->next;
  i++;
 }
 return i;
}
int insert(linklist H,datatype data , int pos)
{
 int i;
 linklist q;
 linklist p = NULL;
 p = H;
 if((pos<0)||(pos>lenth(H)))
 {
  printf("insert error\n");
  return -1;
 }
 if((q = (linklist)malloc(sizeof(linknode))) == NULL )
 {
  perror("malloc");
  exit(-1);
 }
 for(i = 0; i< pos; i++)
 {
  p=p->next;
 }
 q->data = data;
 q->next = p->next;
 p->next = q;
 return 0;
}
int delete(linklist H,int pos)

 int i;
 linklist p;
 p = H;
 if((pos<0)||(pos>lenth(H)))
 {
  printf("delete error\n");
  exit(-1);
 }
 for(i = 0;i < pos;i++)
  H = H->next;
  p = H->next;
  H->next = p ->next;
  free(p);
  
 return 0;
}
int relist(linklist H)
{
 linklist p,q;
 p = H->next;
 H ->next = NULL;
 while(p != NULL)
 {
  q = p;
  p = p->next;
  q ->next = H->next;
  H->next = q;
 }
  

 return 0;
}
int show(linklist H)
{
 linklist p;
 p = H;
 while((p->next)!=NULL)
 {
  printf("data =%d\n",p->data);
  p=p->next;
 }
 return 0;
}
int main(int argc,char * argv[])
{
 int i = 0;
 int ret;
 linklist H;
 H = create();
 for(i = 0;i < 10;i++)
 {
 ret = insert(H,i,i);
 if(ret ==-1)
  printf("insert error\n");
 }
 printf("**********create linklist*************\n");
 ret = show(H);
 if(ret == -1)
  printf("show error\n");
 printf("**********delete 3          *********\n");
 ret = delete(H,3);
 if(ret == -1)
  printf("delete error\n");
 ret = show(H);
 printf("*********reservelist         ********\n");
 ret = relist(H);
 if(ret == -1)
  printf("reservelist error\n");
 show(H);
 return 0;
}

順序儲存與鏈式儲存的區別:
1.順序儲存在邏輯上相鄰的元素其儲存位置也是相鄰的;

2.順序儲存對資料元素的存取為隨機存取,或按地址存取;

3.儲存密度高

4.對錶的插入和刪除元素時間複雜度較差。

5.插入等操作運算耗時,元素的移動要成片移動。

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.