基于链式存储结构的栈和单链表几乎没差,不同的地方在链栈也是只能在一端进行存取操作,而单链表则可以在任何位置进行。单链表的建立分为前插法和后插法,前插法是将每次新插入的结点作为头,后插法是将每一次新进入的结点放在最后面。链栈的建立用前插法是很方便的。
链栈的结点类和单链表的一样:
#pragma once #include<iostream> using namespace std; template<class T> class LinkNode { public: T data; LinkNode *link; public: LinkNode() { link = NULL; //初始化 指针域为空,data域未知 } LinkNode(T &da) { data = da;//传参的构造函数,给data域赋值 link = NULL; //指针域仍未空 } ~LinkNode()//析构函数 可为空 {} };链式栈结构中的top真的成为了一个指针,是一个结点类型指针,用来指向栈顶元素的位置。
//链栈作用在不知道栈元素多少的情况下,便于合理利用空间,不会造成浪费 //没有最大MAXSIZE属性值,也没有栈溢出判断,没有头结点 #pragma once #include"LinkNode.h" #include<iostream> using namespace std; template<class T> class Stack { LinkNode<T> *top;//首指针 public: Stack(); Stack(Stack<T> &S); ~Stack(); void makeEmpty();//销毁链栈 LinkNode<T> * GetTop();//返回链栈的 首指针top void push(T elem);//进栈 T pop();//出栈,这里是链栈 返回的是指向当前栈顶元素的指针(最好返回元素值) int Length();//求栈当前的大小 bool isEmpty();//判链栈空 void output();//一次性全部输出链栈元素,仍从栈顶出 void operator= (Stack<T> &S);//复制函数 }; template<class T> Stack<T>::Stack() { top = NULL;//初始化 首指针为空,即为空栈 } template<class T> Stack<T>::Stack(Stack<T> &S)//切记勿忘 <T> { LinkNode<T> *srcptr = S.GetTop(); LinkNode<T> *destptr = top = NULL; LinkNode<T> *newNode; T elem;//临时存储变量 elem = srcptr->data; newNode = new LinkNode<T>(elem); top = newNode;//此四步骤 是为了解决没有头结点 需先为当前连战的top值赋值 此后保存首指针 destptr = top; srcptr = srcptr->link;//关键的一句,要不然从此和top断开 //从第二个开始循环复制 while(srcptr != NULL) { elem = srcptr->data;//获取当前指针所指向的元素值 newNode = new LinkNode<T>(elem);//以此元素值为参数 new出新的 destptr->link = newNode;//然后放在当前链栈的后面 srcptr = srcptr->link;//两个链栈指针后移一位 destptr = destptr->link; } } template<class T> Stack<T>::~Stack() { makeEmpty(); } template<class T> void Stack<T>::makeEmpty() { LinkNode<T> *del; while(top != NULL) { del = top;//删除循环的赋值,,,del的被赋值必须是循环的 top = del->link; delete del; } } template<class T> LinkNode<T> * Stack<T>::GetTop() { return top; } template<class T> void Stack<T>::push(T elem) { LinkNode<T> *newNode; newNode = new LinkNode<T>(elem); //if(newNode == NULL) // return false; newNode->link = top; top = newNode; //return true; } template<class T> T Stack<T>::pop() { T elem; LinkNode<T> *del = top; top = del->link; elem = del->data; delete del; return elem;//出栈 相当于删除栈顶元素了 } template<class T> int Stack<T>::Length() { LinkNode<T> *current = top; int count = 0; while(current != NULL) { current = current->link; count++; } return count; } template<class T> bool Stack<T>::isEmpty() { return ((top == NULL) ? true : false); } template<class T> void Stack<T>::output() { LinkNode<T> *current = top; int count = 0; while(current != NULL) { cout<<"#"<<count+1<<":"<<current->data<<endl; current = current->link; count++; } } template<class T> void Stack<T>::operator= (Stack<T> &S) { //makeEmpty();//先销毁栈 LinkNode<T> *srcptr = S.GetTop(); LinkNode<T> *destptr = top;//其实销毁后空栈top即为NULL 无须赋值 LinkNode<T> *newNode; T elem;//临时存储变量 elem = srcptr->data; newNode = new LinkNode<T>(elem); top = newNode;//此四步骤 是为了解决没有头结点 需先为当前连战的top值赋值 此后保存首指针 destptr = top; srcptr = srcptr->link; //从第二个开始循环复制 while(srcptr != NULL) { cout<<"hello1"<<endl; elem = srcptr->data;//获取当前指针所指向的元素值 newNode = new LinkNode<T>(elem);//以此元素值为参数 new出新的 destptr->link = newNode;//然后放在当前链栈的后面 srcptr = srcptr->link;//两个链栈指针后移一位 destptr = destptr->link; } }
<CodeLite_Project Name="libcorejni" InternalType="Library">
<Plugins>
<Plugin Name="qmake">
<![CDATA[00010001N0005Debug000000000000]]>
</Plugin>
</Plugins>
<Description/>
<Dependencies/>
<VirtualDirectory Name="src">
<VirtualDirectory Name="jni">
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/Xfermode.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/Utils.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/Typeface.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/Shader.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/Region.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/Rasterizer.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/PorterDuff.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/Picture.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/PathMeasure.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/PathEffect.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/Path.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/Paint.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/NinePatchImpl.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/NinePatch.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/Movie.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/Matrix.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/MaskFilter.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/LayerRasterizer.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/Interpolator.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/Graphics.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/DrawFilter.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/CreateJavaOutputStreamAdaptor.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/com_google_android_gles_jni_GLImpl.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/com_google_android_gles_jni_EGLImpl.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/com_android_internal_os_ZygoteInit.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/com_android_internal_graphics_NativeUtils.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/ColorFilter.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/Canvas.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/Camera.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/BitmapRegionDecoder.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/BitmapFactory.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/Bitmap.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android/graphics/AutoDecodeCancel.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_view_ViewRoot.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_view_Surface.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_view_Display.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_util_XmlBlock.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_util_StringBlock.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_util_Process.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_util_Log.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_util_FloatMath.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_util_FileObserver.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_util_EventLog.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_util_Binder.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_util_AssetManager.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_text_KeyCharacterMap.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_text_format_Time.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_text_AndroidCharacter.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_text_AndroidBidi.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_security_Md5MessageDigest.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_pim_EventRecurrence.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_os_UEventObserver.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_os_SystemProperties.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_os_SystemClock.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_os_StatFs.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_os_Power.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_os_ParcelFileDescriptor.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_os_MessageQueue.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_os_MemoryFile.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_os_FileUtils.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_os_Debug.cpp"/>
<File Name="../../android-omap-20111108-gingerbread/frameworks/base/core/jni/android_opengl_GLES20.cpp"/>
<File Name="../.
如图第一个方框内是异步复位和同步释放电路。有两个D触发器构成。第一级D触发器的输入时VCC,第二级触发器输出是可以异步复位,同步释放后的复位信号。
电路目的:方式复位信号撤除时产生亚稳态事件。
所谓异步复位和同步释放,是指复位信号是异步有效的,即复位的发生与clk无关。后半句“同步释放”是指复位信号的撤除(释放)则与clk相关,即同步的。
下面说明一下如何实现异步复位和同步释放的。
异步复位:显而易见,rst_async_n异步复位后,rst_sync_n将拉低,即实现异步复位。
同步释放:这个是关键,看如何实现同步释放,即当复位信号rst_async_n撤除时,由于双缓冲电路的作用,rst_sync_n复位信号不会随着rst_async_n的撤除而撤除。
假设rst_async_n撤除时发生在clk上升沿,如果不加此电路则可能发生亚稳态事件,但是加上此电路以后,假设第一级D触发器clk上升沿时rst_async_n正好撤除,则D触发器1输出高电平“1”,此时第二级触发器也会更新输出,但是输出值为前一级触发器次clk来之前时的Q1输出状态。显然Q1之前为低电平,顾第二级触发器输出保持复位低电平,直到下一个clk来之后,才随着变为高电平。即同步释放。
module reset_gen (
output rst_sync_n,
input clk, rst_async_n);
reg rst_s1, rst_s2;
wire rst_sync_n ;
always @ (posedge clk, posedge
rst_async_n)
if (rst_async_n) begin
rst_s1 <= 1'b0;
rst_s2 <= 1'b0;
end
else begin
rst_s1 <= 1'b1;
rst_s2 <= rst_s1;
end
assign rst_sync_n = rst_s2;
endmodule