Implementing stacks and queues with arrays and linked lists, respectively

Source: Internet
Author: User

2015.2.8
Tuesday, Sunny

Array operations for Stacks:

The characteristics of the stack: Advanced after-out, LIFO first.

Implemented as an array: you first need to create a struct that includes a data pointer, a variable that points to the "pointer" on the top of the stack, and the size of the tag stack. The data pointer is the first address of an array of a certain size. Structure and this
The array is sufficient to form a complete stack.

1. In the operation of creating a stack, the main thing is to create this struct and this array, and to initialize it, this operation and the creation of the list is similar, the initialization of the statement:
Stack->max_depth = max_depth; Describe the size of the stack
Stack->pop =-1; Indicates that the current stack is an empty stack

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

typedef int data_t;

typedef struct stack{//create a struct
data_t *data;
int pop;
int max_depth;
} sqstack_t;

sqsatck_t * create_empty_sqstack (int max_depth)
{
sqstack_t *stack;
Stack = (sqstack_t *) malloc (sizeof (sqstack_t)); Create a stack first
if (stack)
{
Stack->data = (data_t *) malloc (sizeof (data_t) * max_depth); To create an array, note the algorithm for array size
if (Stack->data = = NULL)
{
Free (stack);
return NULL;
}
}

Stack->max_depth = max_depth; Initialization of the stack
Stack->pop =-1;

return stack;

}

void Destory_sqstack (sqstack_t stack)//Destroy a stack, should be not a linked list, so do not need to release each data in the array of memory, the pointer to the memory is released on the line
{
if (stack)
{
if (Stack->data)
{
Free (stack->data); Releasing an array
Stack->data = NULL;
}
Free (stack);//release the structure, at which time the entire stack is destroyed
stack = NULL;
return;
}
}

void Clear_sqstack (sqstack_t stack)//empty stack, also do not need to clear each, directly to the top of the stack pointer operation on the line
{
if (stack = = NULL)
{
return-1;
}

Stack->pop =-1; Point the top pointer to null, indicating that the stack is empty at this time

Return
}
int Empty_sqstack (sqstack_t stack)//Determine if the stack is empty, just need to search the top of the stack pointer on the line
{
if (stack = = NULL)
{
return-1;
}

return (Stack->pop = =-1 1:0); Determines whether the top pointer of the stack points to an empty

}
int Full_sqstack (sqstack_t stack)//Determine if the stack is full, just look for the stack top pointer and the length of the stack is equal to the line.
{
if (stack = = NULL)
{
return-1;
}
Return (Stack->pop + 1 = = Max_depth 1:0); Search for the stack top pointer is equal to the length of the stack
}

int Push_sqstack (sqstack_t stack,data_t x)//main operation comes up, press stack, should be the stack top pointer always points to the top of the stack of data, so, before pressing the data need to move the stack top pointer, \
{Otherwise the original data will be overwritten
if (Full_sqstack (stack))
{
return-1;
}

stack->pop++;//Move Stack top pointer
Stack->data[stack->pop] = x; Press

return 0;
}
int Pop_sqstack (sqstack_t stack,data_t *x)///is also the main operation, out of the stack, should be the top of the stack pointer always point to the top of the data, before the stack does not need to operate the top pointer, but \
{You will need to modify the position of the top pointer after the stack so that the next action on the stack
if (Empty_sqstack (stack))
{
return-1;
}

if (x)
{
*x = stack->data[stack->pop]; Out of the stack
stack->pop--; Modify the top pointer of the stack
return 0;
}
Else
{
return-1;
}


}
int Get_sqstack_pop (sqstack_t stack,data_t *x)//Get the data at the top of the stack, this function simply takes the data from the top of the stack, so, without modifying the stack top pointer
{
if (Empty_sqstack (stack))
{
return-1;
}

if (x)
{
*x = stack->data[stack->pop];
return 0;
}
Else
{
return-1;
}
}


List operations for Stacks:

The way the list is implemented and the many operations of a single-linked list are similar, especially in the creation, initialization, emptying the stack to clear the list of data, the stack when the destruction action is very similar

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

typedef int data_t;

typedef struct lstack{//create a simple struct
data_t data;
struct Lstack *next;
}linkstack_t;

