今天自己码的代码,C语言还是初学者,希望大家给出建议! :-D

#include <stdio.h>
#include <stdlib.h>

//定义栈的结构体

typedef struct {

//栈的大小和当前储存的个数
int maxSize,size;

//栈的头部(malloc出来的指针-1 则 栈的出口=head+size)
int* head;

}Stack;

//初始化栈的方法

Stack* stackInit(int size){
//申请结构体内存
Stack* p_s=(Stack*)malloc(sizeof(Stack));
//设置栈上限值
p_s->maxSize=size;
//成员为0
p_s->size=0;
//为这个栈申请最大的内存
p_s->head=((int*)malloc(sizeof(int)*size))-1;
return p_s;

}
void stackFree(const Stack* p_s){
//释放栈空间和结构体空间
free(p_s->head+1);
free(p_s);
}

int push(Stack * p_s,const int num){
//如果栈满了返回0(FALSE)
if(p_s->size>=p_s->maxSize)
return 0;

//增加一个栈
p_s->size++;
*(p_s->head+p_s->size)=num;
return 1;

}

int pop(Stack * p_s,int* re){
//如果栈是空的返回FALSE
if(p_s->size<=0) return 0;
//对指针re赋值出栈的值
*re=*(p_s->head+p_s->size);
//成员-1
p_s->size–;
return 1;
}

int main(){
//申请长度为3的栈
Stack* p_s=stackInit(3);
//push 6个进去 测试栈的长度控制
push(p_s,1);
push(p_s,2);
push(p_s,3);
push(p_s,4);
push(p_s,5);
push(p_s,6);
//为num申请内存
int* num=malloc(sizeof(int));
//pop6个栈 测试出栈长度控制
pop(p_s,num);
printf(“%d\n”,*num);
pop(p_s,num);
printf(“%d\n”,*num);
pop(p_s,num);
printf(“%d\n”,*num);
pop(p_s,num);
printf(“%d\n”,*num);
pop(p_s,num);
printf(“%d\n”,*num);
pop(p_s,num);
printf(“%d\n”,*num);

//释放所有动态申请的内存防止内存泄漏
free(num);
stackFree(p_s);
p_s=0;
}

 

