2.顺序表和链表常见接口实现+基本常识

 一.顺序表

1.顺序表的静态存储和动态存储

#define  N  100

struct SeqList

{

int a[N];

int  size;

};
struct SeqList
{
  int* a;
  int size;      //有效数据的个数
  int capacity;  //容量
};
typedef int SeqDateType //升级版
struct SeqList
{
  SeqDateType* a;
  int size;      //有效数据的个数
  int capacity;  //容量
};

实现增删查改

增 头查,尾插,中间插,最重要的四个顺序表

 顺序表常见接口

void SeqListPushBack(struct SeqList* pseq, SeqDateType x);//尾插
{
  if(pq->size == pq->capacity)
  {
  int newcapacity = pseq->capacity == 0 ? 4 : pseq->capacity * 2;
  SeqDateType* newA = realloc(pq->a, sizeof(SeqDateType)*newcapacity);
  if(newA == NULL)
  {
    printf("realloc fail\n");
    exit(-1);
  }
  pseq->a = newA;
  pseq->capacity = newcapacity;
  }
  assert(pseq);
  pseq->a[pseq->size] = x;
  pseq->size++;
}


void SeqListPushFront(struct SeqList* pseq, SeqDateType x);//头查
{
  assert(pseq)
  int end = pq->size -1;
  while(end >= 0)
  {
    pq->a[end + 1] = pq->a[end];
    --end;
  }
  pq->a[0] = x;
  pq->size++;
}


void SeqListPopBack(struct SeqList* pseq);//尾删
{
  assert(pseq);
  assert(pseq->size > 0);
  --pseq->size;
}


void SeqListPopFront(struct SeqList* pseq);//头删
{
  assert(pq);
  assert(pq->size>0);
  int begin = 0;
  while(begin<pq->size - 1)
  {
  pq->a[begin] = pq->a[begin+1];
  }
  pq->size--;
}


void SeqListPopPrint(struct SeqList* pseq);//打印链表
{
  assert(pseq);
  for(int i = 0; i < pq->size; ++i)
  {
    printf("%d ",pq->a[i]);
  }
  printf("\n");
}


void SeqCheckCapacity(SeqList* pseq)//扩容
{
if(pq->size == pq->capacity)
  {
  int newcapacity = pseq->capacity == 0 ? 4 : pseq->capacity * 2;
  SeqDateType* newA = realloc(pq->a, sizeof(SeqDateType)*newcapacity);
  if(newA == NULL)
  {
    printf("realloc fail\n");
    exit(-1);
  }
  pseq->a = newA;
  pseq->capacity = newcapacity;
  }
}

int SeqListFind(SeqList* pq,SeqDateType x)//查找
{
  assert(pq);
  for(int i = 1;i < pq->size;++i)
  {
    if(pq->a[i] == x)
      {
        return i;
      }
  }
  return -1;
}

void SeqListInsert(SeqList* pq, int pose, SeqDateType x)//中间插
{
  assert(pq);
  assert(pose >= 0 && pq <= pq->size);
  SeqCheckCapacity(pq);
  int end = size - 1;
  while(end >= pose)
  {
    pq->a[end + 1] = pq->a[end];
    --end;
  }
  pq->a[pose] = x;
  pq->size++;
}

void SeqListErase(SeqList* pq, int pose)//删除
{
  assert(pq);
  assert(pos >= 0 && pos < pq->size);
  int begin = pose;
  while(begin <= pq->size - 1)
  {
    pq->a[begin] = pa->a[begin+1];
    ++begin;
  }
  pq->size--;
}

void SeqListModify(SeqList* pq,int pos, SeqDateType x)//修改
{
  assert(pq);
  assert(pos >= 0&& pos < pq->size);
  pq->a[pos] = x;
}
typedef struct SeqList
{
  SeqDateType* a;
  int size;      //有效数据的个数
  int capacity;  //容量
}SeqList;
SeqList seq;
void SeqListPopInit(struct SeqList* pseq);//初始化
{
  assert(pseq);
  pseq.a = NULL;
  pseq.size = pseq.capacity = 0;
}
void SeqListPopDestory(struct SeqList* pseq);//销毁
{
  assert(pseq);
  free(pseq->a);
  pseq->a = NULL;
  pseq->size = pseq->capacity = 0;
}

二.链表

二.一.单向链表

typedef int SLTDataType
typedef struct SlistNode  //单向
{
  int data;
  struct SlistNode* next;
}SLTNode;

/*struct ListNode  //双向
{
  int data;
  struct ListNode* next;
  struct ListNode* before;
};*/

