线性表的插入和删除,,遇到麻烦了,望大仙解决,嘿嘿。。。。。
程序代码:
#include <iostream> #include <stdlib.h> using namespace std; #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 #define LIST_INIT_SIZE 10 #define LISTINCREMENT 10 typedef int elemtype; typedef struct { elemtype *elem; int length; int list_size; }sqlist; //构造空的线性表 elemtype init_list_sq(sqlist &list) { list.elem = (elemtype*)malloc(LIST_INIT_SIZE*sizeof(elemtype)); if(!list.elem) exit(OVERFLOW); list.length = 0; list.list_size = LIST_INIT_SIZE; return OK; }// //在表list中第i个位置插入元素e elemtype list_insert_sq(sqlist &list, int i, elemtype e) { //在顺序表list的第i个位置插入元素e //i的合法位置为1到list_length-1 if(i<1 || i>list.length+1) return ERROR; if(list.length>=list.list_size) { elemtype *newbase=(elemtype *)realloc(list.elem, (list.list_size+LISTINCREMENT)*sizeof(elemtype)); if(!newbase) exit(OVERFLOW); list.elem = newbase; list.list_size += LISTINCREMENT; } elemtype *q = &list.elem[i-1]; elemtype *p = NULL; for(p=&(list.elem[list.length-1]); p>=q; --p) { *(p+1) = *p; } *q = e; ++list.length; cout<<"插入成功!"<<endl; return OK; }// //删除list表中第i个元素e elemtype list_delete_sq(sqlist &list, int i, elemtype &e) { //删除元素的合法位置1<=i&&i<=list.length if(i<1&&i>list.length) return ERROR; elemtype *p = &(list.elem[i-1]); e = *p; elemtype *q = &list.elem[list.length - 1]; for(++p; p<=q; ++p) { *(p-1) = *p; } --list.length; return OK; }// int main(int argc, char *argv[]) { sqlist sq_list; init_list_sq(sq_list); do { int i=0; int elem = 0; cout<<"Input the elem you want (or Input 1111 to exit!): "; cin>>elem; if(elem==1111) break; list_insert_sq(sq_list, i, elem); ++i; }while(TRUE); return 0; }输入了,没结果,不知道插入成功没,而且添加输出,还是没显示!