线性表代码实现
1.顺序表代码实现(c语言)
#include <stdio.h>
#include <assert.h>
#include<errno.h>
#include <stdlib.h>
#include<string.h>
typedef int SLDateType;
typedef struct SeqList
{
SLDateType* a;
size_t size;
size_t capacity;
}SeqList;
void SeqListInit(SeqList* ps);
void SeqListDestroy(SeqList* ps);
void SeqListPrint(SeqList* ps);
void SeqListPushBack(SeqList* ps, SLDateType x);
void SeqListPushFront(SeqList* ps, SLDateType x);
void SeqListPopFront(SeqList* ps);
void SeqListPopBack(SeqList* ps);
int SeqListFind(SeqList* ps, SLDateType x);
void SeqListInsert(SeqList* ps, size_t pos, SLDateType x);
void SeqListErase(SeqList* ps, size_t pos);
void CheckCapacity(SeqList* ps);
void SeqListInit(SeqList* ps)
{
printf("please input number of capacity");
scanf("%d", &ps->capacity);
ps->a = (SLDateType *)malloc(sizeof(SLDateType) * ps->capacity);
ps->size = 0;
return;
}
void SeqListDestroy(SeqList* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->size = 0;
ps->capacity = 0;
}
void SeqListPrint(SeqList* ps)
{
assert(ps);
int i;
for (i = 0; i < ps->size; ++i)
printf("%d ", ps->a[i]);
printf("\n");
}
void SeqListPushBack(SeqList* ps, SLDateType x)
{
CheckCapacity(ps);
ps->a[ps->size]=x;
ps->size++;
return;
}
void SeqListPushFront(SeqList* ps, SLDateType x)
{
CheckCapacity(ps);
for (int i = ps->size-1; i >=0; --i)
ps->a[i+1] = ps->a[i];
ps->a[0] = x;
ps->size++;
return;
}
void SeqListPopFront(SeqList* ps)
{
if (ps->size <= 0)
return;
for (int i = 0; i < ps->size-1; ++i)
ps->a[i] = ps->a[i + 1];
ps->size--;
}
void SeqListPopBack(SeqList* ps)
{
if (ps->size <= 0)
return;
ps->size--;
}
int SeqListFind(SeqList* ps, SLDateType x)
{
if (ps->size <= 0)
{
printf("此表为空\n");
return -1;
}
for (int i = 0; i < ps->size; ++i)
if (ps->a[i] == x)
return i;
printf("数据未在表中\n");
return -1;
}
void SeqListInsert(SeqList* ps, size_t pos, SLDateType x)
{
CheckCapacity(ps);
if (ps->size < pos)
{
printf("插入位置不符合要求\n");
return;
}
for (int i = ps->size-1; i >=pos-1; --i)
ps->a[i + 1] = ps->a[i];
ps->a[pos - 1] = x;
ps->size++;
}
void SeqListErase(SeqList* ps, size_t pos)
{
if (ps->size <= 0||ps->size<=pos)
{
printf("此表为空\n");
return;
}
for (int i = pos - 1; i < ps->size-1; ++i)
ps->a[i] = ps->a[i + 1];
}
void CheckCapacity(SeqList* ps)
{
if (ps->capacity == ps->size)
{
SLDateType* new_a = (SLDateType*)realloc(ps->a, (2 * ps->capacity) * sizeof(SLDateType));
if (new_a != NULL)
{
ps->a = new_a;
ps->capacity *= 2;
return;
}
else
{
printf("%s\n", strerror(errno));
}
}
else
{
return;
}
}
int main()
{
SeqList SL;
SeqListInit(&SL);
SeqListPushBack(&SL,4);
SeqListPushBack(&SL, 5);
SeqListPushBack(&SL, 6);
SeqListPushBack(&SL, 7);
SeqListPushBack(&SL, 8);
SeqListPushFront(&SL, 3);
SeqListPushFront(&SL, 2);
SeqListPushFront(&SL, 1);
SeqListPrint(&SL);
SeqListPopFront(&SL);
SeqListPrint(&SL);
SeqListPopBack(&SL);
SeqListPrint(&SL);
SeqListFind(&SL,4);
SeqListInsert(&SL,4,9);
SeqListErase(&SL,5);
SeqListPrint(&SL);
return 0;
}
2.链表代码实现
单链表(C语言)
#include<stdio.h>
#include<assert.h>
#include<malloc.h>
typedef int SLTDateType;
typedef struct SListNode
{
SLTDateType data;
struct SListNode* next;
}SListNode;
SListNode* BuySListNode(SLTDateType x);
void SListPrint(SListNode* plist);
void SListPushBack(SListNode** pplist, SLTDateType x);
void SListPushFront(SListNode** pplist, SLTDateType x);
void SListPopBack(SListNode** pplist);
void SListPopFront(SListNode** pplist);
SListNode* SListFind(SListNode* plist, SLTDateType x);
void SListInsertAfter(SListNode* pos, SLTDateType x);
void SListEraseAfter(SListNode* pos);
void SListDestroy(SListNode** pplist);
SListNode* BuySListNode(SLTDateType x)
{
SListNode* new_node = (SListNode*)malloc(sizeof(SListNode));
new_node->data = x;
new_node->next = NULL;
return new_node;
}
void SListPrint(SListNode* plist)
{
if (plist == NULL)
{
printf("该链表为空\n");
return;
}
while (plist != NULL)
{
printf("%d ", plist->data);
plist = plist->next;
}
printf("\n");
}
void SListPushBack(SListNode** pplist, SLTDateType x)
{
assert(pplist);
if (*pplist == NULL)
{
*pplist = BuySListNode(x);
return;
}
SListNode* p = *pplist;
while (p->next != NULL)
{
p = p->next;
}
p->next = BuySListNode(x);
}
void SListPushFront(SListNode** pplist, SLTDateType x)
{
assert(pplist);
if (*pplist == NULL)
{
*pplist = BuySListNode(x);
return;
}
SListNode* new_node = BuySListNode(x);
new_node->next = *pplist;
*pplist = new_node;
}
void SListPopBack(SListNode** pplist)
{
assert(pplist != NULL);
if (*pplist == NULL)
return;
SListNode* p = *pplist;
SListNode* temp = NULL;
while (p->next != NULL)
{
temp = p;
p = p->next;
}
temp->next = NULL;
free(p);
}
void SListPopFront(SListNode** pplist)
{
assert(pplist != NULL);
if (*pplist == NULL)
return;
SListNode* p = (*pplist)->next;
free(*pplist);
*pplist = p;
}
SListNode* SListFind(SListNode* plist, SLTDateType x)
{
if (plist == NULL)
{
printf("该链表为空表\n");
return plist;
}
SListNode* p = plist;
while (p == NULL || p->data == x)
p = p->next;
if (p->data == x)
return p;
else
{
printf("未找到\n");
return plist;
}
}
void SListInsertAfter(SListNode* pos, SLTDateType x)
{
if (pos == NULL)
{
pos->next = BuySListNode(x);
return;
}
SListNode* new_node = BuySListNode(x);
new_node->next = pos->next;
pos->next = new_node;
}
void SListEraseAfter(SListNode* pos)
{
if (pos == NULL)
return;
SListNode* p = pos->next;
pos->next = p->next;
free(p);
}
void SListDestroy(SListNode** pplist)
{
assert(pplist);
if (*pplist == NULL)
return;
SListNode* p=*pplist;
while (*pplist != NULL)
{
p = p->next;
free(*pplist);
*pplist = p;
}
}
int main()
{
SListNode* p = NULL;
SListPushBack(&p, 1);
SListPushBack(&p, 2);
SListPushBack(&p, 3);
SListPushBack(&p, 4);
SListPushBack(&p, 5);
SListPushBack(&p, 6);
SListPushBack(&p, 7);
SListPrint(p);
SListPushFront(&p,0);
SListPopBack(&p);
SListPrint(p);
SListPopFront(&p);
SListPrint(p);
SListNode* p1=SListFind(p, 5);
SListInsertAfter(p1, 9);
SListPrint(p);
SListEraseAfter(p1);
SListPrint(p);
SListDestroy(&p);
SListPrint(p);
return 0;
}
双向链表(c++)
#include<iostream>
#include<assert.h>
using namespace std;
typedef int LTDataType;
typedef struct ListNode
{
LTDataType _data;
struct ListNode* _next;
struct ListNode* _prev;
}ListNode;
ListNode* ListCreate();
void ListDestory(ListNode* pHead);
void ListPrint(ListNode* pHead);
void ListPushBack(ListNode* pHead, LTDataType x);
void ListPopBack(ListNode* pHead);
void ListPushFront(ListNode* pHead, LTDataType x);
void ListPopFront(ListNode* pHead);
ListNode* ListFind(ListNode* pHead, LTDataType x);
void ListInsert(ListNode* pos, LTDataType x);
void ListErase(ListNode* pos);
ListNode* ListCreate()
{
ListNode* new_node = (ListNode*)malloc(sizeof(ListNode));
if (new_node == NULL)
{
assert(0);
return NULL;
}
new_node->_data = 0;
new_node->_next = NULL;
new_node->_prev = NULL;
return new_node;
}
void ListDestory(ListNode* pHead)
{
assert(pHead);
ListNode* p = pHead->_next;
while (p)
{
ListNode* temp = p->_next;
free(p);
p = temp;
}
}
void ListPrint(ListNode* pHead)
{
assert(pHead);
ListNode* p = pHead->_next;
while (p)
{
cout << p->_data << " ";
p = p->_next;
}
cout << endl;
}
void ListPushBack(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListNode* p = pHead->_next;
ListNode* new_node = (ListNode*)malloc(sizeof(ListNode));
if (new_node == NULL)
{
assert(0);
return;
}
new_node->_data = x;
new_node->_next = NULL;
new_node->_prev = NULL;
if (p == NULL)
{
pHead->_next = new_node;
new_node->_prev = pHead;
return;
}
while (p->_next)
p = p->_next;
p->_next = new_node;
new_node->_prev = p;
}
void ListPopBack(ListNode* pHead)
{
assert(pHead);
ListNode* p = pHead->_next;
if (p == NULL)
{
cout << "the list is empty" << endl;
return;
}
while (p)
p = p->_next;
free(p);
}
void ListPushFront(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListNode* new_node = (ListNode*)malloc(sizeof(ListNode));
if (new_node == NULL)
{
assert(0);
return;
}
new_node->_data = x;
new_node->_next = NULL;
new_node->_prev = NULL;
new_node->_next = pHead->_next;
pHead->_next->_prev = new_node;
pHead->_next = new_node;
new_node->_prev = pHead;
}
void ListPopFront(ListNode* pHead)
{
assert(pHead);
if (pHead->_next == NULL)
{
cout << "the list is empty" << endl;
return;
}
ListNode* p = pHead->_next;
pHead->_next = p->_next;
p->_next->_prev = pHead;
free(p);
}
ListNode* ListFind(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListNode* p = pHead->_next;
if (p == NULL)
{
cout << "the list is empty" << endl;
return NULL;
}
while (p)
{
if (p->_data == x)
return p;
p = p->_next;
}
cout << "no the data in the list" << endl;
}
void ListInsert(ListNode* pos, LTDataType x)
{
assert(pos);
ListNode* new_node = (ListNode*)malloc(sizeof(ListNode));
if (new_node == NULL)
{
assert(0);
return;
}
new_node->_data = x;
new_node->_next = NULL;
new_node->_prev = NULL;
new_node->_prev = pos->_prev;
new_node->_next = pos;
pos->_prev->_next = new_node;
pos->_prev = new_node;
}
void ListErase(ListNode* pos)
{
assert(pos);
pos->_prev->_next = pos->_next;
pos->_next->_prev = pos->_prev;
}
void test_list()
{
ListNode* head = ListCreate();
ListPushBack(head, 1);
ListPushBack(head, 2);
ListPushBack(head, 3);
ListPrint(head);
ListPushFront(head, 4);
ListPushFront(head, 5);
ListPrint(head);
ListPopFront(head);
ListPrint(head);
}
int main()
{
test_list();
}