欢迎来到天天文库
浏览记录
ID:43466196
大小:20.51 KB
页数:3页
时间:2019-10-03
《顺序表删除负数C语言程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、//#include"stdafx.h"#defineMAXNUM11#definetrue1#definefalse0typedefstruct{intdata[MAXNUM];intlength;}list_type;#includevoidcreatelist(list_type*lp){inti,elem;lp->length=0;printf("pleaseinputdatasofthelist");for(i=0;idata[i]=elem;lp->l
2、ength++;}}voidshowlist(list_type*lp){inti;printf("These%drecordsare:",lp->length);if(lp->length<=0){printf("Nodata!");return;}for(i=0;ilength;i++)printf("%d",lp->data[i]);printf("lengthofthelistis:%d",lp->length);}intinsertlist(list_type*lp,intnew_elem,inti){intj;if(lp->length>=MAXNUM){
3、printf("thelistisfull,cannotinsert.");return(false);}if(i<1
4、
5、i>lp->length+1){printf("%disinvalidvalue",i);return(false);}for(j=lp->length-1;j>=i-1;j--)lp->data[j+1]=lp->data[j];lp->data[i-1]=new_elem;lp->length++;return(true);}intdeletelist(list_type*lp,inti){intj;if(i<1
6、
7、i>lp->length){printf("ele
8、mnotexist");return(false);}for(j=i;jlength;j++)lp->data[j-1]=lp->data[j];lp->length--;return(true);}//删除所有负数的函数(自己编写)voiddelete_negative(list_type*lp){inti,j;for(i=0;ilength;i++){if(lp->data[i]<0){for(j=i;jlength;j++)lp->data[j]=lp->data[j+1];lp->length--;i--;}}}voidmain(){list_typeli
9、st;inti,data;createlist(&list);showlist(&list);printf("insert:Enterianddata:");scanf("%d%d",&i,&data);insertlist(&list,data,i);printf("listafterinsert:");showlist(&list);printf("delete:Enteri:");scanf("%d",&i);deletelist(&list,i);printf("listafterdelete:");showlist(&list);delete_nega
10、tive(&list);printf("listafterdeleteallnegative:");showlist(&list);}
此文档下载收益归作者所有