當前位置:首頁 » 編程語言 » c語言員工系統順序線性表
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言員工系統順序線性表

發布時間: 2022-06-20 00:40:08

① 來人求救啊~~~實現線性順序表的操作 c語言

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#defineTRUE1
#defineFALSE0
#defineOK1
#defineERROR0
#defineINFEASIBLE-1
#defineOVERFLOW-2
typedefintStatus;
#defineLIST_INIT_SIZE100/*初始分配的順序表長度*/
#defineLISTINCREMENT10/*溢出時,順序表長度的增量*/
typedefintElemType;/*定義表元素的類型*/
typedefstructsqlist{
ElemType*elem;/*存儲空間的基地址*/
intlength;/*順序表的當前長度*/
intlistsize;/*當前分配的存儲空間*/
}SqList;

StatusInitList_Sq(SqList&L){//演算法2.3
//構造一個空的線性表L。
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem)returnERROR;//存儲分配失敗
L.length=0;//空表長度為0
L.listsize=LIST_INIT_SIZE;//初始存儲容量
returnOK;
}//InitList_Sq

StatusInsertDatum(SqList&L,ElemTypex){
inti;
for(i=0;i<L.length;++i){
if(L.elem[i]==x){
printf("表中有元素%d,插入失敗。 ",x);
returnERROR;
}
}
if(L.length==L.listsize){
L.elem=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!L.elem)returnERROR;
L.listsize+=LISTINCREMENT;
}
for(i=L.length;i>0;--i)
L.elem[i]=L.elem[i-1];
L.elem[0]=x;
++L.length;
returnOK;
}

StatusModifyDatum(SqList&L,ElemTypex,ElemTypey){//把值x改為y
inti;
for(i=0;i<L.length;++i){
if(L.elem[i]==y){
printf("位置%d有數據%d。 ",i+1,y);
printf("替換失敗! ");
returnERROR;
}
}
for(i=0;i<L.length;++i){
if(L.elem[i]==x){
L.elem[i]=y;
returnOK;
}
}
printf("表中沒有元素%d,修改失敗。 ",x);
returnERROR;
}

StatusDeleteDatum(SqList&L,ElemTypex){
inti,flag=0;
for(i=0;i<L.length&&!flag;++i)
if(L.elem[i]==x)flag=1;
if(flag==0||L.length==0){
printf("表中沒有找到元素%!刪除失敗。 ",x);
returnERROR;
}
for(;i<L.length-1;++i)
L.elem[i]=L.elem[i+1];
--L.length;
returnOK;
}

intFindDatum(SqList&L,ElemTypex){
inti;
for(i=0;i<L.length;++i)
if(L.elem[i]==x)returni+1;
printf("沒有找到元素%d。 ",x);
return0;
}

voidShow(SqList&L){
for(inti=0;i<L.length;++i){
if(i%10==0)printf(" ");
printf("%5d",L.elem[i]);
}
printf(" ");
}

voidDestroyList(SqList&L){
if(L.elem)free(L.elem);
}

intmain(){
inti,pos;
ElemTypex,y;
SqListL;
srand((unsigned)time(NULL));
if(!InitList_Sq(L)) {
printf("初始化未成功。");
return1;
}
for(i=0;i<LIST_INIT_SIZE;++i)
if(InsertDatum(L,rand()%1000)==ERROR)--i;
Show(L);
printf("請輸入待插入數據:");
scanf("%d",&x);
InsertDatum(L,x);
Show(L);
printf("測試替換函數! ");
printf("請輸入被替換數和替換數:");
scanf("%d%d",&x,&y);
ModifyDatum(L,x,y);
Show(L);
printf("請輸入欲查詢數據:");
scanf("%d",&x);
pos=FindDatum(L,x);
if(pos!=-1)printf("%d在位置%d。 ",x,pos);
Show(L);
DestroyList(L);
return0;
}

② C語言線性順序表的插入和刪除

