#include<stdio.h> #include<malloc.h> #define TRUE 1 #define FALSE 0 #define ERROR -1 typedef int ElementType; typedef struct node { ElementType data; struct node *next; }linkNode, *linkList; linkList createHeadLinkList(void);//头插法建立单链表 linkList createRearLinkList(void);//尾插法建立单链表 int lengthLinkList(linkList s);//求单链表的表长 linkList getIndex(linkList H, int k);//单链表中按序号查找 linkList getValue(linkList H, ElementType x);//按值查找 int insertLinkList(linkList H, int i, ElementType x);//单链表的插入 int deleteLinkList(linkList H, int i);//删除 void reverseLinkList(linkList H);//倒置 void reverseLinkList(linkList H)//倒置 { linkNode *p, *q; p = H->next; H->next = NULL; while (p) { q = p; p = p->next; q->next = H->next; H->next = q; } } int deleteLinkList(linkList H, int i)//删除 { linkList p, q; p = getIndex(H, i - 1); if (p == NULL) { printf("第i-1个节点不存在"); return FALSE; } else { if (p->next == NULL) { printf("第i-1个节点不存在"); return ERROR; } else { q = p->next; p->next = q->next; free(q); return TRUE; } } } int insertLinkList(linkList H, int i, ElementType x)//求单链表的插入 { linkNode *p, *s; p = getIndex(H, i - 1); if (p == NULL) { printf("插入位置i有错"); return FALSE; } else { s = (linkList)malloc(sizeof(linkNode)); s->data = x; s->next = p->next; p->next = s; return TRUE; } } linkList getValue(linkList H, ElementType x)//按值查找 { linkNode *p = H->next; while (p != NULL && p->data != x) p = p->next; return p; } linkList getIndex(linkList H, int k)//单链表中按序号查找 { linkNode *p = H; int j = 0; while (p->next != NULL && j < k) { p = p->next++; j++; } if (j == k) return p; else return NULL; } int lengthLinkList(linkList H)//表长 { linkNode *p = H; int j = 0; while (p->next != NULL) { p = p->next; j++; } return j; } linkList createRearLinkList(void)//尾插法建立单链表 { linkList H = (linkList)malloc(sizeof(linkNode)); H->next = NULL; linkNode *s, *r = H; int x; printf("请输入数据(-1结束输入)"); scanf("%d", &x); while (x != -1) { s = (linkList)malloc(sizeof(linkNode)); s->data = x; r->next = s; r = s; scanf("%d", &x); } r->next = NULL; return H; } linkList createHeadLinkList(void)//头插法建立单链表 { linkList H = (linkList)malloc(sizeof(linkNode)); H->next = NULL; linkNode *s; int x; printf("请输入数据(-1结束输入)"); scanf("%d", &x); while (x != -1) { s = (linkList)malloc(sizeof(linkNode)); s->data = x; s->next = H->next; H->next = s; scanf("%d", &x); } return H; }
顺序表基本操作
1 #include<stdio.h> 2 #include<malloc.h> 3 4 #define MAXSIZE 30 5 #define OVERFLOW -1 6 #define TRUE 1 7 #define FALSE 0 8 #define ERROR -2 9 10 typedef int ElemType; 11 12 typedef struct 13 { 14 ElemType elem[MAXSIZE]; 15 int length; 16 }seqList; 17 18 void initSeqList(seqList *L)//顺序表的初始化 19 { 20 L->length = 0; 21 } 22 23 int insertSeqList(seqList *L, int i, ElemType x)//插入 24 { 25 int j; 26 27 if (L->length == MAXSIZE - 1) 28 { 29 printf("表已满"); 30 return OVERFLOW; 31 } 32 if (i < 1 || i > L->length + 1) 33 { 34 printf("插入位置错误!"); 35 return FALSE; 36 } 37 for (j = L->length; j >= i; j--) 38 L->elem[j + 1] = L->elem[j]; // 将i之后的元素都向后移动一位 39 L->elem[i] = x; 40 L->length++; 41 return TRUE; 42 } 43 44 int deleteSeqList(seqList *L, int i)//删除 45 { 46 int j; 47 48 if (i < 1 || i > L->length) 49 { 50 printf("不存在第i个元素"); 51 return ERROR; 52 } 53 54 for (j = 1; j < L->length-1; j++) 55 L->elem[j] = L->elem[j + 1]; 56 L->length--; 57 return TRUE; 58 } 59 60 int searchValueSeqList(seqList *L, ElemType x)//顺序表中按值查找 61 { 62 int i = 1; 63 while (i <= L->length && L->elem[i] != x) 64 i++; 65 if (i > L->length) 66 return FALSE; 67 else 68 return i; 69 } 70 71 int searchIndexSeqList(seqList *L, int i)//按位置查找 72 { 73 if (i < 0 && i > L->length) 74 return FALSE; 75 else 76 return L->elem[i]; 77 }