数据结构 线性表的指针实现

 
 
 
 

顺序表示即就是数组,其特点为:
优:(1)用数组存储数据元素,操作方法简单,容易实现
    (2)无须为表示结点间的逻辑关系而增加额外的存储开销
    (3)存储密度高
    (4)顺序表可按元素位序随机存取结点
缺:(1)做插入、删除操作时,需大量移动数据元素,效率非常低
    (2)要占用连续的存储空间,存储分配只能预先进行。分配过大,会导致空间浪费;分配过小将会造成数据溢出。

线性表的指针实现
是利用指针把元素中的各个元素依次连接起来,形成一个单向的链表.这样做的优点是不需要连续的存储单元存放各个元素。
因此在插入新的元素时候不需要移动其他元素为新的元素流出空位,删除表中的元素时,也不需要移动其他元素填补空位。


在初始化单链表时有两种方法,头插法和尾插法。

头插法


尾插法




#include <stdio.h>
#include <malloc.h>
 
 
typedef int ElemType;
typedef struct LNODE *LNode;
 
 
struct LNODE
{
    ElemType data;   //存放的数据
    LNode *next;     //指向下个节点的指针
};
 
 
 
 
void InitPointList(LNode *L){
    (*L) = (LNode *)malloc(sizeof(LNode));
    (*L)->next = NULL;
}
//头插法
void CreateListInHead(LNode *L,int x){
    LNode p;
    p = (LNode *)malloc(sizeof(LNode));
    p->data = x;
    p->next = (*L)->next;
    (*L)->next = p;
}
//尾插法
void CreateListInTail(LNode *L,int x){
    LNode p;
    p = (LNode *)malloc(sizeof(LNode));
    p->data = x;
    p->next = NULL;
 
 
    //获取最后一个元素
    LNode temp;
    temp = (*L);
    if((*L)->next == NULL){
        (*L)->next = p;
    }
    else{
        while(temp->next !=NULL){
            temp = temp->next;
        }
        temp->next = p;
    }
}
 
 
void insert(LNode *L,int x,int pos){
    LNode p;
    p = (LNode *)malloc(sizeof(LNode));
    p->data = x;
 
 
    LNode temp;
    temp = (*L);
    if (temp->next==NULL){
        (*L)->next = p;
        p->next =NULL;
    }
    else{
        for (int i = 1;i<pos;i++)
            temp = temp->next;
 
 
        p->next = temp->next;
        temp->next = p;
    }
}
void deletelist(LNode *L,int pos){
 
 
 
 
    LNode temp;
    temp = (*L);
    if (temp->next==NULL){
        printf("the list is null");
    }
    int j = 0;
    while(temp->next && j< pos - 1){
        j++;
        temp =temp->next;
    }
    if (temp->next == NULL){
        printf("the pos > list length");
        return;
    }
 
 
    LNode pn = temp->next;
    temp->next = pn->next;
    pn =NULL;
    free(pn);
  }
 
 
void ListPrint(LNode *L){
    LNode p;
    p = (*L)->next;
    while(p!=NULL){
        printf("List data = %d \n",p->data);
        p = p->next;
    }
}
 
 
void DestoryList(LNode *L){
    LNode p;
    while(*L){
        p = (*L)->next;
        (*L) = NULL;
        free(*L);
        (*L) = p;
    }
}
 
 
void clearList(LNode *L){
    LNode p,q;
    p = (*L)->next;
    while(p)
    {
      q = p->next;
      p = NULL;
      free(p);
      p = q;
    }
    (*L)->next = NULL;
}
 
 
int main()
{
    LNode L;
    InitPointList(&L);
    int a[]= {1,2,3,4,5};
    for(int i =0;i<5;i++){
        //CreateListInHead(&L,a[i]);
        CreateListInTail(&L,a[i]);
    }
 
 
    insert(&L,88,3);
    ListPrint(&L);
    deletelist(&L,3);
    ListPrint(&L);
    //DestoryList(&L);
    clearList(&L);
    if(L==NULL)
        printf("the List L is NULl");
    else
        printf("the list is not null");
    return 0;
}


注意 释放内存前 先将节点置为NULL;否则将报错  CRT detected that the application wrote to memory after end of heap buffer.
所以建议是 1、内存申请多少释放多少,释放掉你申请过的内存,不要乱释放;2、不能释放已经释放的内存;

 
 

猜你喜欢

转载自blog.csdn.net/weixin_31607113/article/details/79961250
今日推荐