| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 600 人关注过本帖
标题:[原创]再来一个,链表~~~~
只看楼主 加入收藏
hlstone
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2005-12-13
收藏
 问题点数:0 回复次数:3 
[原创]再来一个,链表~~~~
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <iomanip.h>
#include <malloc.h>
typedef int Status;
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
void initlist(LinkList &L,int len)
{

L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
for(int i=len;i>0;--i)
{
LNode *p=(LinkList)malloc(sizeof(LNode));
p->data =i;
p->next=L->next;
L->next=p;
}
LNode *p=L;
for(int y=0;y<=len-1;y++)
{
cout<<p->data<<endl;
p=p->next;
}
}
void ListInsert(LinkList &L, int len, int e)
{
LNode *p=L;
int j=0;
while(p && j<len-1)
{
p=p->next;
++j;
}
if(!p||j>len-1)
cout<<"ERROR!!"<<endl;
LNode *s= (LinkList)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;
for(int i=0;i<=len;i++)
{
cout<<p->data<<endl;
p=p->next;
}
}
void ListDelete(LinkList &L, int len,int &e)
{
LNode *p=L;
int j=0;
while(p->next && j<len-1)
{
p=p->next;
++j;
}
if(!(p->next)||j>len-1)
cout<<"ERROR"<<endl;
LNode *q=p->next;
p->next=q->next;
e=q->data;
free(q);
for(int i=0;i<len-2;i++)
{
cout<<setw(5)<<p->data;
*p++;
}
}
void main()
{
int len,n,t,k;
LinkList L;
cout<<"please input the length of the list \n";
cin>>len;
initlist(L,len);
cout<<"-------------------what do you want to do?--------------------"<<endl;
loop:
cout<<"input 1 to insert postion and element \n input 2 to delete a element \n input 3 to end the project"<<endl;
cin>>t;
if(t==1){
cout<<"\n please input the insert position and element:"<<endl;
cin>>len>>n;
ListInsert(L,len,n);
cout<<"\n";
}
if(t==2){
int e;
cout<<"please input the number you want to delete,\n";
cin>>e;
ListDelete(L,len,e);
cout<<"\n";
}
if(t==3)
exit(1);
else goto loop;
}
搜索更多相关主题的帖子: 链表 
2005-12-13 23:49
EO2
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2005-12-16
收藏
得分:0 

谢谢

2005-12-16 22:13
playboy_dmx
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-5-14
收藏
得分:0 

什么东东?
先看看^_^

2006-05-14 20:50
asdffdsa
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2006-5-25
收藏
得分:0 

不是不提倡使用goto 语句吗
楼主
有什么别的目的吗
为什么不用while 或switch

2006-05-28 15:11
快速回复:[原创]再来一个,链表~~~~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014735 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved