# 数据结构算法实现(线性结构)

## 单链表

```*************************************************************************
> File Name: list.c
> Author: 孟超
> Mail: 2205101365@qq.com
> Created Time: 2019年09月03日 星期二 23时39分13秒
************************************************************************/

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

typedef struct _Node
{
int data;
struct _Node * next;
}Node;

Node *createList()
{

}

//插入操作
void insertList(Node* head , int data)
{
Node *cur = (Node*)malloc(sizeof(Node));
cur->data = data;
}

//遍历
{
{
}
}

{
int count = 0;
{
count++;
}
return count;
}

{
{
{
break;
}
}
}

//删除节点
void deleteList(Node *head , Node *pfind)
{
{
}
head -> next = pfind -> next;
free(pfind);
}

//排序
{
Node *p,*q;
for(int i = 0;i< len-1;i++)
{
q = p -> next;
for(int j = 0;j<len-1-i;j++)
{
if(p->data > q->data)
{
p->data ^= q->data;
q->data ^= p->data;
p->data ^= q->data;
}
p = p->next;
q = p->next;
}
}

}

//逆转链表，思想就是将链表拆分
{
Node *h = head -> next;
Node *t;
while(h)
{
t = h->next;
h = t;
}
}

//销毁链表
{
Node *t;
{
}

}

int main()
{
printf("%d\n",count);
return 0;
}```

## 线性栈

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

typedef struct  stack
{
int len;
int top;
char *_space;
}stack;

void initStack(stack *s , int size)
{
s->len = size;
s->top = 0;
s->_space = (char *)malloc(s->len);
}

int isEmpty(stack *s)
{
return s->top == 0;
}

int isFull(stack *s)
{
return s->top == s->len;
}

void push(stack *s,char ch)
{
s->_space[s->top++] = ch;
}

char pop(stack *s)
{
return s->_space[--s->top];
}

void resetStack(stack *s)
{
s->top = 0;
}

void clearStack(stack *s)
{
free(s->_space);
}

int main()
{
stack s;
initStack(&s,27);
for(char i = 'A'; i != 'Z'+1; ++i)
{
push(&s,i);
}
while(!isEmpty(&s))
{
char c = pop(&s);
printf("%c\n",c);
}
return 0;
}```

## 链栈

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

typedef struct Node
{
char data;
struct Node *next;
}Node;

typedef struct stack
{
Node *top;
}stack;

void initStack(stack *s)
{
s->top = (Node*)malloc(sizeof (Node));
s->top->next = NULL;
}

int isEmpty(stack *s)
{
return s->top->next == NULL;
}

void push(stack *s,char ch)
{
Node *cur = (Node*)malloc(sizeof (Node));
cur->data = ch;

cur->next = s->top->next;
s->top->next = cur;
}

char pop(stack*s)
{
Node *t = s->top->next;
char ch = t->data;
s->top->next = t->next;
free(t);
return ch;
}

int main()
{
stack s;
initStack(&s);
for(char i = 'A'; i != 'Z'+1; ++i)
{
push(&s,i);
}
while(!isEmpty(&s))
{
char c = pop(&s);
printf("%c\n",c);
}
return 0;
}```

## 环形对列

```#include <stdio.h>
#include<stdlib.h>
//front最开始指向空白待插入位置，rear指向有内容的待出队的位置
typedef struct _Queue
{
char *_space;
int _len;
int _rear;
int _front;
}Queue;

void initQueue(Queue *q,int size)
{
q->_len = size;
q->_space = (char *)malloc(sizeof(q->_len));
q->_rear = q->_front = 0;
}

int isEmpty(Queue *q)
{
return q->_front ==  q->_rear;
}

int isFull(Queue *q)
{
return (q->_rear +1)%q->_len == q->_front;
}

void enQueue(Queue *q,char ch)
{
q->_space[q->_rear] =ch;
q->_rear = ++q->_rear %q->_len;
}

char deQueue(Queue *q)
{
char ch = q->_space[q->_front];
q->_front = ++q->_front %q->_len;
return ch;
}

int main()
{
Queue q;
initQueue(&q,27);
for(char i = 'A'; i < 'Z'+1; ++i)
{
enQueue(&q,i);
}
while(!isEmpty(&q))
{
char c = deQueue(&q);
printf("%c\n",c);
}
return 0;
}```