#include"stdio.h"
#include"malloc.h"
#include"iostream.h"
typedef int status;
typedef int elementype;
#define INITSIZE 100
#define INCREMENT 2
struct sqlist
{
elementype *elem;
int length;
int listsize;
};
//建立鏈表,並排列數據
status listinit(sqlist &l)
{
int i=0,x,j,t;
l.elem=(elementype *)malloc(INITSIZE*sizeof(elementype));
if(!l.elem)
{
cout<<"建表失敗"<<endl;
return 0;
}
l.length=0;
l.listsize=INITSIZE;
while(1)
{
cout<<"請輸入數據(輸入0時結束):";
cin>>x;
if(x==0) break;
l.elem[i]=x;
++l.length;
i++;
}
for(i=0;i<l.length-1;i++)
for(j=0;j<l.length-i-1;j++)
if(l.elem[j]>l.elem[j+1])
{
t=l.elem[j+1];
l.elem[j+1]=l.elem[j];
l.elem[j]=t;
}
cout<<"排序成功"<<endl;
return 1;
}
//插入數據
status listinsert(sqlist &l,int i,elementype e)
{
elementype *p,*q,*newbase;
if(i<1||i>l.length)
{
cout<<"i輸入錯誤"<<endl;
return 0;
}
if(l.length>=l.listsize)
{
newbase=(elementype*)realloc(l.elem,(l.listsize+INCREMENT)*sizeof(elementype));
if(!newbase)
{
cout<<"申請空間失敗"<<endl;
return 0;
}
l.elem=newbase;
l.listsize=l.listsize+INCREMENT;
}
q=&(l.elem[i-1]);
for(p=&(l.elem[l.length-1]);p>=q;--p)
{
*(p+1)=*p;
}
*q=e;
++l.length;
cout<<"插入成功";
return 1;
}
//刪除數據
status listdelete(sqlist &l,int i,elementype &e)
{
elementype *p,*q;
if(i<1||i>l.length)
{
cout<<"i輸入錯誤"<<endl;
return 0;
}
p=&(l.elem[i-1]);
e=*p;
q=l.elem+l.length-1;
for(++p;p<=q;++p)
{
*(p-1)=*p;
}
--l.length;
cout<<"刪除成功"<<endl;
free(&e);
return 1;
}
//刪除重復的數據
status listdeleterepeat(sqlist &l)
{
int i,j;
elementype *p,*q,e;
for(i=0;i<l.length-1;i++)
for(j=i+1;j<l.length-1;j++)
if(l.elem[i]==l.elem[j])
{
p=&(l.elem[j]);
e=*p;
q=l.elem+l.length-1;
for(++p;p<=q;++p)
{
*(p-1)=*p;
}
--l.length;
free(&e);
j--;
}
return 1;
}
//輸出順序表數據
status displaylist(sqlist &l)
{
int i;
cout<<"順序表的數據為:"<<endl;
for(i=0;i<l.length;i++)
{
cout<<l.elem[i]<<" ";

}
cout<<endl;
return 1;
}
//查找數據
status locatelem(sqlist &l,int x)
{
elementype *p;
int i=1;
p=l.elem;
while(i<l.length&&(*p++)!=x)
i++;
cout<<i<<endl;
return 1;
}
//清空列表
void listclear(sqlist &l)
{
l.length=0;
}
//銷毀順序表
void listdestroy(sqlist &l)
{
if(l.elem)
free(l.elem);
}
//求順序表長度
status listlength(sqlist &l)
{
cout<<"順序表的長度為:"<<l.length<<endl;
return 1;
}
int main()
{
sqlist l;
int a,i,x;
elementype e;
cout<<"*************************************************"<<endl;
cout<<"* 順序表的表示和實現 *"<<endl;
cout<<"*************************************************"<<endl;
do{
cout<<"*************************************************"<<endl;
cout<<"* 菜單 *"<<endl;
cout<<"* 1.建立順序表 *"<<endl;
cout<<"* 2.插入數據 *"<<endl;
cout<<"* 3.刪除數據 *"<<endl;
cout<<"* 4.刪除重復數據 *"<<endl;
cout<<"* 5.清空數據 *"<<endl;
cout<<"* 6.查找數據 *"<<endl;
cout<<"* 7.順序表的長度 *"<<endl;
cout<<"* 8.顯示順序表 *"<<endl;
cout<<"* 0.退出順序表 *"<<endl;
cout<<"*************************************************"<<endl;
cout<<"輸入你的選擇:";
cin>>a;
switch(a)
{
case 1: listinit(l);
displaylist(l);
break;
case 2: cout<<"請輸入要插入數據的位置:";
cin>>i;
cout<<"請輸入要插入的數據元素:";
cin>>e;
listinsert(l,i,e);
displaylist(l);
break;
case 3: cout<<"請輸入要刪除的數據的位置:";
cin>>i;
listdelete(l,i,e);
displaylist(l);
break;
case 4: cout<<"刪除前的數據為:";
displaylist(l);
listdeleterepeat(l);
cout<<"刪除後的數據為:";
displaylist(l);
break;
case 5: cout<<"清空前為:";
displaylist(l);
cout<<"清空後為:";
listclear(l);
displaylist(l);
break;
case 6: cout<<"輸入你要查找的數據:";
cin>>x;
cout<<"你要查找的數據的位置為:";
locatelem(l,x);
displaylist(l);
break;
case 7: cout<<"順序表的長度為:";
listlength(l);
break;
case 8: displaylist(l);
break;
default: break;
}
}while(a!=0);
return 1;
}

③ 利用c語言實現順序存儲線性表的插入!

