struct-Body pointer

Source: Internet
Author: User

Problems that have been confusing, including stacks, queues, and implementations of pointers to structures,

have problems, but have not found the essential error,

is to define a struct pointer and to request a memory for it;

typedef struct STACK
{
int top;
int base;
Char *elem;
} stack, *stack1;


Stack1
Creat_stack (void)
{
Stack1 s;
s = (stack1) malloc (sizeof (stack));
S->elem = (char*) malloc (maxsize * sizeof (char));
S->top = s->base = 0;
printf ("S->elem = =%p\n", S->elem);
return s;
}

A structural body stack1 s is defined; You can not operate on it, otherwise it will produce an error!!!

Also requires malloc like this

s = (stack1) malloc (stack);

The defined struct pointer simply indicates that the pointer is pointing to the structure, but does not allocate memory space to it, which is similar to declaring, just telling the system what the pointer is for.

Linked lists are linked from one node to the other, and each node needs memory space to store the data.
So every new node you create, you need to open up a space to store your data.


Examples of errors
#include <cstdio>
#include <cstdlib>
#define _oj_

#define MAXSIZE 5
typedef struct LNODE
{
int front;
int rear;
int *elem;
} deque, *deque;



void
Creat_deque (deque q)
{
Q->elem = (int*) malloc (maxsize * sizeof (int));
Q->front = q->rear = 0;
}

Int
Isfull (Deque Q)
{
if ((q->rear + 1)% MaxSize = = Q->front)
return 1;
Else
return 0;
}

Int
IsEmpty (Deque Q)
{
if (Q->front = = q->rear)
return 1;
Else
return 0;
}

Int
Deque_size (deque q)
{
Return (q->front-q->rear + maxsize)% MaxSize;
}

void
Endeque (Deque Q)
{
int x;
scanf ("%d", &x);
Q->elem[q->rear] = x;printf ("%d\n", q->rear);
Q->rear = (q->rear + 1)% MaxSize;
}

Int
Del_deque (deque q)
{
int e;
E = q->elem[q->front];
Q->front = (q->front + 1)% MaxSize;
return e;
}

int main (int argc, char const *argv[]) {
#ifndef _oj_//online_judge
Freopen ("Input.txt", "R", stdin);
#endif

Deque q;//No memory space assigned to Q here!
Creat_deque (q);
printf ("IsEmpty = =%d\n", IsEmpty (q));
printf ("size = =%d\n", deque_size (q));
printf ("full = =%d\n", Isfull (q));

int n, I;
n = 3;
Do
{
Endeque (q);
}
while (!isfull (q));

while (!isempty (q)) {
printf ("del = =%d\n", Del_deque (q));
}


return 0;
}
























struct-Body pointer

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.