C ++ study notes -- 01

Source: Internet
Author: User

Recently I was preparing to switch jobs. So I took a look at C ++ and made some exercises by the way, mainly in terms of data structure. I posted them here to make a series of exercises, so I can urge myself. On the first day, I wrote a stack, debugged it, and solved the memory leakage problem. Copy code 1 # ifndef STACK_H 2 # define STACK_H 3 4 # include "stdlib. h "5 # include" iostream "6 7 class Stack 8 {9 private: 10 typedef int NODE_DATA_TYPE; 11 typedef struct Node {12 NODE_DATA_TYPE data; 13 struct Node * next; 14} Node; 15 Node * header; 16 public: 17 Stack () 18 {19 header = 0; 20} 21 ~ Stack () 22 {23 std: cout <"Destructor called." <std: endl; 24 while (header! = NULL) 25 {26 Node * temp = header; 27 header = header-> next; 28 free (temp); 29} 30 _ ASSERTE (_ CrtCheckMemory ()); 31 std: cout <"Destructor call finished. "<std: endl; 32} 33 Stack (NODE_DATA_TYPE data) 34 {35 header = (Node *) malloc (sizeof (Node )); 36 header-> data = data; 37 header-> next = NULL; 38 _ ASSERTE (_ CrtCheckMemory (); 39} 40 bool isEmpty (void) 41 {42 return header = 0? True: false; 43} 44 void addElement (NODE_DATA_TYPE data) 45 {46 if (header = 0) 47 {48 header = (Node *) malloc (sizeof (Node )); 49 header-> data = data; 50 header-> next = NULL; 51} 52 else 53 {54 Node * newNode = (Node *) malloc (sizeof (Node )); 55 newNode-> data = data; 56 newNode-> next = header; 57 header = newNode; 58} 59 _ ASSERTE (_ CrtCheckMemory ()); 60} 61 NODE_DATA_TYPE popElement (void) 62 {63 if (header = 0) 64 {65 std: cout <"Empty stack, operation teminated. "<std: endl; 66 return-1; 67} 68 else 69 {70 NODE_DATA_TYPE popData = header-> data; 71 Node * temp = header; 72 header = header-> next; 73 free (temp); 74 _ ASSERTE (_ CrtCheckMemory (); 75 return popData; 76} 77} 78 void printStack (void) const 79 {80 Node * temp = header; 81 while (temp! = NULL) 82 {83 std: cout <temp-> data <std: endl; 84 temp = temp-> next; 85} 86} 87 bool searchElement (NODE_DATA_TYPE data) 88 {89 while (header-> next! = NULL) 90 {91 if (header-> data = data) 92 {93 return true; 94} 95 else 96 {97 header = header-> next; 98} 99} 100 return false; 101} 102}; 103 # When the endif copy code starts in this sentence: free (temp);, an error is always reported. The error is as follows: CRT detected that the application wrote to memory after end of heap buffer later turned for help and found that an error occurred while applying for memory. the (Node *) malloc (sizeof (Node )) written as (Node *) malloc (sizeof (Node. The differences between struct and struct pointer in sizeof are ignored, which is slightly incorrect. However, it takes only one night to get it done, which is also a reminder.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.