Home » 未分类 » Linux C 学习笔记 十三

Linux C 学习笔记 十三

数据结构

 

数组

struct Element

{

//…………

};

struct Eelement *create(size_t len);

 

int add(struct Element,struct Element e);

 

int del(struct Elements,size_t index);

 

outAll(struct Elements);

 

void desttory(struct Element);

 

 

队列

 

struct Node

{

Element e;

struct Node *next;

}

create

destroy

enqueue

dequeue

size

iterate //遍历

front //查看

 

Node:结点,数据结构中用与保存数据的单元。

 

栈结构 stack

 

push:亚栈

pop:弹栈

 

 

create()

destroy()

size()

top()

iterate()

 

 

 

链表list

create()

destroy()

insert()

delete()

size()

iterate()

ARRAY

array.h

#ifndef __ARRAY_H
#define __ARRAY_H
#include “element.h”

typedef struct array
{
Element *eHead;
size_t max_size;
size_t counter;
}Array;

//创建并初始化数据
Array *create(size_t len);
//销毁数据
void destroy(Array *arr);
//增加数组元素
ssize_t add(Array *arr,Element e);
//删除数组元素
ssize_t del(Array *arr,size_t index);
//输出数组信息
void out(Array *arr);

//
ssize_t size(Array *arr);
ssize_t update(Array *arr,size_t index,Element e);

#endif

element.h

#ifndef __ELEMENT_H__
#define __ELEMENT_H__
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<memory.h>
typedef struct employee
{

char name[20];
int age;
float salary;
}Element;

 

#endif

array.c

#include”array.h”
#define INIT_MAX_SIZE 5
//创建并初始化数据
Array *create(size_t len)
{
Array *arr = (Array *)malloc(sizeof(Array));
assert(arr != NULL);
memset(arr,0,sizeof(Array));

Element *eHead =
(Element *)malloc(len*sizeof(Element));
assert(eHead != NULL);
memset(eHead,0,len*sizeof(Element));

arr->eHead = eHead;
arr->max_size = len;
return arr;
}
//销毁数据
void destroy(Array *arr)
{
assert(arr != NULL);
if(arr->eHead != NULL)
free(arr->eHead);

free(arr);//不会修改arr = NULL 不会修改函数外的指针
arr = NULL;
}
//增加数组元素
ssize_t add(Array *arr,Element e)
{

assert(arr != NULL);
if(arr->counter >= arr->max_size)
{
Element *eNew = (Element *)
realloc(arr->eHead,(arr->max_size+1)*sizeof(Element));
if(eNew == NULL)
return -1;
arr->eHead = eNew;
arr->max_size++;
}
//添加数组元素
*(arr->eHead+arr->counter) = e;
arr->counter++;
return 0;
}
//删除数组元素
ssize_t del(Array *arr,size_t index)
{
assert(arr != NULL);
//越界
if(index >= arr->counter)
return -1;
int i;
//移动数组,填补空隙
for(i =index;i < arr->counter;i++)
{
*(arr->eHead+i) = *(arr->eHead+i+1);
}
arr->counter–;
return 0;
}

static void out_element(Element *e)
{
printf(“=============Element info=================\n”);
printf(“Name:%s “,e->name);
printf(“Age:%d “,e->age);
printf(“Salary:%f\n “,e->salary);
printf(“\n”);
}

//输出数组u信息
void out(Array *arr)
{
assert(arr != NULL);
int i;
for(i = 0;i<arr->counter;i++)
{
printf(“===%d===”,i);
out_element(arr->eHead+i);
}
}

array_test.c

#include”element.h”
#include”array.h”
int main(void)
{

Array *arr = create(2);
Element e1 = {“Zhangsan”,22,10000.0};
Element e2 = {“Lisi”,23,8000.0};
Element e3 = {“wangwu”,33,99999.0};

add(arr,e1);
add(arr,e2);
out(arr);

 

return 0;
}

 LIST

list.h

#ifndef __LIST_H__
#define __LIST_H__

#include”element.h”
typedef struct node Node;
struct node
{
Element *e;
Node *next;
};