void SListPushBack(SLTNode** plist, SLTDataType x)  //尾插
{
  STLNode* newnode* newnode = BuySLTNode(x);
  if(plist == NULL)
{
  plist = newnode;
}
  else
{
 SLTNode* tail plist;
  while(tail->next != NULL)
  {
    tail = tail->next;
  }
  SListnode* newnode = BuySLTNode(x);
  tail->next = newnode; 
}
}

void SListPushFront(SLTNode** plist, SLTDataType x)//头插
{
  SLTNode* newnode = BuySLTNode(x);
  newnode->next = *pplist;
  *pplist =newnode;
}

void SListPopBack(SLTNode** pplist)//尾删
{
  if(*pplist = NULL)
    return;
  else if ((*pplist)->next = NULL)
  {
  free(*pplist);
  *pplist = NULL;
  }
  else
  {
  SLTNode* prev = NULL;
  SLTNode* tail = plist;
  while(tail->next != NULL)
  {
    prev = tail;
    tail = tail->next;
  }
  free(tail)
  tail = tail->next;
  prev->next = NULL;
  }
}

void SListPopFront(SLTNode** pplist)//头删
{
  if(pplist = NULL);
    return;
  else
  {
    SLTNode* next = (*pplist)->next;
    free(*pplist);
    *pplist = next;
  }
}

void SListPrint(SLTNode* plist)   //打印
  STList* cur = plist;
  while(cur != NULL)
  {
    printf("%d ",cur->data);
    cur = cur->next;
  }
  printf("\n");
}

void SListFind(SLTNode* plist, SLTDataType x)//查找,可以修改
{
 SLTNode*cur = plist;
  while(cur)
  {
    if(cur->data == x)
    {
      return cur;
    }
    cur = cur->next;
  }
  return NULL;
}
void SListInsert(SLTNode* pos, SLTDataType x)
{
  assert(pos);
  SLTNode* newwnode = BuySLTNode(x);
  newnode->next = pos->next =NULL;
  pos->next = newnode;
}
void SListInsertAfter(SLTNode* pos, SLTDataType x)//很麻烦

phead 指针变量  哨兵位的头节点

二.二双向链表

typedef int LTDataType;
typedef struct ListNode            //双向链表
{                                 结构复杂,操作简单
  struct ListNode* next;
  struct ListNode* prev;
  LTDataType data;
}ListNode;

ListNode* BuyListNode(LTDataType x)       //头节点
{
  ListNode* node = (ListNode*)malloc(sizeof(ListNode));
  node->next = NULL;
  node->prev = NULL;
  node->data = x;
  return node;
}


ListNode Listinit()           //
{
  ListNode* phead = BuyListNode(0);
  phead->next = phead;
  phead->prev = phead;
  return phead;
}

void ListNodePushBack(ListNode* phead, LTDataType x)  //尾插
{
  assert(phead);
  ListNode* tail = phead->prev;
  ListNode* newnode = BuyListNode(x);
  tail->next = newnode;
  newnode->prev = tail;
  newnode->next = phead;
  phead->prev = newnode;
}

void ListPrint(ListNode* phead)
{
  ListNode* cur = phead->next;
  while(cur != phead)
  {
    printf("%d ",cur->data);
    cur = cur->next;
  }
  printf("\n");
}

void ListNodePushFront(ListNode* phead, LTDataType x)
{
 assert(phead);
 ListNode* newnode = BuyListNode(x);
 ListNode* first = phead->next;
 phead->next = newnode;
 newnode->prev = phead;
 newnode->next = first;
 first->prev = newnode;
}


void ListNodePopBack(ListNode* phead)
{
  assert(phead);
  assert(phead->next != NULL);
  ListNode* tail = phead->prev;
  ListNode* tailPrev = tail->prev;
  free(tail);
  tailPrev->next = phead;
  phead->prev = tailPrev;
}


void ListNodePopFront(ListNode* phead)   //delete
{
  assert(phead);
  assert(phead->next != phead);
  ListNode* first = phead->next;
  ListNode* second = first->next;
  free(first);
  phead->next = second;
  second->prev = phead;
}

ListNode* ListFind(ListNode* phead, LTDataType x)  //查找
{
  assert(phead);
  while(cur != phead)
  {
    if(cur->data == x)
    {
      return cur;
    }
    cur = cur->next;
  }
return NULL;
}

void ListInsert(ListNode* pos, LTYDataType x)  //插入
{
  assert(pos);
  ListNode* prev = pos-> prev;
  ListNode* newnode = BuyListNode(x);
  prev->next = newnode;
  newnode->prev = prev;
  newnode->next = pos;
  pos->prev = newnode;
}

void ListErase(ListNode* pos)  //delete
{
  ListNode* prev = pos->prev;
  ListNode* next = pos->next;
  prev->next = next;
  next->prev = prev;
  free(pos);
}

  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学c的长弓狗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值