有時會出現這種情況。他會以為是木馬。
int GetElem();
int InstInsert();
typedef int ElemType;
typedef struct{
ElemType *elem; //存儲空間基地址
int length; //當前長度
int listsize;//當前分配的存儲容量
}SqList;
int InitList(SqList *L){
L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->elem)exit(-1);//存儲空間分配失敗
L->length=0; //空表長度為0
L->listsize=LIST_INIT_SIZE; //初始存儲容量
printf("線性鏈表創建成功\n");
return OK;
}
int Input(SqList *L){
ElemType temp,*newbase;
printf("輸入順序列表的數據,以0為結束標志\n");
scanf("%d",&temp);
while(temp!=0){
if(L->length>=L->listsize){
newbase=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!newbase) exit(-1);//存儲空間分配失敗
L->elem=newbase; //空間新基地址
L->listsize+=LISTINCREMENT; //增加存儲容量
}
L->elem[L->length++]=temp;
scanf("%d",&temp);
}
printf("輸入數據結束!!\n");
return OK;
}
int Onput(SqList *L){
int i=0;
printf("輸出線性表數據:");
while(i<L->length){
printf("%d\t",L->elem[i]);
i++;
}
printf("\n");
}
int ClearList(SqList *L){
L->length=0;
printf("清除成功!\n");
return OK;
}
void ListEmpty(SqList L){
if(L.elem!=NULL)
printf("true!\n");
else
printf("false!\n");
}
void ListLength(SqList L){
printf("線性表的長度是:%d\n",L.length);
return ;
}
int GetElem(SqList L,int i,SqList *e){
e=L.elem[i-1];
return e;
}
void PriorElem(SqList L,int cur_e,SqList *pre_e){
if(cur_e!=L.elem[0]){
pre_e=L.elem[0];
printf("前驅值為:%d\n",pre_e);
}
else
printf("pre_e無意義\n");
}
void NextElem(SqList L,int cur_e,SqList *next_e){
if(cur_e!=L.elem[L.length-1]){
next_e=L.elem[L.length-1];
printf("後繼值為:%d\n",next_e);
}
else
printf("next_e無意義\n");
}
int ListInsert(SqList *L,int i,int e){
ElemType *newbase,*p,*q;
if(1>i||i>(L->length+1))
return -1;
if(L->length>=L->listsize){ //新增內存
newbase=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); //開辟新內存空間
if(!newbase)
exit(-1); //存儲分配失敗
L->elem=newbase; //新基地址
L->listsize+=LISTINCREMENT; //新增內存容量
}
q=&(L->elem[i-1]);
for(p=&(L->elem[L->length-1]);p>=q;p--){
*(p+1)=*p;
}
*q=e;
++L->length;
return OK;
}
int ListDelete(SqList *L,int i,int e){
ElemType *p,*q;
if(i<1||i>L->length)
return -1;
q=&(L->elem[i-1]);
e=L->elem[i-1];
p=L->elem+L->length-1;
for(;p>=q;q++)
*q=*(q+1);
--L->length;
printf("刪除的值為:%d\n",e);
return OK;
}

④ 用c語言建立一個順序存儲的線性表,實現線性表的插入、刪除操作

鏈表
1。是由結構體和指針構成的。
2。包括兩個部分一個是數據域和指針域。
3。鏈表中的結點分為兩類:頭結點和一般結點。頭結點是沒有數據域的。
4。基本操作有:初始化鏈表,增加結點和刪除結點,求鏈表的長度等等。
struct Linknode{
int data;
struct Linknode *next;
};
這個地方有個知識點:這個是鏈表的數據結構是有結構體和指針構成。結構體名為Linknode.但這裡面沒有定義結構體變數,只有我們定義了結構體變數才能使用結構體。
結構體變數怎麼定義呢?
有兩種方式:
1。struct Linknode Linklist;
2.typedef struct linknode Linklist.
一般我們都希望採用第2種,這樣的好處是: 當我們再定義結構體變數時,可以用:Linklist p;而如果我們採用第一種還必須採用 struct Linknode p;對比一下就可以知道第2種要簡單很多。那麼下面我們都採用第2種方式來定義結構體變數。
上面我們已經定義了一個鏈表:
1。初始化鏈表。
#include<stdio.h>
#include<stdlib.h>
int InitLinkList(Linklist **Lnode)
{
*Lnode=(Linklist)malloc(sizeof(Linklist));//*Lnode等於L,對與*Lnode的分配空間相當與對主函數中的L分配空間。
if(!*Lnode)
return 0;
(*Lnode)->next=NULL;
}
在初始化鏈表的時候,我們用到了2級指針為什麼呢?因為我們希望在InitLinkList函數生成的頭結點,主函數中也能指向這個頭結點。如果我們用一級指針的話,用malloc分配空間的時候,它將會返回分配空間的首地址給指針變數Lnode,而不能使是的空間被主函數中指針變數L得到這個地址。所以我們要用2級指針。
void main()
{
Linklist *L;
InitLikList(&L);
}
2。增加鏈表結點
增加鏈表結點其實很簡單,一般用到三個結構體指針變數和一個循環結構。
InsertLinkList(Linklist *Lnode)
{
Linklist *p,*q;
int d;
{
scanf("%d",&d);
if(d==-9999)
break;
p=Lnode->next;//p指向頭結點
//通過while循環和指針變數p定位要插入的結點q的位置。
while(p)
p=p->next;
//生成一個要插入的結點
q=(Linklist)malloc(sizeof(Linklist));//申請要插入的結點空間
q->data=d;//填充要插入結點的數據域
q->next=p->next;//首先填充要插入結點q的指針域進行填充。
p->next=q;//然後把定位好的p指針域進行修改指向q.

}while(9);//循環退出的條件是輸入的數據-9999

}
void main()
{
Linklist *L;
InitLinkList(&L);//生成一個頭結點
InsertLinkList(L);//插入結點
}
3。求鏈表的長度:
int LengthLinkList(Linklist *Lnode)
{
int i=0;
Linklist *p;
p=Lnode->next;//p指向鏈表的第一個結點。
while(p)
{
i++;
p=p->next;
}
return i;
}
void main()
{
Linklist *L;
InitLinkList(&L);//生成一個頭結點
InsertLinkList(L);//插入一個結點
LengthLinkList(L)//求鏈表的長度。
}
4.刪除結點
刪除鏈表結點其實很簡單,一般用到三個結構體指針變數和一個循環結構。
DestroyLinkList(Linklist *Lnode)
{
Linklist *p,*q;
p=Lnode;//p指向鏈表的頭結點
while(p)
{
q=p->next;//q指向當前結點的下一個結點。
free(p);//釋放當前結點
p=q;//p指向下一個結點
}
}
void main()
{
Linklist *L;
InitLinkList(&L);//生成一個頭結點
InsertLinkList(L);//插入結點
LengthLinkList(L)//求鏈表的長度。
DestroyLinkList(L);//刪除鏈表結點
}

