189 8069 5689

数据结构-线性表-创新互联

成都创新互联是专业的石台网站建设公司,石台接单;提供成都做网站、成都网站设计、成都外贸网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行石台网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

顺序表 静态顺序表

SqList.c

//
// Created by jimeng on 2022/12/27.
//

#include#include// 静态分配
#define MaxSize 50
typedef struct {
    int data[MaxSize];
    int length;
}SqList;


// 初始化静态顺序表
SqList* InitSqList() {
    SqList* list = (SqList*) malloc(sizeof(SqList));
    list->length = 0;
    return list;
}

// 静态顺序表插入数据
int SqListInsert(SqList * sqList, int i, int e) {
    if(i< 1 || i >sqList->length+1) {
        return 0;
    }
    if(sqList->length >= MaxSize) {
        return 0;
    }
    for(int j = sqList->length; j>=i;j--) {
        sqList->data[j] = sqList->data[j-1];
    }
    sqList->data[i-1] = e;
    sqList->length++;
    return 1;
}

// 静态顺序表打印数据
void SqListPrint(SqList* sqList) {
    int length = sqList->length;
    printf("length: %d\n", length);
    printf("elements: { ");
    for (int i = 0; i< length; ++i) {
        printf("%d ", sqList->data[i]);
    }
    printf("}\n");
}

// 静态顺序表删除操作
int SqListDelete(SqList* sqList, int i, int* e) {
    if(i<1||i>sqList->length) {
        return 0;
    }
    *e = sqList->data[i-1];
    for(int j = i;jlength;j++) {
        sqList->data[i-1] = sqList->data[i];
    }
    sqList->length--;
    return 1;
}

int SqListLocateElem(SqList* sqList, int e) {
    int i;
    for(i = 0;ilength; i++) {
        if(sqList->data[0] == e) {
            return i+1;
            break;
        }
    }
    return 0;
}

main.c

#include#include "SqList/SqList.c"

int main() {

    printf("静态顺序表\n");
    SqList* sqList = SqListInit();
    SqListInsert(sqList, 1, 1);
    SqListPrint(sqList);
    SqListInsert(sqList, 2, 2);
    SqListPrint(sqList);
    int deldata = 0;
    SqListDelete(sqList, 1, &deldata);
    SqListPrint(sqList);
    int index = 0;
    index = SqListLocateElem(sqList, 2);
    printf("2 ->%d\n", index);
    printf("动态顺序表\n");

    return 0;
}
动态顺序表
// 动态分配
#define InitSize 100
typedef struct {
    int * data;
    int ListMaxSize, length;
}SeqList;

// 动态顺序表初始化
SeqList* SeqListInit() {
    SeqList* list = (SeqList*) malloc(sizeof(SeqList));
    list->data = (int*) malloc(sizeof(int)*InitSize);
    list->ListMaxSize = InitSize;
    list->length = 0;
    return list;
}

// 动态顺序表插入数据
int SeqListInsert(SeqList* list, int i, int e) {
    if(i<1 || i>list->length) {
        return 0;
    }
    if(list->length >= list->ListMaxSize) {
        int* newArray = (int*) malloc(sizeof(int)*(list->ListMaxSize+InitSize));
        int j;
        for(j = 0; jlength;j++) {
            newArray[j] = list->data[j];
        }
        newArray[j] = e;
        list->data = newArray;
        list->ListMaxSize +=InitSize;
    }
    for(int j = list->length; j>=i;j--) {
        list->data[j] = list->data[j-1];
    }
    list->data[i-1] = e;
    list->length++;
    return 1;
}
链表 单链表

LinkList.c

//
// Created by jihuaixi on 22-12-27.
//
#include#includetypedef struct LNode {
    int data;
    struct LNode* next;
}LNode, *LinkList;

// 头插法建立单链表
LinkList List_HeadInsert(LinkList list) {
    LNode* s;
    int x;
    list = (LinkList) malloc(sizeof(LNode));
    list->next = NULL;
    scanf("%d", &x);
    while (x != 9999) {
        s = (LNode*) malloc(sizeof(LNode));
        s->data = x;
        s->next = list->next;
        list->next = s;
        scanf("%d", &x);
    }
    return list;
}

// 尾插法建立单链表
LinkList List_TailInsert(LinkList list) {
    int x;
    list = (LinkList) malloc(sizeof(LNode));
    LNode *s, *r = list;
    scanf("%d", &x);
    while (x != 9999) {
        s = (LNode*) malloc(sizeof(LNode));
        s->data = x;
        r->next = s;
        r = s;
        scanf("%d", &x);
    }
    r->next = NULL;
    return list;
}

// 打印链表的值
void List_Print(LinkList list) {
    LNode* s = list->next;
    printf("{ ");
    while (s != NULL) {
        printf("%d ", s->data);
        s = s->next;
    }
    printf("}\n");
}

// 按序号查找值
LNode* getElem(LinkList list, int i) {
    int j = 1;
    LNode *p = list->next;
    if(i == 0) {
        return list;
    }
    if(i< 1) {
        return NULL;
    }
    while (p&&jnext;
        j++;
    }
    return p;
}

// 按值查找节点
LNode* getElemByValue(LinkList list, int e) {
    LNode *p = list->next;
    while (p!=NULL&&p->data!=e) {
        p = p->next;
    }
    return p;
}

// 获取表长
int List_GetLength(LinkList list) {
    int length = 0;
    LNode *p = list;
    while (p->next != NULL) {
        p = p->next;
        length++;
    }
    return length;
}

// 插入节点
int List_Insert(LinkList list, int i, int e) {
    int length = List_GetLength(list);
    if(i< 1 || i >length) {
        return 0;
    }
    LNode *priv = getElem(list, i-1);
    LNode *p = (LNode*) malloc(sizeof(LNode));
    p->data = e;
    p->next = priv->next;
    priv->next = p;
    return 1;
}

// 删除节点
int List_Delete(LinkList list, int i) {
    int length = List_GetLength(list);
    if(i< 1 || i >length) {
        return 0;
    }
    LNode *priv = getElem(list, i-1);
    LNode *q = priv->next;
    priv->next = priv->next->next;
    free(q);
    return 1;
}

main.c

#include#include "LinkList/LinkList.c"

int main() {
    printf("单链表\n");
    LinkList list;
//    list = List_HeadInsert(list);
    list = List_TailInsert(list);
    List_Print(list);
    LNode *e = getElem(list, 2);
    printf("2 ->value: %d\n", e->data);
    LNode *e2 = getElemByValue(list, 3);
    printf("3 ->index: %d\n", e2->data);
    List_Insert(list, 2, 10);
    List_Print(list);
    List_Delete(list, 3);
    List_Print(list);
    int length = List_GetLength(list);
    printf("length: %d\n", length);
    return 0;
}
双链表

DLinkList.c

//
// Created by jihuaixi on 22-12-27.
//

typedef int ElemType;

typedef struct DNode {
    ElemType data;
    struct DNode *prior, *next;
}DNode, *DLinkList;

//创建双链表
DLinkList DLinkList_Creat(DLinkList list) {
    ElemType x;
    DNode *p = (DNode*) malloc(sizeof(DNode)); // 头结点
    list = p;
    scanf("%d", &x);
    DNode *q;
    while (x!=9999) {
        q = (DNode*) malloc(sizeof(DNode));
        q->prior = p;
        q->data = x;
        p->next = q;
        p = q;
        scanf("%d", &x);
    }
    q->next = NULL;
    return list;
}

//打印双链表
void DLinkList_Print(DLinkList list) {
    printf("{ ");
    DNode *p = list->next;
    while (p) {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("}\n");
}

//获取双链表长度
int DLinkList_GetLength(DLinkList list) {
    int length = 0;
    DNode *p = list->next;
    while (p) {
        p = p->next;
        length++;
    }
    return length;
}

//插入双链表元素
int DLinkList_Insert(DLinkList list, int i, ElemType e) {
    if(i< 1 || i >DLinkList_GetLength(list)) {
        return 0;
    }
    DNode *p = list->next;
    int j = 1;
    while (p!=NULL && jnext;
        j++;
    }
    DNode *q = (DNode*) malloc(sizeof(DNode));
    q->data = e;
    q->next = p->next;
    q->prior = p;
    p->next = q;
    q->next->prior = q;
    return 1;
}

//删除双链表元素
int DLinkList_Delete(DLinkList list, int i) {
    if(i< 1 || i >DLinkList_GetLength(list)) {
        return 0;
    }
    DNode *p = list->next;
    int j = 1;
    while (p!=NULL && jnext;
        j++;
    }
    DNode *q = p->next;
    p->next = q->next;
    q->next->prior = p;
    free(q);
    return 1;
}

main.c

#include#include "DLinkList/DLinkList.c"

int main() {

    printf("双链表: \n");
    DLinkList list;
    list = DLinkList_Creat(list);
    DLinkList_Print(list);
    DLinkList_Insert(list, 2, 999);
    DLinkList_Print(list);
    DLinkList_Delete(list, 3);
    DLinkList_Print(list);
    return 0;
}
循环单链表

尾节点的next指向头结点

循环双链表

头结点的prior指向尾节点

尾节点的next指向头结点

静态链表
//
// Created by jihuaixi on 22-12-28.
//
#define MaxSize 50
typedef struct {
    int data;
    int next;
}SLinkList[MaxSize];

next=-1作为结束标志

对于不支持指针的编程语言中,可采用静态链表的方式。

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


本文名称:数据结构-线性表-创新互联
文章源于:http://cdxtjz.cn/article/hhgse.html

其他资讯