首页 > 其他分享 >顺序链表

顺序链表

时间:2023-01-18 15:33:44浏览次数:46  
标签:顺序 return cout int lenght elem 链表 ERROR

    #include <iostream>
    #define MAXSIZE 100
    #define ERROR 0
    #define OK 1
    using namespace std;
    
    typedef struct {
    	int* elem;
    	int lenght;
    }SqlList;
    
    int InitList(SqlList& L) {
    	L.elem = new int[MAXSIZE];
    	if (!L.elem) return ERROR;
    	L.lenght = 0;
    	cout << "初始化成功" << endl;
    	return 1;
    }
    
    
    int GetElem(SqlList& L, int i, int& e) {
    	if (i<1 || i>L.lenght) return ERROR;
    	e = L.elem[i - 1];
    	cout << "第" << i << "个位置的元素是" << e << endl;
    	return OK;
    }
    
    int LocateElem(SqlList& L, int e) {
    	for (int i = 0; i < L.lenght; i++) {
    		if (L.elem[i] == e) {
    			cout << e << "位于第" << i << "个位置" << endl;
    			return OK;
    		}
    	}
    }
    
    int ListInsert(SqlList& L, int i, int e) {
    	if ((i < 1) || (i > L.lenght + 1)) return ERROR;
    	if (L.lenght == MAXSIZE) return ERROR;
    	for (int j = L.lenght - 1; j >= i - 1; j--) {
    		L.elem[j + 1] = L.elem[j];
    	}
    	L.elem[i - 1] = e;
    	++L.lenght;
    	cout << "插入元素成功" << endl;
    	return OK;
    }
    
    int ListDelete(SqlList& L, int i) {
    	if ((i < 1) || (i > L.lenght + 1)) return ERROR;
    	for (int j = i; j <= L.lenght - 1; j++) {
    		L.elem[j - 1] = L.elem[j];
    	}
    	--L.lenght;
    	cout << "删除成功" << endl;
    	return OK;
    }
    
    int main()
    {
    	int temp;
    	SqlList sl;
    	InitList(sl);
    	ListInsert(sl, 1, 10);
    	ListInsert(sl, 2, 20);
    	ListInsert(sl, 3, 30);
    	ListDelete(sl, 2);
    	GetElem(sl, 1, temp);
    	LocateElem(sl, 1);
    }

标签:顺序,return,cout,int,lenght,elem,链表,ERROR
From: https://www.cnblogs.com/qianyuzz/p/17059925.html

相关文章