⑤ 急求助高手大蝦:C語言數據結構順序線性表的實現

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream.h>
#define LIST_INIT_SIZE 50
#define LISTINCREMENT 10
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define CANCEL 0
typedef struct{
int *elem;
int length;
int listsize;
}sqlist;
int compare(int X,int Y)
{if(X==Y)
return X;
else return FALSE;
}//compare的關系判斷
void visit(int &y)
{
y=2*y;
cout<<y<<" ";
}//將y值增加為原來的2倍
int initlist(sqlist &L)
{
L.elem=(int *)malloc(LIST_INIT_SIZE*sizeof(int));
if(!L.elem)
return ERROR;
else
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}//構造一個空的線性表L
int destroylist(sqlist &L)
{
free(L.elem);
return OK;
}//銷毀線性表L
int clearlist(sqlist &L)
{
L.length=0;
return OK;
}//將L重置為空表
int listempty(sqlist L)
{
if (0==L.length)
return TRUE;
else
return FALSE;
}//求當前表L是否為空
int listlength(sqlist L)
{
return L.length;
}//求當前線性表L的長度
int getelem(sqlist L,int i,int &e)
{
if(i<1||i>L.length)
exit(ERROR);
e=*(L.elem+i-1);
return OK;
}//用e返回L中第i個數據元素的值
int locateelem(sqlist L,int e,int(*compare)(int x1,int x2))
{
int i=1,j=0,*p;
p=L.elem;
while(i<=L.length&&!j)
{
j=compare(*p++,e);
++i;
}
if(i<=L.length)
return i-1;
else
return FALSE;
}//求L中第一個與e滿足關系compare()的數據元素的位序,若不存在則返回0
int priorelem(sqlist L,int cur_e,int &pre_e)
{
int i=2,*p;
p=L.elem+1;
while(i<=L.length&&(*p++)!=cur_e)
i++;
if (i>L.length)
return FALSE;
else
{
pre_e=*p-2;
return OK;
}
}//若cur_e是L的數據元素,且不是第一個,則用pre_e返回它的前驅,否則操作失敗,pre_e無定義
int nextelem(sqlist L,int cur_e,int &next_e)
{
int i=1,*p;
p=L.elem;
while(i<L.length&&(*p++)!=cur_e)
i++;
if (i>=L.length)
return FALSE;
else
{
next_e=*p;
return OK;
}
}//若cur_e是L的數據元素,且不是最後一個,則用next_e返回它的後繼,否則操作失敗,next_e無定義
int listinsert(sqlist &L,int i,int e)
{
int *newbase,*p,*q;
if((i<1)||(i>L.length+1))
return ERROR;
if (L.length>=L.listsize)
{
newbase=(int *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(int));
if(!newbase)
{
exit(0);
}
L.elem=newbase;
L.listsize=L.listsize+LISTINCREMENT;
}
q=L.elem+i-1;
for(p=L.elem+L.length-1;p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
return OK;
}//在線性表L中第i個位置插入元素e
int listdelete(sqlist &L,int i,int &e)
{
int *p,*q;
if(i<1||i>L.length)
return ERROR;
else
{
p=L.elem+i-1;
e=*p;
q=L.elem+L.length-1;
for(++p;p<=q;++p)
{
*(p-1)=*p;

}
L.length--;
return OK;
}
}//將線性表中的第i個元素刪除並返回其值
int listtraverse(sqlist L,void(*visit)(int &p))
{
int i=1,*p;
p=L.elem;
while(i<=L.length)
{
visit(*p);
p++;
i++;
}
return OK;
}//依次對L中的每一個元素調用函數visit(),一旦visit()失敗,則操作失敗
void main()
{sqlist L;
int i,j,k,b,n,e,m,a,cur_e,pre_e,next_e;
initlist(L);
cout<<"初始化後的基值地址:"<<L.elem<<" L.length=:"<<L.length<<" L.listsize=:"<<L.listsize<<endl;
cout<<"新建一順序表."<<endl;
cout<<"當前表是否為空表"<<listempty(L)<<endl;
cout<<"定義該線性表長度:"<<endl;
cin>>a;
cout<<"分別輸入線性表的各個元素,按ENTER"<<endl;
for(k=1;k<=a;k++){
cin>>j;
i=listinsert(L,k,j);}
for(b=1;b<=10;b++)
cout<<L.elem[b-1]<<endl;
listlength(L);
cout<<"當前表長:"<<L.length<<endl;
cout<<"輸入要取的數的位置n(n<=10)"<<endl;
cin>>n;
getelem(L,n,e);
cout<<L.elem[n-1]<<endl;
cout<<"與該數相等的的一個數的位序為:"<<locateelem(L,e,compare)<<endl;
cout<<"輸入要取前驅的數的位置m(<=10)"<<endl;
cin>>m;
getelem(L,m,cur_e);
if(priorelem(L,cur_e,pre_e))
cout<<"cur_e的前驅為:"<<pre_e<<endl;
else
cout<<"該元素沒前驅"<<endl;
nextelem(L,cur_e,next_e);
if(nextelem(L,cur_e,next_e))
cout<<"cur_e的後繼為:"<<next_e<<endl;
else
cout<<"該元素沒後繼"<<endl;
cout<<"輸入要刪元素的位序m(<=10)"<<endl;
cin>>m;
listdelete(L,m,e);
cout<<"被刪的元素為:"<<e<<endl;
cout<<"刪除元素後表長為"<<L.length<<endl;
listtraverse(L,visit);
cout<<"置為空表"<<clearlist(L)<<endl;
cout<<"銷毀線性表"<<destroylist(L)<<endl;
}

⑥ 在C語言中,如何構造一個空的順序線性表,然後怎麼用

#include<stdio.h>
#define MAXSIZE 100
struct List
{
int L[MAXSIZE];
int top;
};
int Init()//top指針初始化為0
{
int top;
top=0;
return top;
}
void Input(struct List *t,int p,int n)//輸入n個數據
{
if(n==0||n>=MAXSIZE)printf("空隊列\n");
else
for(p=0;p<n;p++)
scanf("%d",&t->L[p]);
}
void Output(struct List *s,int i,int m)//輸出m(=n)個數據哈
{

if(m==0||m>=MAXSIZE)printf("無法輸出\n");
else
for(i=0;i<m;i++)
printf("%d ",s->L[i]);
}
void main()
{
struct List r;
int n,m;
scanf("%d",&n);
m=Init();
Input(&r,m,n);
Output(&r,0,n);
}

⑦ C 語言編程 (順序線性表)

#ifndef FUNS_H
#define FUNS_H

void error( char *, ... ); /* 輸出錯誤信息,退出程序 */
void flush_stdin( void ); /* 清空「輸入緩沖區」 */

#endif

#ifndef SQLIST_H
#define SQLIST_H

#define INITSIZE 100 /* 順序表初始空間分配量 */
#define INCREMENT 10 /* 順序表空間分配增量 */

typedef int ElemType; /* 聲明ElemType代表的數據類型 */

/* 定義順序表結構 */
typedef struct {
ElemType *elem; /* 存儲空間基址 */
unsigned length; /* 當前長度 */
unsigned listsize; /* 當前空間分配量 */
} Sqlist;

/* 函數聲明 */
int InitList(Sqlist *); /* 創建順序表 */
int InputList(Sqlist *); /* 輸入數據 */
int InsertList(Sqlist *, unsigned); /* 插入數據 */
void DelList(Sqlist *, unsigned, unsigned); /* 刪除數據 */
int Union(Sqlist *, Sqlist *); /* 求順序表並集 */
void Purge(Sqlist *); /* 刪除表中重復元素 */
void Purge2(Sqlist *); /* 刪除表中重復元素 */
void Bubble(Sqlist *, unsigned); /* 冒泡排序 */
int Compare(Sqlist *, Sqlist *); /* 比較兩個順序表的大小 */
void Exchange(Sqlist *, unsigned); /* 前N個元素和後M個元素互換 */
int Location(Sqlist *, ElemType); /* 求元素位置 */
void Disp(Sqlist *, unsigned); /* 顯示順序表信息 */
void Three(Sqlist *, unsigned, unsigned); /* 三次逆轉法 */

#endif /* end of sqlist.h */

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include "../header/funs.h"

/* begin of error 05-8-15 18:40 */
void error( char *fmt, ... )
{ /* 輸出錯誤信息,退出程序 */
va_list args;

va_start( args, fmt );
fprintf( stderr, "error: " );
vfprintf( stderr, fmt, args );
fprintf( stderr, "\n" );
va_end( args );

exit( -1 );
} /* end of error */

/* begin of flush_stdin 05-8-31 19:30 */
void flush_stdin( void ) /* 清空「輸入緩沖區」 */
{
int c;

if ( !feof(stdin) ) {
while( ( c=getchar() ) != '\n' && c != EOF )
;
}
} /* end of flush_stdin */

#include <stdio.h>
#include <stdlib.h>
#include "../header/sqlist.h"

/* begin of InitList 05-8-13 0:30 */
int InitList( Sqlist *L ) /* 創建順序表 */
{
/* malloc 返回值為 void 類型,不需要顯式轉換 */
L->elem = malloc( INITSIZE * sizeof *L->elem ); /* 分配初始空間 */
if ( !L->elem ) {
return 0; /* 創建失敗返回 0 */
}

/* 創建成功,初始化字元串長度和順序表長度 */
L->length = 0;
L->listsize = INITSIZE;

return 1; /* 創建成功返回 1 */
} /* end of InitList */

/* begin of InputList 05-8-13 2:15 */
int InputList(Sqlist *L) /* 接受用戶輸入的字元串 */
{
unsigned i;
L->length = 0;

for ( i = 0; ( L->elem[i]=getchar() ) != '\n' && L->elem[i] != EOF ; ++i ) {
++L->length;
if ( L->length == L->listsize ) { /* 如果順序表已滿 */
ElemType *newbase = realloc( L->elem, /* 增加空間 */
( L->listsize + INCREMENT ) * sizeof *L->elem );
if ( newbase ) { /* 如果分配成功 */
L->elem = newbase; /* 將指針指向新分配好的空間 */
L->listsize += INCREMENT; /* 增大 listsize 的值 */
} else { /* 分配空間失敗 */
return 0; /* 增加空間失敗,返回 0 */
}
}
}

return 1;
} /* end of InputList */

/* begin of InsertList 05-8-13 5:00 */
int InsertList(Sqlist *L, unsigned pos) /* 插入數據 */
{
Sqlist tmp; /* 用於暫時存儲用戶輸入的字元 */
long i;

if ( !InitList( &tmp ) ) {
return 0;
}
if ( InputList( &tmp ) ) {
if ( !tmp.length ) {
free(tmp.elem);
return 1;
}
if ( L->listsize - L->length < tmp.length ) {
ElemType *newbase = realloc( L->elem,
( L->listsize + tmp.length ) * sizeof *L->elem );
if ( newbase ) {
L->elem = newbase;
L->listsize += tmp.length;
} else {
free(tmp.elem);
return 0;
}
}

--pos;
/* 移動字元 */
for ( i = L->length - 1; i >= (long)pos; --i ) {
L->elem[i + tmp.length] = L->elem[i];
}
i = 0;
while ( i < (long)tmp.length ) {
L->elem[pos++] = tmp.elem[i++];
}
L->length += tmp.length; /* 更新字元串長度 */

free(tmp.elem);
return 1;
}

free(tmp.elem);
return 0;
} /* end of InsertList */

/* begin of DelList 05-8-13 12:00 */
void DelList(Sqlist *L, unsigned pos, unsigned size)
{
for ( --pos; pos + size < L->length; ++pos ) {
L->elem[pos] = L->elem[pos + size];
}
L->length -= size;
} /* end of DelList */

/* begin of Union 05-8-13 12:30 */
int Union(Sqlist *L1, Sqlist *L2){ /* 求順序表並集 */
unsigned k;

/* 開始進行並集操作 */
if ( L1 == L2 ) { /* 如果 L1 和 L2 為同一順序表 */
return 1;
}
for ( k = 0; k < L2->length; ++k ) {
/* 在順序表 L1 中找不到順序表 L2 中的第k+1個元素,將第k+1個元素插入順序表 L1 */
if ( !Location( L1, L2->elem[k]) ) {
if ( L1->length == L1->listsize ) { /* 如果順序表已滿 */
ElemType *newbase = realloc( L1->elem, /* 增加空間 */
( L1->listsize + INCREMENT ) * sizeof *L1->elem );
if ( newbase ) {
L1->elem = newbase;
L1->listsize += INCREMENT;
} else {
return 0; /* 增加空間失敗,返回 */
}
}
L1->elem[ L1->length ] = L2->elem[k]; /* 插入到表 L1 */
L1->length++; /* 表 L1 長度增加 */
}
}

return 1; /* 無誤返回 */
} /* end of Union */

/* begin of Purge 05-8-13 13:00 */
void Purge(Sqlist *L) /* 刪除表中重復元素 */
{
unsigned i, j, k;

for ( i = 0; i < L->length; i++ ) {
for ( j = i+1; j < L->length; ) {
if ( L->elem[i] == L->elem[j] ) { /* 若找到重復元素 */
for ( k = j; k < L->length; k++ ) { /* 刪除重復元素 */
L->elem[k] = L->elem[k+1];
}
L->length--; /* 順序表長度減1 */
}
else {
j++;
}
}
}
} /* end of Purge */

/* begin of Purge2 05-8-13 13:20 */
void Purge2(Sqlist *L) /* 刪除表中重復元素 */
{
Sqlist T = *L;
unsigned i;

T.length = 1;
for ( i = 1; i < L->length; i++ ) {
if ( !Location( &T, L->elem[i] ) ) { /* 若找不到重復元素 */
T.elem[T.length] = L->elem[i]; /* 插入 */
T.length++; /* 更新長度 */
}
}
L->length = T.length; /* 更新長度 */
} /* end of Purge2 */

/* begin of Bubble 05-8-13 14:10 */
void Bubble(Sqlist *L, unsigned ascend)
{
ElemType temp;
unsigned i, j, k = L->length - 1;
unsigned l;

if ( !L->length ) {
return;
}
for ( l = 1; l; k-- ) {
l = 0;
for ( i = 0, j = 1; i < k; i++, j++ ) {
/* 根據 ascend 的值進行升序或者降序排列 */
if ( ( L->elem[i] < L->elem[j] ) ^ ascend ) {
temp = L->elem[i];
L->elem[i] = L->elem[j];
L->elem[j] = temp;
l = 1;
}
}
}
} /* end of Bubble */

/* begin of Compare 05-8-13 14:40 */
int Compare(Sqlist *L1, Sqlist *L2) /* 比較兩個順序表 */
{
unsigned k;

if ( L1 == L2 ) {
return 0;
}
for ( k = 0; k < L1->length && k < L2->length; k++ ) {
if ( L1->elem[k] > L2->elem[k] ) {
return 1;
}
if ( L1->elem[k] < L2->elem[k] ) {
return -1;
}
}

return L1->length - L2->length;
} /* end of Compare */

/* begin of Exchange 05-8-13 15:10 */
void Exchange(Sqlist *L, unsigned i) /* 前N個元素和後M個元素互換 */
{
/* 三次逆轉 */
Three( L, 0, i-1 );
Three( L, i, L->length-1 );
Three( L, 0, L->length-1 );
} /* end of Exchange */

/* begin of Three 05-8-13 14:55 */
void Three(Sqlist *L, unsigned i, unsigned j) /* 三次逆轉法 */
{
ElemType temp;

for (; i < j; i++, j-- ) {
temp = L->elem[i];
L->elem[i] = L->elem[j];
L->elem[j] = temp;
}
} /* end of Three */

/* begin of Location 05-8-13 12:10 */
int Location(Sqlist *L, ElemType elem) /* 求元素位置 */
{
unsigned l;

for ( l=0; l < L->length && L->elem[l] != elem; l++ ) {
;
}
if ( l == L->length ) { /* 在順序表中找不到elem */
return 0; /* 返回0 */
}

return ++l; /* 找到則返回元素位置 */
} /* end of Location */

/* begin of Disp 05-8-13 15:20 */
void Disp( Sqlist *L, unsigned total_lists ) /* 顯示順序表信息 */
{
unsigned short i;
unsigned j;

printf( "\n當前一共有 %u 個順序表。每個表的數據如下:\n", total_lists );
for ( i = 0; i < total_lists; i++ ) {
printf( "\n順序表 %d :", i+1 );
for ( j = 0; j < L[i].length; j++ ) {
printf( "%c", L[i].elem[j] );
}
printf( "\n字元串長度:%u 順序表長度:%u\n", L[i].length, L[i].listsize);
}
} /* end of Disp */

#include <stdio.h>
#include <stdlib.h>
#include "header/sqlist.h"
#include "header/funs.h"

#define MAX_LISTS 10 /* 最多可以建立的順序表個數 */

/* 函數聲明 */
char Menu( void ); /* 列印菜單,請用戶選擇 */
unsigned Choose( unsigned, unsigned, char ); /* 接收用戶輸入的選擇 */

static int tmp; /* tmp 用於清空輸入緩沖區 */
const char *msg[] = { "\n請問您要對哪個順序表(%u — %u)進行操作:",
"\n請輸入刪除數目(%u — %u):",
"\n請輸入字元串(原有數據會被覆蓋):",
"\n請輸入插入位置(%u — %u):",
"\n請輸入刪除位置(%u — %u):",
"\n此項操作至少要有兩個順序表才能進行。請再建立一個順序表。",
"\n重復元素已被刪除。",
"\n請問您要進行降序排序還是升序排序(%u 代表降序,%u 代表升序):",
"\n順序表 %u %c 順序表 %u",
"\n請輸如互換點(%u — %u):",
"\n排序完成。" };

int main( void )
{
Sqlist L[ MAX_LISTS ]; /* 順序表數組 */
char choice; /* 記錄用戶選擇的操作 */
unsigned short total_lists = 0; /* 用於記錄目前一共有多少個順序表 */
char *init_msg[] = { "內存不足!創建失敗。", /* 創建順序表的提示信息 */
"順序表創建成功!您可以開始對順序表進行操作了。" };

printf( "\n請先創建一個順序表。最多可以創建 %u 個順序表。\n", MAX_LISTS );

while ( choice = Menu() ) { /* 根據用戶輸入選擇函數運行 */
if ( !total_lists && choice != 1 ) {
printf( "\n請先創建一個順序表。最多可以創建 %u 個順序表。\n", MAX_LISTS );
} else {
switch ( choice ) {
case 1 :
if ( total_lists == MAX_LISTS ) { /* 達到最大限制 */
printf( "\n最多隻能建立 %u 個順序表。\n", MAX_LISTS );
} else {
int i = InitList( &L[total_lists] );
total_lists += i; /* 更新順序表數目 */
printf( "\n%s\n", init_msg[i] );
}
break;
case 2 :
{
unsigned num = Choose( total_lists, 1, 0 );

printf( "%s", msg[choice] );
InputList(&L[num-1]);

break;
}
case 3 :
{
unsigned num, pos;
num = Choose( total_lists, 1, 0 );
pos = Choose( L[num-1].length + 1, 1, choice );
printf( "\n請輸入字元串:" );
/* 在第 num 個順序表的第 pos 個元素處開始插入 */
InsertList( &L[num-1], pos );

break;
}
case 4 :
{
unsigned num, pos, size;
num = Choose( total_lists, 1, 0 );
if ( !L[num-1].length ) {
printf( "\n順序表為空,不能進行刪除操作。\n" );
break;
}
pos = Choose( L[num-1].length, 1, choice );
size = Choose( L[num-1].length - pos + 1, 1, 1 );
/* 從第 num 個順序表的第 pos 個位置開始,刪除 size 個元素 */
DelList( &L[num-1], pos, size );

break;
}
case 5 :
{
unsigned num1, num2;
if ( total_lists < 2 ) {
puts( msg[choice] );
break;
}
num1 = Choose( total_lists, 1, 0 );
num2 = Choose( total_lists, 1, 0 );
if ( Union( &L[num1-1], &L[num2-1] ) ) {
printf( "\n並集操作完成。操作結果保存於順序表 %u 。\n", num1 );
} else {
printf( "\n並集操作失敗。\n" );
}

break;
}
case 6 :
{
unsigned num = Choose( total_lists, 1, 0 );

Purge( &L[num-1] );
puts( msg[choice] );

break;
}
case 7 :
{
unsigned num = Choose( total_lists, 1, 0 );
unsigned ascend = Choose( 1, 0, choice );

Bubble( &L[num - 1], ascend );
puts( msg[10] );

break;
}
case 8 :
{
unsigned num1, num2;
int flag;

if ( total_lists < 2 ) {
puts( msg[5] );
break;
}
num1 = Choose( total_lists, 1, 0 );
num2 = Choose( total_lists, 1, 0 );
flag = Compare( &L[num1-1], &L[num2-1] );
if ( !flag ) {
flag = '=';
} else if ( flag > 0 ) {
flag = '>';
} else {
flag = '<';
}
printf( msg[choice], num1, flag, num2 );

break;
}
case 9 :
{
unsigned num = Choose( total_lists, 1, 0 ), point;
if ( L[num-1].length < 2 ) {
puts("\n元素太少,不能進行互換。");
break;
}
point = Choose( L[num-1].length - 1, 1, choice );
Exchange( &L[num-1], point );
puts( "\n互換完成。" );

break;
}
case 10 :
{
unsigned num = Choose( total_lists, 1, 0 );

Purge2( &L[num-1] );
puts( msg[6] );

break;
}
break;
default:
break;
}
}

/* 列印順序表的內容 */
Disp( L, total_lists );
}

while ( total_lists ) {
free( L[ --total_lists ].elem ); /* 釋放內存 */
}

puts( "\n感謝您使用我們的產品!請按回車退出..." );
getchar();
return 0; /* 退出程序 */
} /* end of main */

/* begin of Choose 05-8-13 3:00 */
unsigned Choose( unsigned up, unsigned low, char c )
{
unsigned num = 0;
do {
printf( msg[c], low, up );
scanf( "%u", &num );
flush_stdin(); /* 清空輸入緩沖區 */
} while ( num > up || num < low );

return num;
} /* end of Choose */

/* begin of Menu 05-8-12 23:20 */
char Menu( void ) /* 列印菜單,並且接受用戶輸入 */
{
int ch = -1; /* ch 用於接收用戶輸入 */

for (;;) {
printf( "\n********************************\n"
"* 1. 創建順序表 *\n"
"* 2. 輸入數據 *\n"
"* 3. 插入數據 *\n"
"* 4. 刪除數據 *\n"
"* 5. 求順序表並集 *\n"
"* 6. 刪除重復元素 *\n"
"* 7. 冒泡排序 *\n"
"* 8. 比較順序表大小 *\n"
"* 9. 前N個元素和後M個元素互換 *\n"
"*10. 刪除重復元素(2) *\n"
"* 0. 退出 *\n"
"********************************\n\n"
"請輸入您要執行的操作(按回車確定):");

scanf( "%d", &ch );
flush_stdin(); /* 清空輸入緩沖區 */

if ( ch < 11 && ch > -1 ) {
break; /* 輸入合法,退出循環 */
}
/* 非法輸入,請用戶重新輸入 */
puts("\n請輸入 0 到 10 之間的數字。\n");
} /* end of for */

return ch; /* 返回操作號 */
} /* end of Menu */

給你的郵箱,發,,網路難編輯

已經發到你的郵箱了,請注意查收.

⑧ C語言線性表順序存儲

建鏈表的函數用引用就好了

#include"stdafx.h"

#include<stdio.h>
#include<stdlib.h>
//-------------------
#defineMAXSIZE100//Thelargestnumberofpeople
typedefintElemType;
typedefstruct
{
ElemTypedata[MAXSIZE];
ElemTypepassward[MAXSIZE];
intlength;
}seqList;
seqList*L;
voidCreaList(seqList*&L,int[MAXSIZE],intn);/////引用
voidPrintList(seqList*L,intn);//不一致
intmain(void)
{
intn=0,m=0;
inta[MAXSIZE];
do
{
if(n>MAXSIZE)
{
printf("Thenumberofpeopletoomuch,pleaseinputagain! ");
}
printf("Pleaseenterthenumberofpeople(upto%d):",MAXSIZE);
scanf_s("%d",&n);
}while(n>MAXSIZE);
printf("Pleaseentertheinitailpassword:");
scanf_s("%d",&m);
CreaList(L,a,n);
printf("--------printthelineartable-------- ");
PrintList(L,n);
printf("初始化成功... ");
system("pause");
}
voidCreaList(seqList*&L,inta[MAXSIZE],intn)/////引用
{
intiPassword;
L=(seqList*)malloc(sizeof(seqList));
for(inti=1;i<=n;i++)
{
L->data[i]=a[i];
printf("Pleaseenterthe%dpersonalpassword:",i);
scanf_s("%d",&iPassword);
L->passward[i]=iPassword;
}
L->length=n;
}
voidPrintList(seqList*L,intn)
{
for(inti=1;i<=n;i++)
{
printf("--------ID---------PASSWORD-------- ");
printf("%6d%5d ",L->data[i],L->passward[i]);
}
}

⑨ C語言實現順序線性表的問題

if(L->length>>=L->listsize)應該是if(L->length +1 > L->listsize)

Status ListInsert_Sq(SqList *L,int i,ElemType e),
ElemType e是形參,在函數執行完成後,e在棧中已釋放,所以鏈表中保存的是一個非法地址,會出內存錯誤。鏈表中保存的應該是元素的地址,而不是保存元素值。

*q=e;/*插入e*/,這里也是錯誤用法.

⑩ C語言 順序線性表 實現學生成績管理系統

樓主要問什麼問題啊