10,261 thoughts on “笔记.C语言.栈的实现

  1. I am also commenting to make you understand of the awesome experience my friend’s princess encountered visiting your site. She even learned some issues, including how it is like to possess a great teaching style to get others effortlessly grasp certain hard to do topics. You truly did more than visitors’ expectations. Thanks for providing the great, safe, revealing as well as easy tips on this topic to Emily.

  2. Aw, this was a reallya veryan extremelyan incrediblyan exceptionally nice postgood post. Taking the timeFinding the timeSpending some timeTaking a few minutes and actual effort to make ato create ato produce ato generate a very goodgreatgoodreally goodtop notchsuperb article… but what can I say… I procrastinateput things offhesitate a lota whole lot and neverand don’t seem tomanage to get anythingnearly anything done.

  3. Generally I don’t read article on blogs, but I would like to say that this write-up very pressured me to check out and do so! Your writing taste has been surprised me. Thank you, very nice post.

  4. Admiring the time and energy you put into your website and detailed information you offer. It’s nice to come across a blog every once in a while that isn’t the same out of date rehashed material. Great read! I’ve bookmarked your site and I’m adding your RSS feeds to my Google account.

  5. Unquestionably imagine that which you stated. Your favourite justification appeared to be at the web the simplest thing to have in mind of. I say to you, I definitely get annoyed even as folks think about concerns that they plainly don’t recognize about. You controlled to hit the nail upon the highest as neatly as outlined out the entire thing with no need side effect , other folks could take a signal. Will likely be back to get more. Thank you

  6. Great post. I was checking continuously this blog and I’m impressed! Very helpful information specially the last part :) I care for such info much. I was seeking this certain information for a long time. Thank you and good luck.

  7. Nice blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple tweeks would really make my blog shine. Please let me know where you got your theme. Bless you

  8. Thanks for one’s marvelous posting! I definitely enjoyed reading it, you can be a great author.I will be sure to bookmark your blog and will come back at some point. I want to encourage you continue your great posts, have a nice afternoon!

  9. I would like to thank you for the efforts you have put in writing this web site. I’m hoping the same high-grade website post from you in the upcoming also. Actually your creative writing abilities has encouraged me to get my own website now. Really the blogging is spreading its wings fast. Your write up is a good example of it.

  10. Hi, I do think this is a great site. I stumbledupon it ;) I mayy
    come back once again since i have book-marked it. Money and freedom iss
    the best way to change, may you be rich and continue to guide other people.

  11. Greetings from Colorado! I’m bored to death at work so I decided to
    check out your site oon my iphone durig lunch break.
    I love the information you present here and can’t wait to take a look when I get home.

    I’m shocked at how quick yoir blog loaded on my
    mopbile .. I’m nott even using WIFI, just 3G .. Anyhow, very good blog!

  12. Hey very cool website!! Man .. Excellent .. Amazing .. I’ll bookmark your website and take the feeds also…I am happy to find a lot of useful info here in the post, we need develop more techniques in this regard, thanks for sharing. . . . . .

  13. Heya i’m for thhe primary time here. I came
    acroiss this board and I to finbd It truly helpful & it helped me out much.
    I’m hoping to offer one thing again and aid others
    such as you aided me.

  14. Good – I should certainly pronounce, impressed with your web site. I had no trouble navigating through all the tabs and related info ended up being truly simple to do to access. I recently found what I hoped for before you know it at all. Reasonably unusual. Is likely to appreciate it for those who add forums or anything, website theme . a tones way for your client to communicate. Nice task..

  15. Hmm is anyone else encountering problems with the images on this blog loading? I’m trying to find out if its a problem on my end or if it’s the blog. Any feedback would be greatly appreciated.

  16. Good day! Do you know if they make any plugins to assist with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains. If you know of any please share. Thanks!

  17. What i do not realize is if truth be told how you’re not really a lot more smartly-appreciated than you may be right now. You’re very intelligent. You already know thus significantly in the case of this topic, produced me individually imagine it from a lot of numerous angles. Its like men and women aren’t interested until it is something to accomplish with Woman gaga! Your individual stuffs excellent. At all times deal with it up!

  18. whoah this blog is great i love studying your posts. Keep up the good paintings! You already know, lots of persons are looking round for this information, you could help them greatly.

  19. I have been browsing online more than 3 hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. In my view, if all website owners and bloggers made good content as you did, the web will be much more useful than ever before.

  20. I wish to express some appreciation to the writer just for rescuing me from this matter. Because of looking out throughout the the net and coming across solutions that were not powerful, I believed my entire life was done. Living minus the answers to the problems you have sorted out by way of this article is a crucial case, as well as the kind which could have negatively affected my entire career if I hadn’t discovered your site. Your own personal ability and kindness in handling every aspect was priceless. I am not sure what I would’ve done if I hadn’t come upon such a step like this. I’m able to now relish my future. Thanks a lot so much for your skilled and effective help. I won’t hesitate to refer the website to any person who needs and wants support about this issue.

  21. Unquestionably believe that which you stated. Your favorite justification seemed to be on the internet the simplest thing to be aware of. I say to you, I definitely get annoyed while people consider worries that they plainly do not know about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects , people could take a signal. Will probably be back to get more. Thanks

  22. My wife and i felt so relieved Jordan managed to do his investigations via the ideas he got out of your web site. It’s not at all simplistic just to choose to be freely giving helpful tips that some others might have been selling. We know we have the website owner to be grateful to for that. These illustrations you made, the straightforward web site navigation, the friendships you make it possible to foster – it is mostly unbelievable, and it’s making our son in addition to our family know that the matter is interesting, and that’s extremely indispensable. Thanks for the whole lot!

  23. Greetings from Los angeles! I’m bored to death at work so I decided to check out your website on my iphone during lunch break. I enjoy the knowledge you provide here and can’t wait to take a look when I get home. I’m surprised at how fast your blog loaded on my cell phone .. I’m not even using WIFI, just 3G .. Anyways, great blog!

  24. This is really interesting, You’re a very skilled blogger. I’ve joined your rss feed and look forward to seeking more of your magnificent post. Also, I’ve shared your web site in my social networks!

  25. Very nice post. I simply stumbled upon your weblog and wished to mention that I’ve really loved surfing around your blog posts. After all I will be subscribing to your rss feed and I am hoping you write again soon!

  26. I am really enjoying the theme/design of your blog. Do you ever run into any browser compatibility issues? A couple of my blog visitors have complained about my website not working correctly in Explorer but looks great in Safari. Do you have any solutions to help fix this problem?

  27. This is very interesting, You’re a very skilled blogger. I have joined your rss feed and look forward to seeking more of your great post. Also, I’ve shared your site in my social networks!

  28. Attractive section of content. I just stumbled upon your site and in accession capital to assert that I acquire actually enjoyed account your blog posts. Any way I will be subscribing to your feeds and even I achievement you access consistently rapidly.

  29. Its like you read my mind! You appear to know so much about this, liuke
    you wrote the book in it or something. I think that you can do with a ffew pics to drive the message home a
    bit, but instead off that,this is great blog.
    An excellent read. I’ll certainly be back.

  30. Superb website you have here but I was curious about if you knew of any community forums that cover the same topics talked about here? I’d really like to be a part of online community where I can get comments from other knowledgeable people that share the same interest. If you have any recommendations, please let me know. Many thanks!

发表评论

电子邮件地址不会被公开。

:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-D :-? :) :( :!: 8-O 8) /:>.>:/ /:6423:/ /:64231:/ /:4123:/