链表,顺序表,链队,链栈,顺序队,顺序栈,二叉树的基本操作函数


链表的基本操作:

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

typedef struct node{
    int data;
    struct node *next;
}list,*L;


L creat(void)
{
    L l1 = malloc(sizeof(list));
    l1->data = -1;
    l1->next = NULL;
    return l1;
}


int getlen(L l1)
{
    int i = 0;
    L h = l1->next;
    while(h != NULL)
    {
        i++;
        h = h->next;
    }
    return i;
}


void show(L l1)
{
    L h = l1->next;
    while( h != NULL)
    {
        printf("%d,",h->data);
        h = h->next;
    }
    printf("\n");
}


int dellist(L l1,int pos)
{
    L h = l1;
    int len = getlen(l1);
    if(h->next == NULL)
    {
        printf("list is empty\n");
        return -1;
    }


    if(pos < 0 || pos > len-1)
    {
        printf("pos error\n");
        return -1;
    }


    while(pos--)
    {
        h = h->next;
    }
    
    L d = h->next;
    h->next = d->next;
    free(d);
    return 0;
}


int insert(L l1,int pos,int val)
{
    L h = l1;
    int len = getlen(l1);
    if(pos < 0 || pos > len)
    {
        printf("pos error\n");
        return -1;
    }
    while(pos--)
    {
        h = h->next;
    }
    L s = malloc(sizeof(list));
    s->data = val;
    s->next = h->next;
    h->next = s;
    return 0;
}


int changlist(L l1,int pos,int val)
{
    L h = l1->next;
    int len = getlen(l1);
    if(pos < 0 || pos > len-1)
    {
        printf("pos error\n");
        return -1;
    }
    while(pos--)
    {
        h = h->next;
    }
    h->data = val;
    return 0;
}


int search(L l1,int pos)
{
    L h = l1->next;
    int len = getlen(l1);
    if(pos < 0 || pos > len-1)
    {
        printf("pos err\n");
        return -1;
    }
    int i;
    while(pos--)
    {
        h = h->next;
    }
    printf("find %d\n",h->data);
    return h->data;
}


int main()
{
    L l1 = creat();
    show(l1);
    insert(l1,0,1);
    insert(l1,1,11);
    show(l1);
    insert(l1,2,22);
    insert(l1,3,33);
    show(l1);
    dellist(l1,2);
    show(l1);
    search(l1,1);
    return 0;
}





顺序表的基本操作:

#include<stdio.h>
#include<stdlib.h>
#define MAX 100
typedef struct Snode{
    int a[MAX];
    int len;
}snode,*sqlist;


sqlist creat(void)
{
    sqlist list = (sqlist)malloc(sizeof(snode));
    if(list == NULL)
    {
        printf("creat error\n");
        return NULL;
    }


    list->len = 0;
    return list;
}

int listisfull(sqlist l)
{
    return (l->len == MAX ? 1 : 0);
}


int getlen(sqlist l)
{
    return l->len;
}

