数据结构

//顺序表的增删查改
#include<stdio.h>

#define MAX_SIZE 100

int arr[MAX_SIZE] = {0};
int size = 0;  //代表数组中有效数据个数

//数组元素初始化,一开始数组中没有有效数据,所有数据赋值成-1
void arr_init()
{
    for (int i = 0; i < MAX_SIZE; i++)
    {
        arr[i] = -1;
    }
     size = 0;
}
//判断顺序表是否满,如果满了返回1,不满返回0
int arr_full()
{
    if (size == MAX_SIZE)
    {
        return 1;
    }
    else
    {
        return 0;
    }
    //第二种判断:return(size==MAX_SIZE)
}

//判断是否空,空返回1,非空返回0
int arr_empty()
{
    return(size == 0);
}


//插入数据,提供位置,先判断数组是否满,数据移动,数据插入,返回有效数据大小
int arr_insert(int n,int data)//n是下标
{
    if (n > size)
    {
        printf("插入的位置不正确,重新输入\n");
        return size;
    }
    //arr_full()如果返回0,代表没有满,加上!则说明没有满执行下一步操作
    if (!arr_full())
    { 
        //数据移动
        for (int i = size; i >n; i--)
        {
            arr[i] = arr[i - 1];

        }
        arr[n] = data;
        return ++size;  //数据已经增加
    }
    return size;
}

//顺序表中删除数据,判断是否为空,从前往后移动,最后一个赋值-1
int arr_delete(int n)
{
    if (n > size)
    {
        printf("删除失败\n");
        return size;
    }
    //如果不为空,可以删除数据,如果不为空返回0,!0代表1
    if (!arr_empty())
    {
        //数据移动,从n+1位置移动到n,直到size位置为止
        for (int i = n; i < size; i++)
        {
            arr[i] = arr[i + 1];
        }
        arr[size - 1] = -1;
        return --size;
    }
    return size;
}
//数据查找
int arr_find(int n)
{
    if (n < size)
    {
        printf("找到的结果是%d\n", arr[n]);
        return arr[n];
    }
    else
    {
        printf("数据没有找到\n");
        return -1;
    }

}

void arr_change(int n, int data)
{
    if (arr_find(n)!=-1)
    {
        arr[n] = data;
        printf("数据修改成功\n");
    }
    else
    {
        printf("数据修改失败\n");
    }
}

//打印数组元素所有内容
void show()
{
    for (int i = 0; i < size; i++)
    {
        printf("%d\t", arr[i]);
    }
    printf("\n");
}
int main()
{
    arr_init();
    for (int i = 0; i < 10; i++)
    {
        arr_insert(0, i * 2);
    }
    show();
    arr_delete(5);
    show();
    arr_change(30, 123);
    show();
    getchar();
    return 0;
}

//栈  先进后出(FILO)
#include<stdio.h>

#define STACK_SIZE 100

int arr[STACK_SIZE] = { 0 };

int top = 0;

int stack_full()
{
    return top == STACK_SIZE;
}

int stack_empty()
{
    return top == 0;
}

int stack_insert(int data)
{
    if (!stack_full())
    {
        arr[top] = data;
        return ++top;
    }
    return -1;//代表插入失败
}
//出栈
int stack_pop(int top)
{
    if (!stack_empty())
    {
        int temp = arr[top - 1];
        top--;
        return temp;
    }
    else
    {
        printf("栈已经空了\n");
        return -1;
    }
}

int GetTop()
{
    if (!stack_empty())
    {
        return arr[top - 1];
    }
    else
    {
        return -1;
    }
}

void show()
{
    for (int i = top; i>0; i--)
    {
        printf("%d\t", arr[i-1]);
    }
}

int main()
{
    for (int i = 0; i < 10; i++)
    {
        stack_insert(i * 4);
    }
    show();
    printf("出栈元素值%d\n", stack_pop(4));
    show();
    printf("栈顶元素是%d\n", GetTop());
    show();
    getchar();
    return 0;
}
//队列..先进先出
#include<stdio.h>
#include<stdlib.h>
//链表其中一个节点
typedef struct node
{
    int data;
    struct node*next;
}QUEUE_NODE,*PQUEUE_NODE;

//队列的结构体,保存了队列的头和队列的尾部
typedef struct queue
{
    PQUEUE_NODE front;//结构体指针,指向链表头
    PQUEUE_NODE rear;
    int size;
}QUEUE;

//创建节点,malloc申请节点以及数据赋值
//返回值:创建的节点指针,加上static说明只能在当前文件使用
static PQUEUE_NODE create_node(int data)
{
    PQUEUE_NODE temp=(PQUEUE_NODE) malloc(sizeof(QUEUE_NODE));
    if (temp == NULL)
    {
        printf("节点创建失败");
        return NULL;
    }
    temp->data = data;
    temp->next = NULL;
    return temp;
}

//头节点删除,失败返回NULL,成功返回新的头结点
static int delete_node(QUEUE* que )
{
    if (que == NULL)
    {
        return ;
    }
    PQUEUE_NODE  temp = que->front->next;
    free(que->front);
    return temp;
}

//初始化队列,传入队列指针,成功返回1,失败返回0
int init(QUEUE* que)
{
    if (que == NULL)
    {
        return 0;
    }
    que->front = NULL;
    que->rear = NULL;
    que->size = 0;
    return 1;
} 

//判断队列是否空,空返回 1,非空返回0
int empty(QUEUE* que)
{

    return !que->front&& !que->rear;
    //法2return que->size == 0;
}