linkstack_t * create_empty_linkstack (int max_depth)
{
linkstack_t *stack;
Stack = (linkstack_t *) malloc (sizeof (linkstack_t));
if (stack)
{
Stack->next = NULL; Initialize the top of the stack, which is the table header
}

return stack;
}

void Clear_linkstack (linkstack_t stack)
{
linkstack_t *node;
if (stack = = NULL)
{
return-1;
}

if (NULL! = stack->next)//empty operation, emptying the memory, using the tag pointer
{
node = stack->next;
Stack->next = node->next;
Free (node);
}
Return
}

void Destory_linkstack (linkstack_t stack)//Destroy operation, Big principle: first empty, after destruction (hehe, seemingly very green)
{
if (stack)
{
Clear_linkstack (stack);
Free (stack);
stack = NULL;
}
}

int Empty_linkstack (linkstack_t stack)//To determine whether it is empty, only need to look up the table header pointer on the line (note that there is no write judgment stack full function, should be for the list can grow, all not full)
{
if (stack)
{
return (Stack->next = = NULL? 1:0); The lookup table header pointer is pointing to an empty
}
Else
{
return-1;
}
}

int Push_linkstack (linkstack_t stack,data_t x)//stack operation, always place the data in the position pointed to by the header, it's simple.
{
linkstack_t *node;

if (stack = = NULL)
{
return-1;
}

node = (linkstack_t *) malloc (sizeof (linkstack_t));
if (node = = NULL)
{
return-1;
}
Node->data = x;

Node->next = stack->next; Inserted action
Stack->next = node;

return 0;
}

int Pop_linkstack (linkstaxk_t stack,data_t *x)//out of the stack operation, before the stack needs to determine whether the stack is empty, so as not to mistakenly operate, after the stack to the top of the data released, so, \
{You need to have a struct that is marked with a pointer tag that needs to be disposed
linkstack_t *node;

if (stack = = NULL | | stack->next = = NULL)
{
return-1;
}
node = stack->next;//Action on the marker pointer
Stack->next = node->next;

if (x)
{
*x = node->data;
}

Free (node);

return 0;

}

int Get_linkstack_pop (linkstack_t stack,data_t *x)//Get stack top data, this time just fetch, do not release
{
if (stack = = NULL | | stack->next = = NULL)
{
return-1;
}

*x = stack->next->data;

return 0;


}

Array implementations of the queue:

Characteristics of the queue: FIFO, LIFO

The way the array is implemented: the first need to clear the array length, so that the flag team head and the tail of the pointer can manipulate the array, here is the use of the loop array, so the actual length of the array is equal to the length of the stored data +1;
Here are two important formulas for the first and the end of the team:

Queue->rear= (queue->rear+ 1)% N; Take the first team data
Queue->rear= (queue->rear+ 1)% N; Inserting data into the end of a team

Queue->front = = queue->rear//empty stack judging conditions
Queue->front = = (queue->rear + 1)% N//Man Judging conditions


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

#define Fifo_len 9//number of stored data
#define N (Fifo_len + 1)//The actual length of the array

typedef int data_t;
typedef struct{
data_t Data[n];
int front;
int rear;
}sequeue_t;

sequeue_t *create_empty_queue ()//Create an empty queue, the required initialization is to point the first and the end of the team pointer to the mark bit, the empty stack is 0
{
sequeue_t *queue;
Queue = (sequeue_t *) malloc (sizeof (sequeue_t));
if (queue = = NULL)
{
return NULL;
}

Queue->front = queue->rear = 0; Initialize the team head and the tail of the team pointer
return queue;
}

void Destory_queue (sequeue_t *queue)//Destroy the queue and directly release the memory that the struct pointer points to.
{
if (queue)
{
Free (queue);
}
}

int Empty_queue (sequeue_t *queue)//Determine if the queue is empty, mainly to search for the first and the tail of the team and compare the pointer
{
if (queue = = NULL)
{
return-1;
}

return (Queue->front = = queue->rear? 1:0); Comparison formula for empty queues
}

int Full_queue (sequeue_t *queue)//Determine if the queue is full
{
if (queue = = NULL)
{
return-1;
}

Return ((queue->rear + 1)% N = = Queue->front 1:0); Comparison formula for full queues
}

int Clear_queue (sequeue_t *queue)//empty queue operation, mainly is to talk about the team head and the tail of the pointer pointing to the empty stack of the flag bit just fine
{
if (queue = = NULL)
{
return-1;
}
Queue->front = queue->rear = 0; Empty operation, as with initialization
return 0;
}

