赞
踩
头文件:
- #ifndef _SEQLIST_H_
-
- #define _SEQLIST_H_
-
-
-
- #include<stdio.h>
-
- #include<malloc.h>
-
- #include<assert.h>
-
-
-
- #define SEQLIST_INIT_SIZE 8
-
- typedef int ElemType;
-
- typedef struct SeqList
-
- {
-
- ElemType *base; //开辟空间,数据表开辟的连续空间
-
- int capacity; //容量,最大长度
-
- int size; //表的长度,实际长度
-
- }SeqList;
-
-
-
- void InitSeqList(SeqList *list);
-
- void push_back(SeqList *list,ElemType x);
-
- void show_list(SeqList *list);
-
- void push_front(SeqList *list,ElemType x);
-
- void pop_back(SeqList *list);
-
- void pop_front(SeqList *list);
-
- void insert_pos(SeqList *list,ElemType x,int pos);
-
- int find(SeqList *list,ElemType key);
-
- int length(SeqList *list);
-
- void delete_pos(SeqList *list,int pos);
-
- void delete_val(SeqList *list,ElemType x);
-
- void sort(SeqList *list);
-
- void reserve(SeqList *list);
-
- void clear(SeqList *list);
-
- void destroy(SeqList *list);
-
-
-
-
- #endif //_SEQLIST_H_
具体方法实现(顺序表创建回收,增删改查等操作):
- #include"SeqList.h"
-
- #include <stdbool.h>
-
- /*
- *线性表初始化
- */
-
- void InitSeqList(SeqList *list)
-
- {
-
- list->base = (ElemType *)malloc(sizeof(ElemType) * SEQLIST_INIT_SIZE); //开辟连续空间
-
- assert(list->base != NULL);
-
- list->capacity = SEQLIST_INIT_SIZE;
-
- list->size = 0;
-
- }
-
- /*
- *尾插法(插入数据必须要判断表是否为满)
- */
-
- void push_back(SeqList *list, ElemType x)
-
- {
-
- if(list->base == NULL)
-
- {
-
- printf("list is not exist!\n");
-
- return;
-
- }
-
- if (list->size >= list->capacity)
-
- {
-
- printf("list full!\n");
-
- return;
-
- }
-
- list->base[list->size] = x;
-
- list->size++;
-
- }
-
-
-
- void show_list(SeqList *list)
-
- {
-
- int i = 0;
-
- if(list->base == NULL)
-
- {
-
- printf("list is not exist!\n");
-
- return;
-
- }
-
- if(list->size == 0||list->base == NULL)
-
- {
-
- printf("list empty!");
-
- return;
-
- }
-
- for(i=0;i<list->size;i++)
-
- {
-
- printf("%d ",list->base[i]);
-
- }
-
- printf("\n");
-
-
-
- }
-
- /*
- *头插法(插入数据必须要判断表是否为满)
- */
-
- void push_front(SeqList *list,ElemType x)
-
- {
-
- int i = 0;
-
- if(list->base == NULL)
-
- {
-
- printf("list is not exist!\n");
-
- return;
-
- }
-
- if(list->size >= list->capacity)
-
- {
-
- printf("list full!");
-
- return;
-
- }
-
- for(i=list->size;i>0;i--)
-
- {
-
- list->base[i] = list->base[i-1];
-
- }
-
- list->base[0] = x;
-
- list->size++;
-
- }
-
-
-
- /*
- *尾删法(删除数据必须要判断表是否为空)
- */
-
- void pop_back(SeqList *list)
-
- {
-
- if(list->base == NULL)
-
- {
-
- printf("list is not exist!\n");
-
- return;
-
- }
-
- if(list->size <= 0)
-
- {
-
- printf("list empty!");
-
- return;
-
- }
-
- list->size--;
-
- }
-
- /*
- *头删法(删除数据必须要判断表是否为空)
- */
-
- void pop_front(SeqList *list)
-
- {
-
- int i = 0;
-
- if(list->base == NULL)
-
- {
-
- printf("list is not exist!\n");
-
- return;
-
- }
-
- if(list->size <= 0)
-
- {
-
- printf("list empty!");
-
- return;
-
- }
-
- printf("%d ",list[0]);
-
- for(i=0;i<list->size-1;i++)
-
- {
-
- list->base[i]=list->base[i+1];
-
- }
-
- list->size--;
-
- }
-
- /*
- *在指定位置pos插入元素x
- */
-
- void insert_pos(SeqList *list,ElemType x ,int pos)
-
- {
-
- int i = 0;
-
- if(list->base == NULL)
-
- {
-
- printf("list is not exist!\n");
-
- return;
-
- }
-
- if(list->size >= list->capacity)
-
- {
-
- printf("list full!");
-
- return;
-
- }
-
- if(pos <= 0 || pos > list->capacity)
-
- {
-
- printf("pos is illegal!");
-
- return;
-
- }
-
- if(pos == 1)
-
- {
-
- /*list->base[0] = x;
- return;*/
-
- push_front(list,x); //当插入位置为第一位时,可直接用头插法插入数据
-
- }
-
- else if(pos == list->size+1)
-
- {
-
- /*list->base[pos-1] = x;
- return;*/
-
- push_back(list,x); //当插入位置为最后一位时,可直接用尾插法插入数据
-
- }
-
- else{
-
- for(i=list->size;i>=pos;i--)
-
- {
-
- list->base[i]=list->base[i-1];
-
- }
-
- list->base[pos-1]=x;
-
- list->size++;
-
- }
-
-
-
- }
-
- /*查找给出元素的位置,每个元素只出现一次,若有多个相同元素考虑使用数组*/
-
- int find(SeqList *list, ElemType key)
-
- {
-
- int i = 0;
-
- if(list->base == NULL)
-
- {
-
- printf("list is not exist!\n");
-
- return -1;
-
- }
-
- for(i=0;i<list->size;i++)
-
- {
-
- if(key == list->base[i])
-
- {
-
- return i+1;
-
- }
-
- }
-
- return -1;
-
- }
-
- /*顺序表的长度*/
-
- int length(SeqList *list)
-
- {
-
- if(list->base == NULL)
-
- {
-
- printf("list is not exist!\n");
-
- return -1;
-
- }
-
- return list->size;
-
- }
-
- /*删除指定位置的元素*/
-
- void delete_pos(SeqList *list, int pos)
-
- {
-
- if(list->base == NULL)
-
- {
-
- printf("list is not exist!\n");
-
- return;
-
- }
-
- if(pos <= 0 || pos > list->size)
-
- {
-
- printf("pos is illegal!\n");
-
- return;
-
- }
-
- if(pos == 1)
-
- {
-
- pop_front(list);
-
- }
-
- else if(pos == list->size)
-
- {
-
- pop_back(list);
-
- }
-
- else
-
- {
-
- for(int i = pos;i<list->size;i++)
-
- {
-
- list->base[i-1] = list->base[i];
-
- }
-
- list->size--;
-
- }
-
- }
-
-
-
- void delete_val(SeqList *list, ElemType x)
-
- {
-
- int pos;
-
- if(list->base == NULL)
-
- {
-
- printf("list is not exist!\n");
-
- return;
-
- }
-
- pos = find(list,x);
-
- if(pos == -1)
-
- {
-
- printf("value is not exist!\n");
-
- return;
-
- }
-
- delete_pos(list,pos);
-
- }
-
-
-
- void sort(SeqList *list)
-
- {
-
- //C99的C语言支持bool类型,而Visual Studio Code不支持(C++是支持的)。一些编译器认为使用该类型不够安全,加入#include<stdbool.h>
-
- bool flag = false;
-
- ElemType a;
-
- if(list->base == NULL)
-
- {
-
- printf("list is not exist!\n");
-
- return;
-
- }
-
- for(int i = 0;i<list->size-1;i++)
-
- {
-
- flag = false;
-
- for(int j = 0;j<list->size-1-i;j++)
-
- {
-
- if(list->base[j]>list->base[j+1])
-
- {
-
- a = list->base[j];
-
- list->base[j] = list->base[j+1];
-
- list->base[j+1] = a;
-
- flag = true;
-
- }
-
- }
-
- /*如果某一轮排序中无任何位置变化,则顺序表已经排好序,直接跳出循环*/
-
- if(flag == false)
-
- {
-
- break;
-
- }
-
- }
-
- }
-
-
-
- void reserve(SeqList *list)
-
- {
-
- ElemType temp;
-
- if(list->base == NULL)
-
- {
-
- printf("list is not exist!\n");
-
- return;
-
- }
-
- if(list->size == 0||list->size == 1)
-
- {
-
- return;
-
- }
-
- for(int i = 0;i < list->size/2;i++)
-
- {
-
- temp = list->base[i];
-
- list->base[i] = list->base[list->size-i-1];
-
- list->base[list->size-i-1] = temp;
-
- }
-
- }
-
- /*清除数据,表仍存在*/
-
- void clear(SeqList *list)
-
- {
-
- if(list->base == NULL)
-
- {
-
- printf("list is not exist!\n");
-
- return;
-
- }
-
- list->size = 0;
-
- return;
-
- }
-
- /*清除数据,释放表,删除base所指的顺序表空间*/
-
- void destroy(SeqList *list)
-
- {
-
- free(list->base);
-
- list->base = NULL;
-
- list->capacity = 0;
-
- list->size = 0;
-
- return;
-
- }
主程序:
- #include"SeqList.h"
-
- #include<stdio.h>
-
- int main()
-
- {
-
- SeqList myList;
-
- InitSeqList(&myList);
-
-
-
- ElemType Item;
-
- int pos;
-
- int len = 0;
-
- int select = 1;
-
- while(select)
-
- {
-
- printf("***********************************\n");
-
- printf("* [1] push_back [2] push_front *\n");
-
- printf("* [3] show_list [4] pop_back *\n");
-
- printf("* [5] pop_front [6] insert_pos *\n");
-
- printf("* [7] find [8] length *\n");
-
- printf("* [9] delete_pos [10] delete_val*\n");
-
- printf("* [11] sort [12] reserve *\n");
-
- printf("* [13] clear [14*] destroy *\n");
-
- printf("* [0] quit_system *\n");
-
- printf("***********************************\n");
-
- printf("请选择:>");
-
- scanf("%d",&select);
-
- if(select == 0)
-
- break;
-
- switch(select)
-
- {
-
- case 1:
-
- printf("请输入要插入的数据(-1结束):>");
-
- while(scanf("%d",&Item),Item!=-1)
-
- {
-
- push_back(&myList,Item);
-
- }
-
- break;
-
- case 2:
-
- printf("请输入要插入的数据(-1结束):>");
-
- while(scanf("%d",&Item),Item!=-1)
-
- {
-
- push_front(&myList,Item);
-
- }
-
- break;
-
- case 3:
-
- show_list(&myList);
-
- break;
-
- case 4:
-
- pop_back(&myList);
-
- break;
-
- case 5:
-
- pop_front(&myList);
-
- break;
-
- case 6:
-
- printf("请输入要插入的位置和数据(以输入数据为-1结束):>");
-
- while(scanf("%d %d",&pos,&Item))
-
- {
-
- if(Item == -1)
-
- {
-
- break;
-
- }
-
- insert_pos(&myList,Item,pos);
-
- }
-
- break;
-
- case 7:
-
- printf("请输入要查找的数据:>");
-
- scanf("%d",&Item);
-
- pos = find(&myList,Item);
-
- if(pos == -1)
-
- {
-
- printf("查找数据不存在\n");
-
- }
-
- else
-
- {
-
- printf("查找元素在第 %d 位\n",pos);
-
- }
-
- break;
-
- case 8:
-
- len = length(&myList);
-
- printf("顺序表中数据长度为 %d ",len);
-
- break;
-
- case 9:
-
- printf("请输入要删除数据元素的位置:>");
-
- scanf("%d",&pos);
-
- delete_pos(&myList,pos);
-
- break;
-
- case 10:
-
- printf("请输入要删除数据元素的值:>");
-
- scanf("%d",&Item);
-
- delete_val(&myList,Item);
-
- break;
-
- case 11:
-
- sort(&myList);
-
- break;
-
- case 12:
-
- reserve(&myList);
-
- break;
-
- case 13:
-
- clear(&myList);
-
- break;
-
- /*case 14:
- destroy(&myList);
- break;*/
-
- default:
-
- printf("输入的选择错误,请重新输入.\n");
-
- break;
-
- }
-
- }
-
- destroy(&myList);
-
- return 0;
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。