//入队,把数据存入队列中,
void push_queue(QUEUE* que, int DATA)
{
    //创建节点
    PQUEUE_NODE pnode = create_node(DATA);
    if (NULL == pnode)
    {
        return;
    }
    //如果尾部指针不为空,直接把数据插入到尾部指针的next位置
    if (que->rear != NULL)
    {
        (que->rear)->next = pnode;
    }
    //如果尾指针尾部为空,说明队列中没有数据,让头结点和尾部节点都指向新创建的节点
    else
    {
        que->front = pnode;
    }
    que->rear = pnode;
    que->size++;
}

//出队列,把数据从队列中删除,从队列头删除
int pop_queue(QUEUE *que)
{
    int data = que->front->data;
    //删除节点返回删除的下一个节点
    PQUEUE_NODE pDel = delete_node(que);
    //把头指针移到下一个节点
    que->front = pDel;
    que->size--;
    return data;
}

//显示所有队列数据
void show(QUEUE* que)
{
    PQUEUE_NODE p = que->front;
    while (p)
    {
        printf("%d->", p->data);
        p = p->next;
    }
    printf("NULL\n");
}

//查看队首元素
int front_queue(QUEUE* que)
{
    if (que->front == NULL)
    {
        return -1;
    }
    return que->front->data;
}

//得到队列头数据
int getfront(QUEUE* que)
{
    if (que == NULL)
    {
        return -1;
    }
    return que->front->data;
}

//得到大小
int getsize(QUEUE* que)
{
    return que->size;
}

int main()
{
    QUEUE que;
    init(&que);

    for (int i = 0; i < 10; i++)
    {
        push_queue(&que, i * 3);
    }

    show(&que);
    pop_queue(&que);
    show(&que);
    int x = getsize(&que);
    printf("%d\n", x);
    int y = getfront(&que);
    printf("%d", y);
    getchar();
}
//双向链表
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
    char data;
    struct node* prev;
    struct node* next;
}NODE,*PNODE;

typedef struct list
{
    PNODE head;//头部
    PNODE tail;//尾部
    int size;
}LIST;

static PNODE create_node(char data, PNODE prev, PNODE next)
{
    PNODE pnew=(PNODE)malloc(sizeof(NODE));
    if (NULL == pnew)
    {
        printf("error");
        return NULL;
    }
    pnew->data = data;
    pnew->next = next;
    pnew->prev = prev;
    return pnew;
}

//初始化
void init(LIST* list)
{
    list->head = NULL;
    list->tail = NULL;
    list->size = 0;
}

//判断空
int empty(LIST*list)
{
    return list->size == 0;
}

//头部添加数据
int insert_head(LIST*list, int n, char data)
{
    list->head= create_node(data, NULL, list->head);
    if (list->head == NULL)
    {
        printf("error,创建失败");
        return 0;
    }
    //说明只有一个节点
    if (list->head->next == NULL)
    {
        list->head->next->prev = list->head;
    }
    else
    {
        list->tail = list->head;
    }
    list->size++;
    return list->size;
}

//数据删除,第一个和c匹配的节点
int del_n(char c, LIST*list)
{
    PNODE ptemp = list->head;
    if (!empty(list))
    {
        while (ptemp!=NULL)
        {
            if (ptemp->data != c)
            {
                ptemp = ptemp->next;
            }
            else
            { 
                break; 
            }

        }
        if (ptemp->prev == NULL)
        {
            list->head = list->head->next;
            list->head->prev = NULL;
            free(ptemp);
            list->size--;
        }
        else if (ptemp->next == NULL)
        {
            delete_end(list);
        }
        else
        {
            PNODE p = ptemp->prev;
            p->next = ptemp->next;
            ptemp->next->prev = p;
            free(ptemp);
            list->size--;
        }
        return list->size;
    }
    return 0;
}

//尾部追加
void append(LIST* list, char data)
{
    //创建节点,尾指针指向新创建节点
    list->tail=create_node(data, list->tail, NULL);
    if (list->tail == NULL)
    {
        printf("error,创建节点失败");
        return;
    }
    //说明只有一个节点
    if (list->tail->prev == NULL)
    {
        list->head = list->tail;
    }
    else
    {
    list->tail->prev->next = list->tail;
    }
    list->size++;
}

//尾部删除
int delete_end(LIST* list)
{
    if (!empty(list))
    {

    }
    PNODE pdel= list->tail->prev;

    if (list->tail == list->head)
    {
        free(list->tail);
        list->head = list->tail = NULL;
    }
    else
    {
        free(list->tail);
        pdel->next = NULL;
        list->tail = pdel;
        list->size--;
    }
}

//显示所有数据,从头到尾
void show(LIST* list)
{
    PNODE ptemp = list->head;
    while (ptemp)
    {
        printf("%c <->", ptemp->data);
        ptemp = ptemp->next;
    }
    printf("end\n");
}

//显示所有数据,从尾到头
void show_end(LIST* list)
{

    PNODE ptemp = list->tail;
    while (ptemp)
    {
        printf("%c <->", ptemp->data);
        ptemp = ptemp->prev;
    }
    printf("end\n");
}

int main()
{
    LIST list;
    init(&list);
    for (int i = 0; i < 6; i++)
    {
        append(&list, 'B' + i);
    }
    show(&list);
    printf("\n");
    show_end(&list);
    del_n('C', &list);
    show(&list);
    getchar();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值