int En_queue (sequeue_t *queue,data_t x)//To insert an array into the tail of the queue, you first need to determine if the queue is full, and then insert the data according to the criteria
{
if (queue = = NULL | | Full_queue (Queue) = = 1)
{
return-1;
}

Queue->rear= (queue->rear+ 1)% N; Modify the tail pointer to where you want to insert the data, and then insert the data
Queue->data[queue->rear] = x;
return 0;

}

int De_queue (sequeue_t *queue, data_t *x)//To go out of the first data, the first need to be empty, because the data is stored in the array, the subsequent insertion of data can be directly overwritten, so there is no need to clear the operation
{
if (queue = = NULL | | Empty_queue (Queue) = = 1)
{
return-1;
}

Queue->front= (queue->front+ 1)% N; Modify the team's first pointer to point to the data that will be acquired
if (x)
{
*x = Queue->data[queue->front]//Fetch data
}

return 0;
}


List-implemented queues:

Linked list Implementation Queue operation: Here the team head and the tail of the team wrapped in a structure, the operation of the two pointers in a slightly different way, especially when creating an empty queue, not the data structure of the linked list operation,
But the two pointers to the corresponding operation, more experience.

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

typedef int data_t;
typedef struct lqueue{
data_t data;
struct Lqueue *next;
}queuenode_t,*queuelist_t;

typedef struct{
queuelist_t front,rear;
}linkqueue;

Queue_link *create_empty_linkqueue ()//Create an empty queue: Create a struct that encapsulates the pointer, and then initialize the two pointers
{
Linkqueue *queue;

Queue = (Linkqueue *) malloc (sizeof (linkqueue)); Create a queue header
if (queue = = NULL)
{
return NULL;
}
Queue->rear = Queue->front = NULL; Initializing a table header
return queue;
}

void Clear_linkqueue (Linkqueue *queue)//emptying team, the list implementation of the queue needs to be emptied, where the team first pointer always points to the first data structure body
{
queuelist_t Node_free;

if (queue = = NULL)
{
return-1;
}

while (Queue->front! = NULL)
{
Node_free = queue->front;
Queue->front = node->free->next;
Free (node_free);
}

return;


}

void Destory_linkqueue (sequeue_t * queue)//Destroy queue: Large principle, first empty, then destroyed
{
if (queue)
{
Clear_linkqueue (queue);
Free (queue);
}
}

int Empty_linkqueue (Linkqueue *queue)//Determine if the queue is empty, as long as the search for the first pointer is empty.
{
if (queue = = NULL)
{
return-1;
}
return (Queue->front = = NULL? 1:0);
}

int En_linkqueue (linkqueue *queue,data_t x)//Insert data into the queue, in two cases, you need to determine whether there is data in the original queue, and then make different insertions according to different circumstances.
{Note here: You need to modify the tail pointer after inserting the data, and you need to modify the first pointer when you insert the data for the first time.
queuelist_t node_new;

if (queue = = NULL)
{
return-1;
}

Node_new = (queuelist_t) malloc (sizeof (queuenode_t));

if (node_new = = NULL)
{
return-1;
}

Node_new->data = x;
Node_new->next = NULL;

if (Queue->front = = NULL)//No data was originally in the queue
{
Queue->front = Queue->rear = node_new;
}
else//previously available data in queue
{
Queue->rear->next = node_new; Inserting data to the end of a queue
Queue->rear = node_new; Modify the tail pointer to point to the last inserted data
}

return 0;
}

int De_linkqueue (linkqueue *queue,data_t *x)//Fetch data from the queue: according to the data after the queue is empty divided into two operations, respectively, modify the team head or the first pointer!!!
{
queuelist_t Node_remove;
if (queue = = NULL | | queue->front = = NULL)
{
return-1;
}

Node_remove = queue->fornt;
Queue->front = node_remove->next;

if (Queue->front = = null)//The queue is empty after the data has been taken, the tail pointer needs to be modified
{
Queue->rear = NULL; Need to modify the pointer to the tail
}

if (x)
{
*x = node_remove->data;
}

Free (node_remove);

return 0;
}

Implementing stacks and queues with arrays and linked lists, respectively

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.