📄 c-c++中结构体(struct)知识点强化2 [c-c++].htm
字号:
<TD width=744 height=7></TD>
<TD width=8 height=7><IMG height=7
src="C-C++中结构体(struct)知识点强化2 [C-C++].files/right_k.gif"
width=8></TD></TR></TBODY></TABLE>
<CENTER>
<TABLE height=7 cellSpacing=0 cellPadding=0 width=760 align=center
bgColor=#ffffff border=0>
<TBODY>
<TR>
<TD width=8 background=C-C++中结构体(struct)知识点强化2 [C-C++].files/k_bg_left.gif
bgColor=#ffa200><IMG height=8
src="C-C++中结构体(struct)知识点强化2 [C-C++].files/k_bg_left.gif" width=8></TD>
<TD align=middle width=744 height=100><SPAN id=ad_743_art
style="HEIGHT: 100px"> </SPAN></TD>
<TD width=8 background=C-C++中结构体(struct)知识点强化2 [C-C++].files/k_bg_right.gif
bgColor=#ffa200><IMG height=8
src="C-C++中结构体(struct)知识点强化2 [C-C++].files/k_bg_right.gif"
width=8></TD></TR></TBODY></TABLE>
<TABLE height=7 cellSpacing=0 cellPadding=0 width=760 align=center
bgColor=#ffffff border=0>
<TBODY>
<TR>
<TD width=8 background=C-C++中结构体(struct)知识点强化2 [C-C++].files/k_bg_left.gif
bgColor=#ffa200><IMG height=8
src="C-C++中结构体(struct)知识点强化2 [C-C++].files/k_bg_left.gif" width=8></TD>
<TD style="BORDER-TOP: #000000 1px solid" width=743><SPAN
id=menu>-</SPAN></TD>
<TD width=8 background=C-C++中结构体(struct)知识点强化2 [C-C++].files/k_bg_right.gif
bgColor=#ffa200><IMG height=8
src="C-C++中结构体(struct)知识点强化2 [C-C++].files/k_bg_right.gif"
width=8></TD></TR></TBODY></TABLE>
<TABLE height=7 cellSpacing=0 cellPadding=0 width=760 align=center
bgColor=#ffffff border=0>
<TBODY>
<TR>
<TD width=8 background=C-C++中结构体(struct)知识点强化2 [C-C++].files/k_bg_left.gif
bgColor=#ffa200><IMG height=8
src="C-C++中结构体(struct)知识点强化2 [C-C++].files/k_bg_left.gif" width=8></TD>
<TD align=middle width=744 height=100>
<TABLE cellSpacing=0 cellPadding=0 width=744 bgColor=#ffffff border=0>
<TBODY>
<TR>
<TD
style="BACKGROUND-POSITION: right top; BACKGROUND-ATTACHMENT: fixed"
vAlign=top align=middle width=577>
<TABLE cellSpacing=0 cellPadding=5 width="100%" border=0>
<TBODY>
<TR vAlign=top>
<TD class=article>
<TABLE height=22 cellSpacing=0 cellPadding=2 width="98%"
border=0>
<TBODY>
<TR>
<TD>您现在的位置:<A
href="http://www.pconline.com.cn/pcedu/index.html">软件资讯</A> > <A
href="http://www.pconline.com.cn/pcedu/empolder/index.html">开发特区</A> > <A
href="http://www.pconline.com.cn/pcedu/empolder/gj/index.html">开发语言</A> > <A
href="http://www.pconline.com.cn/pcedu/empolder/gj/c/index.html">C/C++</A></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=6 width="99%"
background=C-C++中结构体(struct)知识点强化2 [C-C++].files/point.gif
border=0>
<TBODY>
<TR>
<TD align=middle colSpan=3>
<TABLE cellSpacing=0 cellPadding=0 width="100%"
border=0>
<TBODY>
<TR>
<TD width="15%"> </TD>
<TD align=middle width="68%"><FONT color=#339900
size=3><B>C/C++中结构体(struct)知识点强化</B></FONT></TD>
<TD vAlign=bottom align=right
width="17%">出处:PConline</TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD width=145>[ 2005-03-07 15:12:06 ] </TD>
<TD align=middle>作者:管宁 </TD>
<TD align=right>责任编辑:xietaoming</TD></TR></TBODY></TABLE>
<DIV align=center><SPAN id=zl_550>-</SPAN>
<TABLE
style="BORDER-RIGHT: #cccccc 1px solid; BORDER-TOP: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; BORDER-BOTTOM: #cccccc 1px solid"
cellSpacing=0 width=414 bgColor=#f7f8f9 border=0>
<TBODY>
<TR>
<TD>
<DIV id=demo
style="OVERFLOW: hidden; WIDTH: 380px; HEIGHT: 20px">
<DIV id=demo1>
<TABLE cellSpacing=0 cellPadding=0 width=380 border=0>
<TBODY>
<TR>
<TD height=18>·<A
href="http://www.pconline.com.cn/pcedu/softnews/dongtai/0503/565543.html"
target=_blank><FONT
color=#0000ff>微软宣布64位Windows4月亮相</FONT></A></TD>
<TD height=18>·<A
href="http://www.pconline.com.cn/pcedu/softnews/dongtai/0503/566444.html"
target=_blank><FONT color=#0000ff>微软解释WinXP
OEM激活更改问题</FONT></A></TD></TR>
<TR>
<TD height=18>·<A
href="http://www.pconline.com.cn/pcedu/pingce/0502/563365.html"
target=_blank><FONT
color=#0000ff>聆听世界:龙卷风网络收音机评测</FONT></A></TD>
<TD height=18>·<A
href="http://www.pconline.com.cn/pcedu/pingce/0502/560818.html"
target=_blank><FONT
color=#0000ff>独家首发!百度硬盘搜索抢先试用</FONT></A></TD></TR>
<TR>
<TD height=18>·<A
href="http://www.pconline.com.cn/pcedu/soft/st/winxp/0503/565591.html"
target=_blank><FONT
color=#0000ff>玩转XP系统还原功能+技巧图解</FONT></A></TD>
<TD height=18>·<A
href="http://www.pconline.com.cn/pcedu/soft/gj/others/0502/563279.html"
target=_blank><FONT
color=#0000ff>与百度硬盘搜索的亲密接触</FONT></A></TD></TR>
<TR>
<TD height=18>·<A
href="http://www.pconline.com.cn/pcedu/soft/wl/brower/0503/564186.html"
target=_blank><FONT color=#0000ff>真酷!Maxthon
RSS阅读抢鲜试用</FONT></A></TD>
<TD height=18>·<A
href="http://www.pconline.com.cn/pcedu/qq/jiqiao/0502/556807.html"
target=_blank><FONT
color=#0000ff>腾讯QQ2005珊瑚虫版设置攻略</FONT></A></TD></TR>
<TR>
<TD height=18>·<A
href="http://www.pconline.com.cn/pcedu/sj/pm/other/0502/563416.html"
target=_blank><FONT
color=#0000ff>方寸见真功-趣话图标前世今生</FONT></A></TD>
<TD height=18>·<A
href="http://www.pconline.com.cn/pcedu/sj/wz/flash/0503/565376.html"
target=_blank><FONT
color=#0000ff>步入魔域-Flash光特效分析</FONT></A></TD></TR>
<TR>
<TD height=18>·<A
href="http://www.pconline.com.cn/pcedu/sj/pm/coreldraw/0502/560324.html"
target=_blank><FONT
color=#0000ff>春雨浪漫-设计一把心爱的雨伞</FONT></A></TD>
<TD height=18>·<A
href="http://www.pconline.com.cn/pcedu/sj/famous/qilianshan/"
target=_blank><FONT
color=#0000ff>更易学习:新Photoshop视频教程</FONT></A></TD></TR></TBODY></TABLE></DIV>
<DIV id=demo2></DIV></DIV>
<SCRIPT>
var speed=100
demo2.innerHTML=demo1.innerHTML
function Marquee(){
if(demo2.offsetTop-demo.scrollTop<=0)
demo.scrollTop-=demo1.offsetHeight
else{
demo.scrollTop++
}
}
var MyMar=setInterval(Marquee,speed)
demo.onmouseover=function() {clearInterval(MyMar)}
demo.onmouseout=function() {MyMar=setInterval(Marquee,speed)}</SCRIPT>
</TD></TR></TBODY></TABLE></DIV><BR><BR> 程序种有两个组成部分
<P></P>
<P class=code>test *create() </P> 和
<P class=code>void showl(test *head)
</P> 这两个函数,create是用来创建链表的 ,showl是用来显示链表的。
<BR><BR> create函数的返回类型是一个结构体指针,在程序调用的时候我们用了showl(create());,而不用引用的目的原因是引导指针是一个全局指针变量,我们不能在showl()内改变它,因为showl()函数内有一个移动操作head=head->next;,如果是引用的话我们就破坏了head指针的位置,以至于我们再也无法找会首地址的位置了。
<BR><BR> 下面我们来分解整个程序,以一个初学者的思想来思考整个程序,由浅入深的逐步解释。
<BR><BR> 首先,我们写这个程序,要考虑到由于是一个链表结构,我们不可能知道它的大小到底是多大,这个问题我们可以用动态开辟堆内存来解决,因为堆内存在程序结束前始终是有效的,不受函数栈空间生命期的限制,但要注意的是我们必须有一个指针变量来存储这一链状结构的进入地址,而在函数内部来建立这一指针变量显然是不合适的,因为函数一旦退出,这个指针变量也随之失效,所以我们在程序的开始声明了一个全局指针变量。
<P></P>
<P class=code>test *head;<FONT
color=green>//创建一个全局的引导进入链表的指针</FONT></P>
<P> 好解决了这两个问题,我们接下去思考
<BR><BR> 有输入就必然有输出,由于输出函数和输入函数是相对独立的,为了不断测试程序的正确性好调试我们先写好输出函数和main函数捏的调用,创建函数我们先约定好名为create。
<BR><BR> 我们先写出如下的代码:</P>
<P class=code>#include <<FONT
color=maroon>iostream</FONT>> <BR><FONT
color=blue>using</FONT> <FONT
color=blue>namespace</FONT> std; <BR>
<BR><FONT color=blue>struct</FONT> test <BR>{
<BR> <FONT
color=blue>char</FONT> name[10];
<BR> <FONT
color=blue>float</FONT> socre;
<BR> test *next;
<BR>}; <BR> <BR>test *head;<FONT
color=green>//创建一个全局的引导进入链表的指针 </FONT> <BR>
<BR>test *create() <BR>{ <BR>
<BR> <FONT
color=blue>return</FONT> head;<FONT
color=green>//返回链首指针 </FONT> <BR>} <BR>
<BR><FONT
color=blue>void</FONT> showl(test *head)
<BR>{ <BR> <FONT
color=maroon>cout</FONT><<"链首指针:"<<head<<endl;
<BR> <FONT
color=blue>while</FONT>(head)<FONT
color=green>//以内存指向为null为条件循环显示先前输入的内容 </FONT>
<BR> {
<BR> <FONT
color=maroon>cout</FONT><<head->name<<"|"<<head->socre<<endl;
<BR> head=head->next;
<BR> } <BR>} <BR>
<BR><FONT color=blue>void</FONT> <FONT
color=blue>main</FONT>() <BR>{
<BR> showl(create());
<BR> <FONT
color=maroon>cin</FONT>.get();
<BR> <FONT
color=maroon>cin</FONT>.get(); <BR>}</P>
<P> 程序写到这里,基本形态已经出来,输入和调用我们已经有了。</P>
<P> 下面我们来解决输入问题,链表的实现我们是通过循环输入来实现的,既然是循环我们就一定得考虑终止循环的条件,避免死循环和无效循环的发生。
</P>
<P>
<TABLE cellSpacing=0 cellPadding=3 width="96%" align=center
border=0>
<TBODY>
<TR>
<TD class=article align=right><A
href="http://www.pconline.com.cn/pcedu/empolder/gj/c/0503/567942.html">[上一页]</A>
<A
href="http://www.pconline.com.cn/pcedu/empolder/gj/c/0503/567942.html"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -