数据结构——顺序表,单链表,双向链表的实现及全部代码

线性表的顺序表示和实现


#include

#define MaxSize 100

typedef  int ElemType;
typedef  bool Status;

typedef struct{//定义结构体
    ElemType *elem;
    int length;
}SqList;

Status InitList(SqList &L){//顺序表初始化
    L.elem=new ElemType[MaxSize];
    if(!L.elem) {
        printf("顺序表初始化失败\n");
        return false;
    }
    L.length=0;
    printf("顺序表初始化成功\n");
    return true;
}

Status ListInsert(SqList &L,int i,ElemType e){//顺序表的插入
    if(i<1||i>L.length+1) {
        printf("插入失败,原因是插入位置不合法\n");
        return false;
    }
    if(i==MaxSize){
        printf("插入失败,原因是存储空间已经满了\n");
        return false;
    }
    for(int j=L.length-1;j>=i;j--)L.elem[j+1]=L.elem[j];
    L.elem[i-1]=e;
    ++L.length;
    printf("在第%d个位置插入的值为%d\n",i,e);
    return true;

}

Status GetElem(SqList L,int i,ElemType &e){//顺序表的取值
    if(i<1||i>L.length) return false;
    e=L.elem[i-1];
    return true;
}


int LocateElem(SqList L,ElemType e){//顺序表的查找
    for(int i=0;iL.length){
        printf("需要删除的索引位置不合法\n");
        return false;
    }
    for(int j=i-1;j

线性表的链式表示和实现

#include 
#include 

typedef struct ListNode {//定义结构体
    int data;
    struct ListNode* next;
} ListNode;

ListNode* InitList() {//初始化结点
    ListNode* node = (ListNode*)malloc(sizeof(ListNode));//ListNode* node = new ListNode;
    node->next = NULL;
    return node;
}

void InsertList(ListNode* node, int index, int value) {//插入结点
    ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
    newNode->data = value;

    ListNode* p = node;
    int i = 0;
    while (i < index && p->next != NULL) {
        i++;
        p = p->next;
    }

    newNode->next = p->next;
    p->next = newNode;
}

int GetListLength(ListNode* node) {//求表长
    int length = 0;
    ListNode* p = node->next;  // Skip the dummy head node
    while (p != NULL) {
        length++;
        p = p->next;
    }
    return length;
}

int GetNodeValueByIndex(ListNode* node, int index) {//按索引求值
    ListNode *p = node->next;  // Skip the dummy head node
    int i = 0;
    while (i < index && p != NULL) {
        i++;
        p = p->next;
    }

    if (p != NULL) {
        return p->data;
    } else {
        // Handle invalid index
        printf("Invalid index\n");
        return -1;  // Or any other value to indicate an error
    }
}

int GetNodeIndexByValue(ListNode* node, int value) {//按值求索引
    ListNode *p = node->next;  // Skip the dummy head node
    int i = 1;
    while (p != NULL) {
        if(p->data==value){
            return i;
        }
        i++;
        p = p->next;
    }

    if (p == NULL) {
        return -1;
    }
}

void DeleteNode(ListNode *node,int index){//删除结点
    int i=0;
    ListNode *p= node;
    if(index<1||index> GetListLength(node)){
        printf("Failed to delete node\n");
    } else{
        while(p&&inext;
            i++;
        }
        ListNode *q=p->next;//要删除的节点
        p->next=q->next;
        free(q);
        printf("Successfully deleted node\n");
    }

}

void PrintNode(ListNode *node){//打印链表
    ListNode *p=node->next;
    printf("Start printing nodes ");

    while (p){
        printf("%d ",p->data);
        p=p->next;
    }
    printf("\n");
}

int main() {
    ListNode* node = InitList();
    InsertList(node, 0, 1);
    InsertList(node, 1, 2);
    InsertList(node, 2, 3);
    InsertList(node, 3, 4);
    InsertList(node, 4, 5);
    InsertList(node, 5, 6);

    int length = GetListLength(node);
    printf("List length: %d\n", length);

    int index = 1;
    int value = GetNodeValueByIndex(node, index);
    if (value != -1) {
        printf("Value at index %d: %d\n", index, value);
    }

    printf("Value at index %d: %d\n", GetNodeIndexByValue(node,10), 10);
    printf("Value at index %d: %d\n", GetNodeIndexByValue(node,3), 3);

    PrintNode(node);

    DeleteNode(node,2);
    printf("List length: %d\n", GetListLength(node));
    // Free allocated memory

    PrintNode(node);
}

拓展——双向链表


#include 
#include 

// 定义链表节点结构
struct Node {
    int data;           // 数据域
    struct Node* prev;  // 指向前一个节点的指针
    struct Node* next;  // 指向下一个节点的指针
};

// 初始化链表
void initList(struct Node** head) {
    *head = NULL;   // 头指针置空
}

// 创建新节点
struct Node* createNode(int element) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = element;
    newNode->prev = NULL;
    newNode->next = NULL;
    return newNode;
}

// 在指定位置插入节点
void insertAtPosition(struct Node** head, int element, int position) {
    struct Node* newNode = createNode(element);

    // 链表为空时,新节点为头节点
    if (*head == NULL) {
        *head = newNode;
    } else {
        struct Node* current = *head;
        int count = 1;

        // 找到要插入位置的前一个节点
        while (current->next != NULL && count < position - 1) {
            current = current->next;
            count++;
        }

        // 连接新节点
        newNode->next = current->next;
        newNode->prev = current;
        if (current->next != NULL) {
            current->next->prev = newNode;
        }
        current->next = newNode;
    }
}

// 删除指定位置的节点
void deleteAtPosition(struct Node** head, int position) {
    if (*head == NULL) {
        return;  // 链表为空,无需删除
    }

    struct Node* current = *head;
    int count = 1;

    // 找到要删除的节点
    while (current != NULL && count < position) {
        current = current->next;
        count++;
    }

    // 如果找到了要删除的节点
    if (current != NULL) {
        if (current->prev != NULL) {
            current->prev->next = current->next;
        } else {
            *head = current->next;
        }
        if (current->next != NULL) {
            current->next->prev = current->prev;
        }
        free(current);
    }
}

// 打印链表中的元素
void printList(struct Node* head) {
    printf("List: ");
    struct Node* current = head;

    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }

    printf("\n");
}

// 释放链表的内存
void freeList(struct Node* head) {
    struct Node* current = head;
    while (current != NULL) {
        struct Node* temp = current;
        current = current->next;
        free(temp);
    }
}

int main() {
    struct Node* head;
    initList(&head);

    // 插入元素
    insertAtPosition(&head, 5, 1);
    insertAtPosition(&head, 10, 2);
    insertAtPosition(&head, 15, 2);

    // 打印链表
    printList(head);

    // 删除元素
    deleteAtPosition(&head, 2);

    // 打印链表
    printList(head);

    // 释放链表内存
    freeList(head);

    return 0;
}


 

你可能感兴趣的:(数据结构,c语言,算法)