大学三年一直在断断续续的学习数组结构,从来没有系统的总结过,这次下定决心好好的总结一下!
循规蹈矩先从线性表写
每一种数据结构都有两种存储结构,即顺序存储结构和链式存储结构。
数序存储结构顾名思义,数据在计算机内部存储是顺序存储。顺序存储的优点是,可以提供数据的随机访问,缺点是插入元素时需要移动大量数据元素。
下面是 c代码实现
首先定义一个结构体
1 #define int DATATYPE 2 #define MAXSIZE 100 3 4 typedef struct{ 5 DATATYPE data[MAXLEN]; 6 int len; 7 }SEQUENLIST;
下面是完整代码
#include <stdio.h> #include <stdlib.h> #define DATATYPE1 int #define MAXSIZE 100 typedef struct{ DATATYPE1 data[MAXSIZE]; int len; }SEQUENLIST; void init(SEQUENLIST *l){ l->len = 0; } int length(SEQUENLIST *l){ return l->len; } DATATYPE1 get(SEQUENLIST *l,int i){ if (i < 1 || i >l->len) return NULL; else return l->data[i - 1]; } int locate(SEQUENLIST *l,DATATYPE1 x){ int k = 1; while (k <= l->len && l->data[k - 1] != x) ++k; if (k < l->len) return k; else return -1; } void insert(SEQUENLIST *l,int i,DATATYPE1 x){ if (i < 1 || i > l->len + 1 || l->len == MAXSIZE) printf("error"); else { int k; for (k = l->len;k >= i;k--) l->data[k] = l->data[k-1]; l->data[i-1] = x; l->len++; } } void delete(SEQUENLIST *l,int i){ if (i < 1 || i > l->len || l->len == 0) printf("error"); else { int k = i + 1; while (k++ <= l->len) l->data[k - 2] = l->data[k - 1]; l->len--; } } int isEmptey(SEQUENLIST *l){ if (l->len == 0) return 1; else return 0; } int main(int argc, const char * argv[]) { SEQUENLIST *l; SEQUENLIST L; l = &L; init(l); int n; //插入元素数量 DATATYPE1 dt; scanf("%d",&n); int i = n; while (i-- > 0 && (scanf("%d",&dt) == 1)) { insert(l, 1,dt); //在表头插入数据 } printf("%d ",length(l)); for (int i = l->len;i >= 1;i--) printf("%d",l->data[i - 1]); delete(l, 2); printf(" "); for (int i = l->len;i >= 1;i--) printf("%d",l->data[i - 1]); }
线性表的链式存储特点是一组任意的存储单元,可以是顺序的也可以不是不连续的,存储线性表的数据元素,这里引入节点的概念,一个节点表示一个数据元素,每个节点除了要存储数据元素的信息,还要存储指向下一节点的指针。
代码实现
typedef struct node { DATATYPE data; struct node *next; } LINKLIST;
#include <stdio.h> #include <stdlib.h> #define DATATYPE2 char typedef struct node{ DATATYPE2 data; struct node *next; }LINKLIST; LINKLIST *init(){ LINKLIST *head; head = (LINKLIST *) malloc(sizeof(LINKLIST)); head->next = NULL; return head; } LINKLIST *rcreate() { LINKLIST *head,*last,*p; head = (LINKLIST *) malloc(sizeof(LINKLIST)); head->next = NULL; last = head; char ch; while ((ch = getchar()) != ' ') { p = (LINKLIST *) malloc(sizeof(LINKLIST)); p->data = ch; last->next = p; last = p; p->next = NULL; } return head; } LINKLIST *hecreate() { LINKLIST *head,*p; head = (LINKLIST *) malloc(sizeof(LINKLIST)); head->next = NULL; char ch; while ((ch = getchar()) != ' ') { p = (LINKLIST *) malloc(sizeof(LINKLIST)); p->data = ch; p->next = head->next; head->next = p; } return head; } int length(LINKLIST *head) { int i; LINKLIST *p; p = head->next; while (p != NULL) { p = p->next; ++i; } return i; } LINKLIST *get(LINKLIST *head,int i) //按序号查找 { int j; LINKLIST *p; p = head; j = 0; while (j < i && p) { p = p->next; j++; } return p; } LINKLIST *locate(LINKLIST *head,DATATYPE2 x) { LINKLIST *P; P = head->next; while ( P && P->data != x ) { P = P->next; } return P; } void insert(LINKLIST *head,int i,DATATYPE2 x) { LINKLIST *h,*p; h = (LINKLIST *) malloc(sizeof(LINKLIST)); h->data = x; h->next = NULL; p = get(head,i-1); if (p != NULL) { h->next = p->next; p->next = h; } else { printf("insert fail"); } } void delete(LINKLIST *head,int i) { LINKLIST *p,*q; p = get(head, i-1); if (p != NULL && p->next != NULL) { q = p->next; p->next = q->next; free(q); } else { printf("delete fail"); } } int isEmpty(LINKLIST *head) { if (head->next == NULL) return 1; else return 0; } int main() { LINKLIST *head,*p,*q,*r; head = init(); head = rcreate(); p = head->next; printf("创建的链表:"); while (p != NULL) { printf("%c",p->data); if (p->next != NULL) printf("->"); p = p->next; } printf(" "); //插入测试 char ch; while ((ch = getchar()) != ' ') insert(head, 2, ch); q = head->next; printf("插入后的链表:"); for (;q != NULL;q = q->next) { printf("%c",q->data); if (q->next != NULL) printf("->"); } printf(" "); //删除测试 int i; scanf("%d",&i); delete(head, i); r = head->next; printf("删除后的元素"); for (;r != NULL; r = r->next) { printf("%c",r->data); if (r->next != NULL) printf("->"); } exit(0); }
除了单链表还有循环链表,和双向循环链表,
//循环链表 typedef struct node { DATATYPE data; struct node *next,*rear; //尾指针指向头结点 }
//双向链表 typedef struct node { DATATYPE data; struct node *next,*pi; }