int listisempty(sqlist l)
{
    if(l->len == 0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int insert(sqlist l,int pos ,int val)
{
    int len = l->len;
    if(len == MAX)
    {
        return -1;
    }
    if(pos < 0 || pos > len)
    {
        printf("pos error");
        return -1;
    }
    int i;
    for(i = len -1; i>=pos;i--)
    {
        l->a[i+1] = l->a[i];
    }
    l->a[pos] = val;
    l->len++;
    return 0;
}

int ChangeList(sqlist L,int pos,int val)
{
    int len = L->len;
    if(pos<0||pos>len-1){
        printf("pos error\n");
        return -1;
    }
    L->a[pos] = val;
    return 0;
}

int SearchList(sqlist L,int pos)
{
    int len = L->len;
    if(pos<0||pos>len-1){
        printf("pos error\n");
        return -1;
    }
    printf("find :%d\n",L->a[pos]);


    return L->a[pos];
    
}
int SearchListByVal(sqlist L,int val)
{
    int len = L->len;
    int i;
    for(i=0;i<len;i++){
        if(L->a[i]==val){
            printf("find pos:%d,val:%d\n",i,L->a[i]);
            return 0;
        }
    }
    
    printf("find nothing\n");
    return -1;
}
int ClearList(sqlist L)
{
    L->len=0;
}

void ShowList(sqlist L)
{
    int len = L->len;
    int i;
    for(i=0;i<len;i++){
        printf("%d,",L->a[i]);
    }
    printf("\n");
}

int DelList(sqlist L,int pos)
{
    int len = L->len;
    if(len==0){
        printf("list is empty\n");
        return -1;
    }
    if(pos<0||pos>len-1){
        printf("pos error\n");
        return -1;
    }

    int i;
    for(i=pos;i<len;i++){
        L->a[i]=L->a[i+1];
    }

    L->len--;
    return 0;
}

int main()
{
    return 0;
}



二叉树:

#include<stdio.h>
typedef struct node{
    int data;
    struct node *lchild;
    struct node *rchild;
}bt,*bitree;

bitree creat(void)
{
    bitree t = NULL;
    char c;
    scanf("%c",&c);
    if(c != '#')
    {
        t = malloc(sizeof(bt));
        t->data = c;
        t->lchild = creat();
        t->rchild = creat();
    }
    return t;
}

void qianxu(bitree t)
{
    if(t == NULL)
    {
        printf("the bitree is empty");
        return;
    }
    printf("%c,",t->data);
    qianxu(t->lchild);
    qianxu(t->rchild);
}
void zhongxu(bitree t)
{
    if(t == NULL)
    {
        printf("the bitree is empty");
        return;
    }
    qianxu(t->lchild);
    printf("%c,",t->data);
    qianxu(t->rchild);
}
void qianxu(bitree t)
{
    if(t == NULL)
    {
        printf("the bitree is empty");
        return;
    }
    qianxu(t->lchild);
    qianxu(t->rchild);
    printf("%c,",t->data);
}



typedef int data_t
struct node{
    data_t data;
    struct node *next;
};


链队:

linkqueue.h:

typedef int data_t
struct node{
    data_t data;
    struct node *next;
};

typedef struct node Lnode;
typedef struct node *linklist;

//将头尾指针包在一起
struct qnode{   
    linklist front;
    linklist rear;
};
typedef struct qnode Lqnode;
typedef struct qnode * linkqueue;

linkqueue CreateQueue();             //创建空队
int EmptyQueue(linkqueue LQ);        //判空
int ClearQueue(linkqueue Q);         //清空队
int GetLenth();                      //获取队列长度
int EnQueue(linkqueue LQ,data_t val);//入队
data_t DeQueue(linkqueue LQ);        //出队
void DestroyQueue();                 //销毁

linkqueue.c:

#include <stdio.h>
#include <stdlib.h>
#include "linkqueue.h"

int EnQueue(linkqueue LQ,int val)
{
    if(EmptyQueue(LQ)){

        LQ->rear=LQ->front;
        //注意:如果是空队,注意修改尾指针指向

    }

    linklist s = malloc(sizeof(Lnode));  //生成一个新的节点
    s->data = val;                       

    s->next = NULL;                      //插入到末尾
    LQ->rear->next = s;

    LQ->rear = s;                        //修改尾指针
    return 0;

}
int EmptyQueue(linkqueue LQ)
{
    //没有有效数据,只剩头节点
    if(LQ->front->next==NULL){
        return 1;
    }
    else {
        return 0;
    }
}

int DeQueue(linkqueue LQ)
{
    if(EmptyQueue(LQ)){
        printf("queue is empty\n");
        return -1;
    }
    

    linklist d = LQ->front->next;  //记录删除节点位置
    int val = d->data;             //取值
    
    LQ->front->next  = d->next;    //删除
    free(d);                       //释放
    
    return val;
}
测试用的main.c

#include <stdio.h>
#include "linkqueue.h"
#include <stdlib.h>

int main(void)
{

    linklist h = malloc(sizeof(Lnode));
    linklist s1= malloc(sizeof(Lnode));
    linklist s2= malloc(sizeof(Lnode));
    linklist s3= malloc(sizeof(Lnode));

    h->data = -1;
    s1->data = 11;
    s2->data = 22;
    s3->data = 33;

    h->next = s1; s1->next=s2; s2->next=s3; s3->next=NULL;
    

    linkqueue lq = malloc(sizeof(Lqnode));
    lq->front = h;
    lq->rear = s3;

    while(!EmptyQueue(lq)){
        printf("%d\n",DeQueue(lq));
    }

    EnQueue(lq,100);
    EnQueue(lq,200);
    EnQueue(lq,300);

    while(!EmptyQueue(lq)){
        printf("%d\n",DeQueue(lq));
    }

    return 0;
}


顺序队:

squeue.h:

#ifndef _SQUEUE_H
#define _SQUEUE_H

#define MAX 10

typedef int data_t;
struct node{
    data_t a[MAX];
    int front;
    int rear;
};
typedef struct node Sqnode;
typedef struct node * squeue;

squeue CreateQueue(void);                //创建
int EmptyQueue(squeue SQ);               //判空
int FullQueue(squeue SQ);                //判满
int EnQueue(squeue SQ,data_t val);       //入队
data_t DeQueue(squeue SQ);               //出队

#endif

squeue.c

#include <stdio.h>
#include <stdlib.h>
#include "squeue.h"

squeue CreateQueue(void)
{
    squeue sq = malloc(sizeof(Sqnode));
    sq->front=sq->rear=0;

    return sq;
}

int EmptyQueue(squeue SQ)
{
    return (SQ->rear==SQ->front?1:0);
}

int FullQueue(squeue SQ)
{
    int rear  = SQ->rear;
    int front = SQ->front;

    if( (rear+1)%MAX == front ){
        return 1;
    }
    else{
        return 0;
    }
}
int EnQueue(squeue SQ,int val)
{
    if(FullQueue(SQ)){
        printf("queue is full\n");
        return -1;
    }
    SQ->rear=SQ->rear%MAX;

    SQ->a[SQ->rear] = val;

    SQ->rear++;

    return 0;
}
int DeQueue(squeue SQ)
{
    if(EmptyQueue(SQ)){
        printf("queue is empty\n");
        return -1;
    }
    
    SQ->front = SQ->front%MAX;
    int val = SQ->a[SQ->front];
    SQ->front++;
    return val;
}
main.c

#include <stdio.h>
#include "squeue.h"
int main(void)
{
    squeue SQ = CreateQueue();

    EnQueue(SQ,11);
    EnQueue(SQ,22);
    EnQueue(SQ,33);

    while(!EmptyQueue(SQ)){
        printf("%d\n",DeQueue(SQ));
    }

    return 0;
}

链栈:

linkstack.h

#ifndef _LINKSTACK_H
#define _LINKSTACK_H


struct node{
    int data;
    struct node *next;
};
typedef struct node Lnode;
typedef struct node *linkstack;

linkstack CreateStack(void);           //创建
int GetStackLenth(linkstack S);        //栈的元素个数
int GetTop(linkstack S);               //取栈顶
int ClearStack(linkstack S);           //清空栈
int StackEmpty(linkstack S);           //判空
int PopStack(linkstack S);             //出栈
int PushStack(linkstack S,int val);    //入栈
void DestroyStack();                   //销毁

#endif
linkstack.c
#include <stdio.h>
#include <stdlib.h>
#include "linkstack.h"
//创建一个空栈
linkstack CreateStack(void)
{
    linkstack top = malloc(sizeof(Lnode));
    top->data = -1;
    top->next = NULL;

    return top;
}

int PushStack(linkstack S,int val)
{
    //新节点
    linkstack x = malloc(sizeof(Lnode));
    x->data = val;

    //插入(先链后断)
    x->next = S->next;
    S->next = x;

    return 0;
}

int StackEmpty(linkstack S)
{
    if(S->next==NULL){
        return 1;
    }
    else {
        return 0;
    }
}


int PopStack(linkstack S)
{
    if(StackEmpty(S)){
        printf("stack is empty\n");
        return -1;
    }

    linkstack d = S->next;  //记录删除位置
    int val = d->data;      //取值
    
    S->next = d->next;      //删除 

    free(d);                //释放

    return val;
}
main.c

#include <stdio.h>
#include "linkstack.h"

int main(void)
{
    linkstack mystack = CreateStack();
    
    PushStack(mystack,123);
    PushStack(mystack,456);
    PushStack(mystack,500);


    while(!StackEmpty(mystack)){
        printf("%d\n",PopStack(mystack));
    }
    
    PushStack(mystack,100);
    PushStack(mystack,200);
    PushStack(mystack,300);
    
    while(!StackEmpty(mystack)){
        printf("%d\n",PopStack(mystack));
    }
    return 0;
}


顺序栈:

sstack.h

typedef int data_t;
struct snode{
    data_t a[MAX];
    int top; //记录栈顶元素下标
};
typedef struct snode Snode;
typedef struct snode * stack;

stack CreateStack(void);          //创建空栈
int StackIsFull(stack S);         //判空
int PushStack(stack S,data_t val);//压栈
int StackIsEmpty(stack S);        //判满
data_t PopStack(stack S);         //弹栈

/*自己实现*/
int ClearStack(stack S);        //清空栈
int GetTop(stack S);            //获取栈顶
int GetStackLenth(stack S);     //获取栈的元素个数
int ShowStack(stack S);         //打印栈
int DestroyStack(stack S);      //销毁栈

#endif

stack.c

#include <stdio.h>
#include <stdlib.h>
#include "sstack.h"
stack CreateStack(void)
{
    stack  s = malloc(sizeof(Snode));
    s->top = -1;  //创建空栈

    return s;
}

int StackIsFull(stack S)
{
    if (S->top == MAX-1){
        return 1;
    }
    else {
        return 0;
    }
}

int PushStack(stack S,int val)
{
    if(StackIsFull(S)){
        printf("stack is full\n");
        return -1;
    }
    S->top++;
    S->a[S->top] = val;

}

int StackIsEmpty(stack S)
{
    if(S->top==-1){
        return 1;
    }
    else {
        return 0;
    }
}

int PopStack(stack S)
{
    if(StackIsEmpty(S)){
        printf("stack is empty\n");
        return -1;
    }

    int val = S->a[S->top];
    S->top--;
    return val;
}
int ClearStack(stack S)
{
    S->top=-1;
}
int GetTop(stack S)
{
    if(StackIsEmpty(S)){
        printf("stack is empty\n");
        return -1;
    }
    return S->a[S->top];
}
int GetStackLenth(stack S)
{
    return (S->top+1);
}
int ShowStack(stack S)
{
    int i;
    int len = GetStackLenth(S);

    for(i=0;i<len;i++){
        printf("%d,",S->a[i]);
    }
    printf("\n");
}
int DestroyStack(stack S)
{
    free(S);
}


main.c

#include <stdio.h>
#include <stdlib.h>
#include "sstack.h"

int main(void)
{

    stack mystack = CreateStack();

#if 0
    int n;
    scanf("%d",&n);
    
    while(n){

        PushStack(mystack,n%2);
        n = n/2;
    }

    while(!StackIsEmpty(mystack)){
        printf("%d",PopStack(mystack));
    }
    printf("\n");
#endif 

    PushStack(mystack,11);
    PushStack(mystack,22);
    PushStack(mystack,33);

    printf("%d\n",GetTop(mystack));
    printf("%d\n",GetStackLenth(mystack));

    ClearStack(mystack);

    ShowStack(mystack);

    /*
    while(!StackIsEmpty(mystack)){
        printf("pop:%d\n",PopStack(mystack));
    }
    */
    return 0;
}


双向链表的基本操作;

 //链表的结构
  5 typedef struct doubleLink
  6 {
  7     int data;
  8     struct doubleLink *pre;
  9     struct doubleLink *next;
 10 }dnode;
 11 
 12 //创建链表
 13 dnode* Creatdlink()
 14 {
 15     dnode* head;
 16     dnode* p;
 17     dnode* pnext;
 18     int temp;
 19     head = (dnode*)malloc(sizeof(dnode));
 20     head->pre = NULL;
 21     head->next = NULL;
 22     p = head;
 23     printf("请输入数据:");
 24     while (scanf("%d", &temp)!=EOF)
 25     {
 26         pnext = (dnode*)malloc(sizeof(dnode));
 27         pnext->data = temp;
 28         p->next = pnext;
 29         pnext->pre = p;
 30         pnext->next = NULL;
 31         p = pnext;
 32     }
 33     return head;
 34 }
 35 
 36 //插入结点
 37 dnode* insertdnode(dnode* head, int pos, int i)
 38 {
 39     int temp;
 40     dnode* prev;
 41     dnode* p;
 42     prev = head;
 43     for (temp = 0; temp < pos - 1; temp++)
 44         prev = prev->next;
 45     p = (dnode*)malloc(sizeof(dnode));
 46     p->data = i;
 47     p->pre = prev;
 48     p->next = prev->next;                                                                                     
 49     prev->next = p;
 50     prev->next->pre = p;
 51     return head;
 52 }
 53 
 54 //删除结点
 55 dnode* deletednode(dnode* head, int i)
 56 {
 57     dnode* p;
 58     p = head;
 59     while (p != NULL)
 60     {
 61         if (p->data == i)
 62         {
 63             p->pre->next = p->next;
 64             p->next->pre = p->pre;
 65             free(p);
 66             return head;
 67         }
 68         p = p->next;
 69     }
 70     printf("data is not found\n");
 71     return head;
 72 }

链表反向

void reverse(test* head)

{

test* pe = head;

test* ps = head->next;

while(ps)

{

pe->next = ps->next;

ps->next = head; 

head = ps; 

ps = pe->next;

}

}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值