数据结构笔记-顺序表链表

    科技2022-07-15  122

    顺序表

    结构函数操作【初始化,插入,删除,值查找】 //结构—存储数组,last索引 typedef struct{ datatype data[maxsize]; int last;// last索引为数组的索引;表空时为-1; }seqlist;

    函数操作-初始化

    seqlist *CreateSeqlist() { seqlist * lq; lq->last = -1; return lq; } void main() { seqlist l; InitList(&l); } void InitList(seqlist* lq) { lq->last = -1; }

    函数操作—插入

    检查【溢出检查;插入位置是否异常】插入操作 int InsertList(seqlist* lq,int i,datatype x) // i 插入位置;x 插入数据 { int j; if(lq->last == maxsize-1) { printf(“list is full”); return (-1); } if( i<1 || i>lq->last+1)// 插入位置【1,n】数组索引为【0,n-1】 { printf(“position is illegal”); return (0); } for(j = lq->last; j <= i-1;j—){ lq->data[j+1] = lq->data[j]; } data[j-1] = x; lq->last++; return(1); }

    函数操作—删除操作

    检查删除 int DeleteList(seqlist* lq,int i)//删除第 i 个 元素 { int j; if(i<1 || i>lq->last+1) { printf(“position is invaild”); return(0); } for(j=i;j<=lq->last;j++) lq->data[j] = lq->data[j-1]; lq->last—; return(1); }

    函数操作—值查找

    int LocationSeqlist(seqlist* lq,datatype x) { int i = 0; while(i<=lq->last&&lq->data[i]!=x) i++; if(i>lq->last) return -1; else return i; }

    链表

    结构函数操作【建表(头插,尾插),删,改,添,查】

    结构

    typedef struct linknode { datatype data; struct linknode * next; }Linknode,*LinkList; // typedef struct linknode * Linklist;

    函数操作-创表

    头插;尾插 //头插法 Linknode * Create_Linklist(){ Linknode * head,*p; int x; head = NULL; while(x != -9999){ //-9999 创表的结束标志 scanf(“%d”,&x); p = new Linknode; p -> data = x; p->next = head; head = p; } return head; } //尾插法 Linknode * Create_LinkLIst2(){ Linknode * head,*rear,*s; int x = 0; head = NULL; rear = head; while(x != -1){//结束标志 //常规操作—信息录入 scanf(“%d”,&x); s = new Linknode; s ->data = x; s -> next = NULL; //插入 if(head == NULL) head = s;//当表空时,使头指针链接第一个元素 else rear -> next = s; //. rear 存储链表最后一位的地址,rear的next = 新节点地址建立连接 rear = s; // 链接上了新元素后,再使得rear存储最后一位的地址 //返回 值 return head; } }

    函数操作-求表长

    //带头节点的链表-即头指针指向头节点;头节点不存储数据;不计数 int Len_List(Linklist l){ Linknode * p = l; int length = -1; while(p){ p = p->next; length++; } return length; } //不带头节点 int len_Linklist(Linklist l){ Linknode* p = l; int length = 0; if(l == NUll) return 0;// 空表 while(p){ p=p->next; length++; } return length; }

    函数操作-查找

    // 序号查找-找第i个 Linknode * SearchList1(Linklist l,int i){ Linknode * p = l; int n = 0; while(p -> next != NULL && n <i ){ p = p -> next; n++; } if(n == i) return p; else return NUll; } // 值查找 Linknode * SearchList2(Linklist l,int data){ Linknode* p = l; while(p->next != NUll && p -> data != data){ p = p-> next; } if(p->data == data) return p; else return NULL; }

    函数操作-插入

    // 后插:插在p的后面 s = new Linknode; s->data = data; s->next = p->next; p->next = s; //前插: 插在p的前面 Linknode * q = l; while(q->next != p) q = q->next; //q为p的前驱节点 s->next = q->next; q->next = s; //插入第i个后面 void InsertList(Linklist head,int i,char x){ Linknode *s,*p; p = head; int j = 0; while(p != NULL && j < i){ p = p -> next; j++; } if(p != NULL) { s = new Linknode; s->data = x; s->next = p->next; p->next = s; } else print(“fail”); }

    函数操作-删除

    //删除节点p;q为p的前驱 q->next = p->next; delete(p); //删除值为x的节点 //q为p的前驱,遍历时为q的哨兵; Linknode* DeleteList(Linklist head,char x){ Linknode *p,*q; if(head == NULL){ printf(“list is empty.”) return NULL; } if(head -> next == NULL){ if(head->data != x) { printf(“not fount”); return head; } else { delete head; return NULL; } q = head; p = q -> next; while(p != NUll && p->data != x){ q=p; p=p->next; } if(p != NULL){ q->next = p->next; delete p; } else pirntf(“not found”); } }

    循环链表

    双向链表

    c++的模板库调用

    还不会

    c++的文档

    https://doc.bccnsoft.com/docs/cppreference/index.html

    https://www.runoob.com/cplusplus/cpp-standard-library.html

    待看: https://blog.csdn.net/yas12345678/article/details/52601578

    #include <iostream> #include <list> using namespace std;
    Processed: 0.013, SQL: 8