《数据结构 C语言链表操作.doc》由会员分享,可在线阅读,更多相关《数据结构 C语言链表操作.doc(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、#include #include #define MAXSIZE 10000typedef int ElemType;typedef struct list ElemType *elem; int listsize; int length;Sqlist;void initlist_sq(Sqlist *L)/创建一个链表L - elem = (ElemType *)malloc(MAXSIZE*sizeof(ElemType); if(! L - elem) exit(0);L - length = 0;L - listsize = MAXSIZE;void insert_sq(Sqlist
2、 *L,int n,ElemType x)/插入元素(n为插入的位置,x为插入的值)int i;if(n L - length + 1) printf(n is errorn); return ;for(i = L- length;i = n;i-)L - elemi + 1 = L - elemi;L - elemi = x;/L - elemi - 1 = x;L - length+;/printf(*Sqlist inserted*n); void creat_sq(Sqlist *L)/输入链表的值int tablen,i = 0;ElemType temp;printf(please
3、 input the table lengthn); scanf(%d,&tablen);printf(please input a serial numbern);/*doscanf(%d,&temp);insert_sq(L,i,temp);i+;while(i tablen) & (i listsize );*/while(i elemi = temp;L - length+;i+;void display_sq(Sqlist *L)/输出当前链表的值int i;i = 0;doprintf(%dt,L - elemi);while(+i length);printf(n*Sqlist
4、diplay*n); int Search_sq(Sqlist *L,ElemType x)/在当前链表中查找x int i; i = 0; do if(L - elemi = x) return i + 1; while(+i length); return 0;ElemType Search_sq_byV(Sqlist *L,int n)/(输入位置n,查找该位置的值)return L - elemn - 1;void delete_sq(Sqlist *L,int pos)/删除 pos为删除的位置doL-elempos - 1 = L-elempos;pos+;while(pos le
5、ngth);L - length-;int main(void)Sqlist L1;ElemType temp;char cmd;int i=0;initlist_sq(&L1);printf(X=EXIT,C=CREATE,P=DISPLAY,I=INSERT,S=SEARCH,D=DELETEn );while(1)cmd=getchar();switch(cmd)case C:case c:creat_sq(&L1);printf(X=EXIT,C=CREAT,P=DISPLAY,I=INSERT,S=SEARCH,D=DELETE n);break;case I:case i:prin
6、tf(please input the number to be insertedn);scanf(%d,&temp);printf(please input the position to be inserted(1100)n);scanf(%d,&i);insert_sq(&L1,i,temp);/insert_sq(&L1,i - 1,temp);printf(*Sqlist inserted*n); printf(X=EXIT,C=CREAT,P=DISPLAY,I=INSERT,S=SEARCH,D=DELETE n); break;case S:case s:printf(plea
7、se input the number wantedn);scanf(%d,&temp);if(Search_sq(&L1,temp)printf(该数在链表中的位置是第%d个.n, Search_sq(&L1,temp);elseprintf(该数在链表中不存在n);printf(X=EXIT,C=CREAT,P=DISPLAY,I=INSERT,S=SEARCH,D=DELETE n);break;case V:case v:printf(please input the pos wantedn);scanf(%d,&i);if(i = 1 & i = 0 & i = L1.length)printf(该位置的值已经删除!n);elseprintf(该位置不存在!n);printf(X=EXIT,C=CREAT,P=DISPLAY,I=INSERT,S=SEARCH,D=DELETE n);break;case P:case p:display_sq(&L1);printf(X=EXIT,C=CREAT,P=DISPLAY,I=INSERT,S=SEARCH,D=DELETE n);break;case X:case x:free(L1.elem);return 1;default:break;return 1;