Node *create();
void destroy(Node *head);
void insert(Node *head,Element *e);
void delete(Node *head,Element *e);
Node *find(Node *head,Element *e);
int size(Node *head);
void iterate(Node *head);

#endif

element.h

#ifndef __ELEMENT_H__
#define __ELEMENT_H__
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<memory.h>
typedef struct employee
{

int id;
char name[20];
int age;
float salary;
}Element;
void out_element(Element *e);
Element *make_element(Element *e,int id,char *name,int age,float salary);
#endif

element.c

#include”element.h”
void out_element(Element *e)
{
printf(“\n==========Element info===============\n”);
printf(“ID:%d “,e->id);
printf(“Name:%s “,e->name);
printf(“Age:%d “,e->age);
printf(“Salary:%.2f”,e->salary);
printf(“\n”);
}

Element *make_element(Element *e,int id,char *name,int age,float salary)
{
assert(e != NULL);
e->id = id;
strcpy(e->name,name);
e->age = age;
e->salary = salary;
return e;
}

list.c

#include”list.h”

/*
* 创建链表
*/
Node *create()
{
Node *head = (Node *)malloc(sizeof(Node));
assert(head != NULL);
memset(head,0,sizeof(Node));

return head;
}

/*
* 销毁链表
*/
void destroy(Node *head)
{
if(head == NULL)
return ;
destroy(head->next);
free(head);
printf(“freed: %p\n”,head);
}

/*
* 向链表中插入元素
*/
void insert(Node *head,Element *e)
{
assert(head != NULL);

//创建新的节点
Node *pNew = (Node *)malloc(sizeof(Node));
assert(pNew != NULL);
memset(pNew,0,sizeof(Node));

pNew->e = e;

Node *pPre = head;
Node *pCur = head->next;

while(pCur != NULL)
{
if(pCur->e->id >= e->id)//找到元素合适的插入位置
break;

//指针后移
pPre = pPre->next;
pCur = pCur->next;
}
//查找一个和合适的插入位置

pNew->next = pCur;
pPre->next = pNew;
}

/*
* 删除链表中某个元素的节点
*/
void delete(Node *head,Element *e)
{
assert(head != NULL);
Node *pCur = head->next;
Node *pPre = head;
while(pCur != NULL)
{
if(pCur->e->id == e->id)//找到了合适的插入位置
break;
pPre = pPre->next;
pCur = pCur->next;
}

//空链表,或者未找到
if(pCur == NULL)
return ;
//删除

pPre->next = pCur->next;
free(pCur);
}

/*
* 查找包含某个元素的节点
*/
Node *find(Node *head,Element *e)
{
assert(head != NULL);
Node *pCur = head->next;
while(pCur != NULL)
{
if(pCur->e->id == e->id)
break;
pCur = pCur->next;
}
return pCur;
}

/*
* 返回链表的长度
*/
int size(Node *head)
{
assert(head != NULL);
int counter = 0;
Node *pCur = head->next;
while(pCur != NULL)
{
pCur = pCur->next;
counter++;
}
return counter;
}

/*
* 遍历链表
*/
void iterate(Node *head)
{
assert(head != NULL);
Node *pCur = head->next;
while(pCur != NULL)
{
out_element(pCur->e);
pCur = pCur->next;
}

//if(head == NULL)
//return;
//iteratr(head->next);
//if(head->e != NULL)
//out_element(head->e);
}

list_test.c 

#include “list.h”
int main(void)
{

Node *list = create();
Element *e1 = (Element *)malloc(sizeof(Element));
Element *e2 = (Element *)malloc(sizeof(Element));
Element *e3 = (Element *)malloc(sizeof(Element));

make_element(e1,1,”Zhangsan”,22,10000.0);
make_element(e2,2,”Lisi”,33,999999.0);
make_element(e3,3,”wangwu”,44,8888.0);

insert(list,e1);
insert(list,e2);
insert(list,e3);

iterate(list);

printf(“%d\n”,size(list));
printf(“============================\n”);

out_element(find(list,e2)->e);

delete(list,e2);
delete(list,e3);

printf(“%d\n”,size(list));
destroy(list);

free(e1);
free(e2);
free(e3);

return 0;
}

This entry was posted in 未分类. Bookmark the permalink.