链表输出某一位置的值以及该位置

来源:互联网 发布:星际战甲软件 编辑:程序博客网 时间:2024/06/07 04:02
#include<stdio.h>#include<stdlib.h>#include<malloc.h>#define OK 1#define ERROR 0#define OVERFLOW -2typedef int status;typedef int ElemType;typedef struct LNode{ElemType data;struct LNode *next;}LNode,*LinkList;void creatlist(LinkList &L,int n){int i;LinkList p;L=(LinkList)malloc(sizeof(LNode));L->next=NULL;printf("输入%d个数",n);for(i=n;i>0;--i){p=(LinkList)malloc(sizeof(LNode));scanf("%d",&p->data);p->next=L->next;L->next=p;}}void printlist(LinkList &L){    LinkList p;for(p=L->next;p!=NULL;p=p->next){printf("%d",*p);}}int  getelem(LinkList &L,int i,ElemType e){int j=0;LinkList p;p=L;while(p&&j<i){p=p->next;j++;}e=p->data;return e;}int  location(LinkList &L,ElemType e){int j=0;LinkList p;p=L;while(p!=NULL&&p->data!=e){j++;p=p->next;}return j;}int main(){int i,j,k,e,l,m;LinkList L;printf("输入一个数:\n");scanf("%d",&i);creatlist(L,i);printf("输入一个数:\n");scanf("%d",&j);printlist(L);printf("\n");getelem(L,j,e);k=getelem(L,j,e);printf("%d",k);printf("输入一个数:\n");scanf("%d",&l);location(L,l);m=location(L,l);printf("%d",m);return 0;}