/*
作者@BIGPIMPIMP
时间:2017.01.23 这是一个用来实现顺序表的程序 主要实现顺序表的建立和各种操作 */ #include<stdio.h> #include<stdlib.h> //定义一个顺序表的结构体 typedef struct sqlist { int maxsize; int length; int * pHead; }sqlist; //给顺序表分配内存空间,并确定最大长度 int * createSqlist(sqlist *p) { printf("enter maxsize:\n"); scanf("%d",&(p->maxsize)); return (int *)malloc(sizeof(int)*p->maxsize); } //初始化一个顺序表,确定当前长度,并给每个元素赋值 bool intiSqlist(sqlist *p) { if (p->pHead != NULL) { printf("enter length:\n"); scanf("%d",&(p->length)); printf("enter %d numbers:", p->length); for (int i = 1; i <= p->length; i++) { scanf("%d", p->pHead + i - 1); } return true; } else return false; } //打印出顺序表 void showSqlist(sqlist *p) { printf("maxsize:%d\nlength:%d\nhead adress:%p\n",p->maxsize,p->length,p->pHead); for (int i = 1; i <= p->length; i++) { printf("%d ",*((p->pHead)+i-1)); } putchar('\n'); } //判断顺序表是否为空 bool isEmpty(sqlist *p) { if (p->pHead != NULL&&p->length > 0) return false; else return true; } //判断顺序表是否为满 bool isFull(sqlist *p) { if (p != NULL) if (p->length < p->maxsize) return false; else return true; else return false; } //按数据查找元素,并返回该元素的序号 int getLocation(sqlist *p,int e) { if (p != NULL) { for (int i = 1; i <= p->length; i++) if (*(p->pHead + i - 1) == e) { printf("no.%d element is %d\n", i, e); return i; } printf("element is not found\n"); return NULL; } else { printf("sqlist is not exist\n"); return NULL; } } //按序号查找元素,并返回该元素的值 int getElement(sqlist *p, int loc) { if (p != NULL) { if (loc >= 1 && loc <= p->length) { printf("no.%d element is %d\n",loc,*(p->pHead + loc - 1)); return *(p->pHead + loc - 1); } else { printf("element is not found\n"); return NULL; } } else { printf("sqlist is not exist\n"); return NULL; } } //插入一个元素 bool insertElement(sqlist *p, int e, int loc) { if (!isEmpty(p)) { if ((loc >= 1 && loc <= p->length + 1) && p->length < p->maxsize) { for (int i = p->length; i >= loc; i--) { *(p->pHead + i) = *(p->pHead + i - 1); } *(p->pHead + loc - 1) = e; p->length += 1; return true; } else return false; } else return false; } //删除一个元素 bool deleteElement(sqlist *p, int loc) { if (!isFull(p)) { if (loc >= 1 && loc <= p->length) { for (int i = loc; i < p->length; i++) { *(p->pHead + i - 1) = *(p->pHead + i); } p->length -= 1; return true; } else return false; } else return false; } //倒置顺序表 bool invert(sqlist *p) { if (!isEmpty(p)) { int temp; for (int i = 1; i < p->length - i+1; i++) { temp = *(p->pHead + i - 1); *(p->pHead + i - 1) = *(p->pHead + p->length - i); *(p->pHead + p->length - i) = temp; } return true; } else return false; } //排序顺序表 bool sort1(sqlist *p) { if (!isEmpty(p)) { int temp; for (int i = 1; i <= p->length; i++) { for (int j = i + 1; j <= p->length; j++) { if (*(p->pHead + i - 1) > *(p->pHead + j - 1)) { temp = *(p->pHead + i - 1); *(p->pHead + i - 1) = *(p->pHead + j - 1); *(p->pHead + j - 1) = temp; } } } return true; } else return false; }
/*
作者@BIGPIMPIMP
时间:2017.01.24 这是一个实现链表的程序 主要实现链表的建立和各种常见操作 */ #include<stdio.h> #include<stdlib.h> //定义一个单链表结点的结构体 typedef struct Node { int data; struct Node * pNext; }Node,*pNode; //定义一个单链表的结构体 typedef struct LinkList { int length; struct Node * pHead; }LinkList,*pLinkList; //利用头插法建立一个链表,并为每一个节点分配内存 bool createLinkList(pLinkList p) { if (p != NULL) { printf("enter length:\n"); scanf("%d", &(p->length)); //建立头结点 p->pHead = (pNode)malloc(sizeof(Node)); p->pHead->data = NULL; p->pHead->pNext = NULL; for (int i = 1; i <= p->length; i++) { pNode pNextNode = (pNode)malloc(sizeof(Node)); pNode temp; temp = p->pHead->pNext; p->pHead->pNext = pNextNode; pNextNode->pNext = temp; } return true; } else { printf("linklist is not exist\n"); return false; } } //判断单链表是否为空 bool isEmpty(pLinkList p) { if (p != NULL) if (p->length == 0 || p->pHead == NULL) return true; else return false; else return false; } //初始化单链表,并为每一个结点的数据域赋值 bool intiLinkList(pLinkList p) { if (p != NULL) { if (!isEmpty(p)) { pNode temp = p->pHead->pNext; printf("enter %d numbers:\n", p->length); for (int i = 1; i <= p->length; i++) { scanf("%d", &(temp->data)); temp = temp->pNext; } return true; } else { printf("empty linklist\n"); return true; } } else { printf("linklist is not exist\n"); return false; } } //打印出单链表 void showLinkList(pLinkList p) { if (p != NULL) { if (!isEmpty(p)) { printf("head address:%p\nlength:%d\n", p->pHead, p->length); pNode temp = p->pHead->pNext; for (int i = 1; i <= p->length; i++) { printf("%d ", temp->data); temp = temp->pNext; } } else printf("empty linklist\n"); } else printf("linklist is not exist\n"); } //按序号查找元素,并返回该元素的值 int getElement(pLinkList p,int loc) { if (p!=NULL) { if (!isEmpty(p)) { if (loc>=1&&loc<=p->length) { pNode temp = p->pHead; for (int i = 1; i <= loc; i++) temp = temp->pNext; return temp->data; } else { printf("element is not found\n"); return NULL; } } else { printf("empty linklist\n"); return NULL; } } else { printf("linklist is not exist\n"); return NULL; } } //按元素查找序号,并返回该元素的序号 int getLoc(pLinkList p,int e) { if (p!=NULL) { if (!isEmpty(p)) { pNode temp = p->pHead->pNext; int i = 1; while (temp->data!=e) { temp = temp->pNext; i++; } if (i >= 1 && i <= p->length) return temp->data; else { printf("element is not found\n"); return NULL; } } else { printf("empty linklist\n"); return NULL; } } else { printf("linklist is not exist\n"); return NULL; } } //利用后插法插入一个元素 bool insertElement(pLinkList p,int loc,int e) { if (p!=NULL) { if (loc >= 0 && loc <= p->length) { //从头结点遍历链表,找到第loc位元素的前驱 pNode temp = p->pHead; for (int i =0; i < loc;i++) temp = temp->pNext; pNode pElement = (pNode)malloc(sizeof(Node)); pElement->data = e; pElement->pNext = temp->pNext; temp->pNext = pElement; p->length += 1; return true; } else { printf("element can not been inserted\n"); return false; } } else { printf("linklist is not exist\n"); return false; } } //删除一个元素 bool deleteElement(pLinkList p, int loc) { if (p != NULL) { if (loc>=1&&loc<=p->length&&p->pHead!=NULL) { //从头结点遍历链表,找到待删除元素的前驱 pNode pre = p->pHead; for (int i = 1; i < loc;i++) pre = pre->pNext; pNode temp = pre; temp = pre->pNext->pNext; free(pre->pNext); pre->pNext = temp; p->length -= 1; return true; } else { printf("element can not been deleted\n"); return false; } } else { printf("linklist is not exist\n"); return false; } } //排序单链表 bool sort(pLinkList p) { if (p!=NULL) { if (!isEmpty(p)) { pNode pre = p->pHead; pNode pos; for (int i = 1; i <= p->length; i++) { pre = pre->pNext; pos = pre; for (int j = i + 1; j <= p->length; j++) { pos = pos->pNext; if (pre->data > pos->data) { int temp; temp = pre->data; pre->data = pos->data; pos->data = temp; } } } return true; } else { printf("empty linklist\n"); return true; } } else { printf("linklist is not exist\n"); return false; } }
用c语言实现顺序表与单链表的部分操作
最新推荐文章于 2024-03-21 18:28:24 发布