## 链队列

```#include <stdio.h>
#include <stdlib.h>
struct QNode
{
int data;
struct QNode * next;
};
//链式存储中， 只放两个头尾指针
struct Queue
{
struct QNode *front;
struct QNode *rear;
};

void initQueue(struct Queue * q)
{
q->rear = q->front =
(struct QNode*)malloc(sizeof(struct QNode));
q->rear->next = NULL;
}
int isQueueEmpty(struct Queue * q)
{
return q->rear == q->front;
}
void enQueue(struct Queue * q,int dat)
{
struct QNode * cur =
(struct QNode *)malloc(sizeof(struct QNode));
cur->data = dat;
cur->next = q->rear->next;
q->rear->next = cur;
q->rear = cur;
}
int deQueue(struct Queue * q)
{
if(q->front->next == q->rear)
{
//只有一个元素
int t = q->front->next->data;
q->front->next = q->front->next->next;
free(q->rear);
q->rear = q->front;
return t;
}
else
{
//有多个元素
int t = q->front->next->data;
struct QNode* pdel = q->front->next;
q->front->next = q->front->next->next;
free(pdel);
return t;
}
}
void clearQueue(struct Queue *q)
{
struct QNode * t = q->front;
struct QNode * cur;
while(t)
{
cur = t->next;
free(t);
t = cur;
}
q->front = q->rear = NULL;
}
int main()
{
struct Queue q;
initQueue(&q);
for(int i=0; i<10; i++)
{
enQueue(&q,i);
}
while(!isQueueEmpty(&q))
{
printf("%d\n",deQueue(&q));
}
clearQueue(&q);
return 0;
}```

0 条评论

• ### 数据结构与算法面试题

自实现strcmp、自实现strcpy等 https://blog.csdn.net/z_ryan/article/details/79250265

• ### Linux内核配置编译及启动过程分析

Linux内核并不能被用户直接使用，发行版才可以。Linux主要的工作是内存管理，进程调度等等，发行版加上了桌面和各种可用的工具，才能被用户使用。

• ### 学Java-Spring使用Quartz任务调度定时器

睁开眼看一看窗外的阳光，伸一个懒腰，拿起放在床一旁的水白开水，甜甜的味道，晃着尾巴东张西望的猫猫，在窗台上舞蹈。你向生活微笑，生活也向你微笑。 请你不要询问我...

• ### LeetCode 234：回文链表 Palindrome Linked List

Given a singly linked list, determine if it is a palindrome.

• ### LeetCode 234：回文链表 Palindrome Linked List

Given a singly linked list, determine if it is a palindrome.

• ### 链表逆序

1.构造5个节点a,b,c,d,e,并对它们进行初始化； 2.将a,b,c,d,e,5个节点连接在一起

• ### 摄像机公网全终端无插件直播安放视频流媒体服务器EasyNVS如何查看调用录像？

EasyNVS云管理平台是新一代的云上架构，基于创新的超融合和技术构建， 具备完整的视频流媒体服务能力和运维管理服务能力的云架构平台，可将分布在不同区域和网络环...

• ### Open vSwitch系列之openflow版本兼容

众所周知Open vSwitch支持的openflow版本从1.0到1.5版本（当前Open vSwitch版本是2.3.2）通过阅读代码，处理openflow...

• ### Leetcode 876. Middle of the Linked List

版权声明：博客文章都是作者辛苦整理的，转载请注明出处，谢谢！ https://blog.csdn....