您的位置:首页 > 理论基础 > 数据结构算法

嵌入式Linux C语言基础——ARM Linux内核常见数据结构

2013-04-16 17:16 363 查看
1、单向链表、双向链表、循环链表

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
struct STU
{char name[10];
char stuno[10];
int age;
int score;
};
typedef struct STU ElemType;
struct LNODE
{
ElemType data;
struct LNODE *next;
};
typedef struct LNODE LNode;
typedef struct LNODE *LinkList;

int init(LinkList *L)
{*L=(LNode *)malloc(sizeof(LNode));
if(!L)
exit(0);
memset(&((*L)->data),0,sizeof(struct STU));
(*L)->next=NULL;
return 1;
}

int ListInsert(LinkList L,int i,ElemType e)
{
LinkList p,s;
int j=0;
p=L;

while(p&&j<i-1)
{p=p->next;
++j;
}
if(!p||j>i-1) return 0;

s=(LinkList)malloc(sizeof(LNode));
s->data=e;

s->next=p->next;
p->next=s;
return 1;
}

int Less_EqualList(ElemType *e1,ElemType *e2)
{if(strcmp(e1->name,e2->name)<=0)
return 1;
else
return 0;
}

void MergeList(LinkList La,LinkList Lb,LinkList *Lc)
{
LinkList pa,pb,pc;
pa=La->next;
pb=Lb->next;
*Lc=pc=La;
while(pa&&pb)
{
if(Less_EqualList(&pa->data,&pb->data))
{pc->next=pa;pc=pa;pa=pa->next;}
else
{pc->next=pb;pc=pb;pb=pb->next;}
}
pc->next=pa?pa:pb;
free(Lb);
}

void printlist(LinkList L)
{
int i;
LinkList p;
p=L;
printf("name      stuno     age     score\n");
while(p->next)
{p=p->next;
printf("%-10s %s\t%d\t%d\n",p->data.name,p->data.stuno,p->data.age,p->data.score);}
printf("\n");
}

//#include<stdio.h>
//#include<malloc.h>
//#include<stdlib.h>
main()
{
struct STU e;
LinkList La,Lb,Lc;
printf("First is InsertList function.\n");
init(&La);
strcpy(e.name,"stu4");
strcpy(e.stuno,"100001");
e.age=80;
e.score=1000;
ListInsert(La,1,e);

strcpy(e.name,"stu6");
strcpy(e.stuno,"100002");
e.age=80;
e.score=1000;
ListInsert(La,2,e);
printlist(La);
getchar();

strcpy(e.name,"stu8");
strcpy(e.stuno,"100003");
e.age=80;
e.score=1000;
ListInsert(La,3,e);
printlist(La);
getchar();

init(&Lb);
strcpy(e.name,"stu5");
strcpy(e.stuno,"100001");
e.age=80;
e.score=1000;
ListInsert(Lb,1,e);

strcpy(e.name,"stu7");
strcpy(e.stuno,"100002");
e.age=80;
e.score=1000;
ListInsert(Lb,2,e);

strcpy(e.name,"stu1");
strcpy(e.stuno,"100003");
e.age=80;
e.score=1000;
ListInsert(Lb,3,e);
printlist(Lb);
getchar();

MergeList(La,Lb,&Lc);
printlist(Lc);
getchar();
}


2、二叉树、平衡树

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct TNode{
char data;
struct TNode *lchild;
struct TNode *rchild;
};
typedef struct TNode Node;

void init(Node **node)
{
*node=(Node *)malloc(sizeof(Node));
(*node)->lchild=(*node)->rchild=NULL;
(*node)->data=0;
}

void construct(char data,Node **node)
{
int i;
Node *temp_node=*node;
while(temp_node)
{
if(!temp_node->data)
{temp_node->data=data;
break;
}

else if(data<=temp_node->data)
{if(!temp_node->lchild)
{init(&temp_node->lchild);
temp_node->lchild->data=data;
break;}
else
{temp_node=temp_node->lchild;
continue;}
}

else if(data>temp_node->data)
{
if(!temp_node->rchild)
{init(&temp_node->rchild);
temp_node->rchild->data=data;
break;}
else
{temp_node=temp_node->rchild;
continue;}
}
}
return;
}

int PreOrderTraverse(Node *tree_node)
{
if(tree_node)
{if(printf("%c ",tree_node->data))
if(PreOrderTraverse(tree_node->lchild))
if(PreOrderTraverse(tree_node->rchild))
return 1;
return 0;
}
else
return 1;
}

void main()
{
int i;
Node *root;
char data[8]={'e','f','h','g','a','c','b','d'};
init(&root);
for(i=0;i<8;i++)
construct(data[i],&root);
printf("PreOrder.............\n");
PreOrderTraverse(root);
}


3、哈希表

实现一个采用开链址法避免冲突的哈希表,哈希函数为key%10,哈希键值如下所示:10,23,34,53,67,865,896,54,46,12,45,2345,43,234

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "malloc.h"
#define NUM 10
#define KEYNUM 14
struct elem
{int k;
int si;
};
typedef struct elem ElemType;

struct node
{ElemType data;
struct node *next;
};
typedef struct node Node;
typedef struct node *List;

ElemType key[KEYNUM];
List table[NUM];

void InitKey()
{int i;
key[0].k=10;
key[1].k=23;
key[2].k=34;
key[3].k=53;
key[4].k=67;
key[5].k=865;
key[6].k=896;
key[7].k=54;
key[8].k=46;
key[9].k=12;
key[10].k=45;
key[11].k=2345;
key[12].k=43;
key[13].k=234;
for(i=0;i<KEYNUM;i++)
{key[i].si=0;
}
printf("InitKey() success!!!\n");
}

void InitTable(List *L)
{
*L=(Node *)malloc(sizeof(Node));
if(!L)
exit(0);
memset(&((*L)->data),0,sizeof(struct elem));
(*L)->next=NULL;
}

void TableInsert(List L,ElemType e)
{List p,s;
p=L;
s=(Node *)malloc(sizeof(Node));
s->data=e;
s->next=p->next;
p->next=s;
}

void CreateList()
{int i,index;
int num[NUM];
ElemType el;
List p;

for(i=0;i<NUM;i++)
{num[i]=0;
InitTable(&table[i]);
}
for(i=0;i<KEYNUM;i++)
{index=(key[i].k)%10;
num[index]++;
el.k=key[i].k;
el.si=num[index];
index=index-2;
TableInsert(&table[index],el);
}
}

void FindList()
{int i,g=0,ch,index;
List pr;
printf("Please Enter A Key:\n");
scanf("%d",&ch);
index=ch%10;
index=index;
pr=table[index];
do
{if(pr->data.k==ch)
{g=1;
printf("The address:%d\t The Key:%d \t Search Length:%d \n",index,pr->data.k,pr->data.si);}
pr=pr->next;
}
while(pr);
if(g==0) printf("no relative key\n");
}

void DisplayList()
{int i;
List pr;
printf("Address \t\t  The Key \t\t Search Length \n");
for(i=0;i<NUM;i++)
{ pr=table[i];
printf("%d \t ",i);
do{
printf("%d \t  %d \t",pr->data.k,pr->data.si);
pr=pr->next;
}
while(pr);
printf("\n");
}
}

main()
{char ch;
InitKey();
CreateList();
do{
printf("Choose you wanted:D:DisplayList\nF:FindList\nQ:Quit\n");
ch=getchar();
switch(ch)
{
case 'D':DisplayList();break;
case 'F':FindList();break;
case 'Q':exit(0);
}
printf("come on!y/n?\n");
ch=getchar();
}while(ch!='n');
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息