浏览 1840 次
锁定老帖子 主题:想了很久还是不知道怎么改程序
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-11-06
#include "stdlib.h" #include "math.h" #define ElemType int #define OK 1 #define ERROR 0 #define N 6 typedef struct LNode { ElemType data; struct LNode *next; int length; }LNode,*LinkList; LNode *creat_L(); void out_L(LNode *L); void insert_L(LNode *L,int i ,ElemType e); ElemType delete_L(LNode *L,int i); int locat_L(LNode *L,ElemType e); int InitList_Sq(LinkList &L) { L=(LNode*)malloc(sizeof(LNode)); L->next=NULL; return OK; }//初始化 int ListInsert_L(LinkList &L,int i,ElemType &e) { LNode *s,*p;int j; p=L,j=0; while(p&&j<i-1){p=p->next;++j;} if(!p||j>i-1)return ERROR; s=(LinkList)malloc(sizeof(LNode)); s->data=e;s->next=p->next; p->next=s; return OK; }//输入 int ListDelete_L(LinkList &L,int i,ElemType &e) { LNode *p,*q; int j; p=L,j=0; while(p->next&&j<i-1){p=p->next;++j;} if(!(p->next)||j>i-1) return ERROR; q=p->next;p->next=q->next; e=q->data;free(q); return OK; }//删除 int GetElem_L(LinkList L,int i,ElemType &e) { LNode *p; int j; p=L->next; j=1; while(p&&j<i){p=p->next;++j;} if(!p||j>i) return ERROR; e=p->data; return OK; }//取表中第i个元素 int length_L(LinkList &L ,int i) { LinkList p; i=0; p=L->next; while(p) {i++;p=p->next;} return i; }//求表长 int ListElem_L(LinkList L,ElemType e) { int i; i=L[0].data; while(i&&L[i].data!=e)i=L[i].data; return i; } void print_L(LinkList L) { LNode *p; p=L->next; cout<<endl; while(p!=NULL) { cout<<p->data; p=p->next; } } //显示 void main() { int i; ElemType e; LinkList L; InitList_Sq(L); cout<<"1.建立单链表L"<<endl; cout<<"2.插入元素e"<<endl; cout<<"3.删除表中第i个元素"<<endl; cout<<"4.取表中第i个元素"<<endl; cout<<"5.退出"<<endl; cout<<"按数字键选择: "<<endl; int s; cin>>s; while(s!=4) { switch(s) { case 1: cout<<"输入几个元素组成单链表L"<<endl; for(i=1;i<=N;i++) { cin>>e;ListInsert_L(L,i,e); } print_L(L); cout<<endl; //break; case 2: cout<<"向表中插入元素: "<<endl; cin>>s; cout<<endl; ListInsert_L(L,i,s); cout<<"插入后的单链表为: "<<endl; print_L(L); cout<<endl; //break; case 3: cout<<"输入要删除的元素:"<<endl; int s; cin>>s; i=0; while(i<N) { if(L->data==s) { ListDelete_L(L,i+1,e); cout<<"删除的元素存在 :e="<<e<<endl;break; } i++; } if(i>=N) cout<<"要删除的元素不存在"; else { cout<<"删除后的单链表为: "<<endl; print_L(L); } cout<<endl;return; //break; case 4: cout<<"从表中查找元素: <<"<<endl; cin>>s; cout<<endl; ListElem_L(L,e); cout<<"插入后的单链表为: "<<endl; print_L(L); cout<<endl;return; break; case 5:return; default: cout<<"按任意键退出!"<<endl; } } } 编译结果是: j:\数据结构\单链表\单链表.cpp(146) : warning C4700: local variable 'e' used without having been initialized 单链表.obj - 0 error(s), 0 warning(s) 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |