中北大学 算法与数据结构实验报告(2)

2019-04-23 22:43

int i;

LNode *p;

int array[LIST_INIT_LENGTH]; L=(LinkList)malloc(sizeof(LNode)); L->next=NULL;

printf(\ for(i=0;i

for(i=0;idata=array[i]; //for example to a CreateList p->next=L->next; L->next=p; } //end of for

} //end of CreateList_L() function

void Contray(LinkList &head) //Contray() function

{ //Delete the NO.i element of LinkList and return by variable e LNode *p,*q; p=head;

head=NULL; while(p) { q=p; p=p->next; q->next=head; head=q; } //end of while

cout<

void main() //main() function {

LinkList L; LNode *p;

int i,LNodeNum; //j is just a counter for cycle

cout<<\ cout<<\ cin>>LNodeNum;

CreateList_L(L,LNodeNum); p=L;

5

cout<next; cout<data<<\ \ }

cout<

cout<<\ Contray(L); //call function Contray();

cout<data<<\ \ //output the LinkList after Contray L=L->next; }

cout<<\ getch();

}//end of main() function

6、实现单向线性链表插入

# include # include # include # include

# define INIT_LENGTH 10 # define OK 1 # define ERROR 0

typedef struct LNode //define LNode structure { int data; struct LNode *next;

6

}LNode,*Linklist;

int ListInsert_L(Linklist &L,int i,int e)

{//在带头结点的单链线性表L中第i个位置之前插入元素e LNode *p=L; int j=0;

while(p&&jnext; ++j; }

if(!p||j>i-1) //i小于1或i大于表长 { cout<<\错误!这个位置不存在!\ return (ERROR); }

LNode *s;

s=(Linklist)malloc(sizeof(LNode));//生成新结点 s->data=e;

s->next=p->next; p->next=s; return (OK); } //ListInsert_L() end

void main() //main() function { int i,j,e;

LNode node[10]; LNode *L,*p;

int array[INIT_LENGTH+1]={5,8,12,18,25,30,37,46,51,89}; L=node;

L=(Linklist)malloc(sizeof(LNode)); L->next=NULL; for (i=10;i>0;i--)

{ p=(Linklist)malloc(sizeof(LNode)); p->data=array[i-1]; p->next=L->next; L->next=p; } p=L;

cout<

cout <next; cout<data<<\ }

7

cout<>j;

cout<<\请输入要插入的元素: \ cin>>e;

if(ListInsert_L(L,j,e))

{ cout <next; cout<data<<\ } }

cout<

7、实现单向线性链表删除

#include #include #include

typedef struct LNode { int data; struct LNode *next; }LNode,*LinkList;

void CreateList_L(LinkList &L,int n) //CreateList_L() function { //创建一个带头结点的单链表L int i;

LNode *p;

L=(LinkList)malloc(sizeof(LNode)); L->next=NULL; for(i=0;i

8

cin>>p->data; p->next=L->next; L->next=p; }//结束for

}//end of CreateList() function

int ListDelete_L(LinkList &L,int i,int &e) //ListDelete_L() function

{ //在带头结点的单链线性表L中,删除第i个元素,并由e返回其值 LNode *p,*q; int j=0; p=L;

while(p->next&&jnext;++j; }

if(!p||j>i-1) //删除位置不合理

{ cout<<\位置\的数据不存在!\ getch(); return(0); }

q=p->next; //用指针q指向被删除的结点 p->next=q->next; //删除第i个结点

e=q->data; //取出第i个结点数据域值 free(q); //释放第i个结点 return (e); }//结束删除元素

void main() //main() function {

LinkList L; LNode *p;

int e; //e can be Every DataType int i,j; //j is just a counter for cycle int LListNodeNum;

cout<<\ cout<<\请输入创建的单链表的结点个数: \ cin>>LListNodeNum;

cout<

cout<next;

while(p) //输出创建的单链表 { cout<data<<\ \ p=p->next;

9


中北大学 算法与数据结构实验报告(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:考研数学历年真题(1987-2011)年数学一

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: