step3. team day4 stack and linear table data structures

Add: circular list beginner may not understand, in addition to multi-drawing, to imagine an infinite circular list of unidirectional (or bidirectional) list, each element is an intermediate element, it is better to understand.

1. Stack are two special teams and management logic linear table, both the linear form

2. The principle of the stack is a first in, FILO (first in last out), similar to the barrels of biscuits, is first removed and finally loaded spent, and can only push the stack popping operation

3. The team principle is first in first out FIFO (first in first out), similar to water, the water is flowing first into the water, can only be inserted in the tail, head of the queue delete operations.

 

① sequential storage structure stack implementation, and the order of the list, like, just need to follow the rules of the stack

② linked list structure to achieve stack


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


DataType int typedef;
typedef struct {Stack
DataType Data;
struct Stack * Next; // stack position
} linkstack;

// Create an empty stack 1
linkstack linkstack_create * ()
{
linkstack S * = NULL;
S = (linkstack *) the malloc (the sizeof (linkstack));

s->next = NULL;

return s;
}

2 // stack push operation.
Void linkstack_push (linkstack S *, int value)
{
linkstack linkstack_create * TEMP = ();
temp-> Data = value;

while(s->next != NULL){ s = s->next;}

temp->next = s->next;
s->next = temp;
}


Analyzing the stack is empty //
int linkstack_is_empty (linkstack S *)
{
return S-> Next == NULL. 1: 0;?
}

//4.出栈 弹栈
int linkstack_pop(linkstack *s)
{
if(linkstack_is_empty(s) == 1)
{
printf("stack is empty.\n");
return -1;
}

int value;
linkstack * temp;
while(s->next->next != NULL){ s = s->next;}
value = s->next->data;
temp = s->next;
s->next = NULL;
free(temp);
temp = NULL;

return value;
}

//打印栈里面的所有值
int linkstack_show(linkstack *s)
{
if(linkstack_is_empty(s) == 1)
{
printf("stack is empty.\n");
return -1;
}
while(s->next !=NULL){
printf("%d ",s->next->data);
s = s->next;
}
printf("\n");
return 0;
}

int main(int argc, const char *argv[])
{
linkstack *s = NULL;
s = linkstack_create();

linkstack_push(s,1);
linkstack_push(s,2);
linkstack_push(s,3);
linkstack_push(s,4);
linkstack_push(s,5);
linkstack_push(s,6);

linkstack_show(s);

printf("%d \n",linkstack_pop(s));
linkstack_show(s);
return 0;
}

③ sequentially storing the team achieved, taking mutual I need to know the relationship between the tail and head of the queue values, to achieve the cycle queue


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

#define N 32

typedef struct queue{
int date[N];
int front;
int rear;
}sequeue;

sequeue * sequeue_creat(){
sequeue * q = NULL;
q = (sequeue*)malloc(sizeof(sequeue));

q->front = 0;
q->rear = 0;
return q;
}

int sequeue_is_full(sequeue *q){
return (q->rear + 1) % N == q->front ? 1 : 0;
}

int sequeue_is_empty(sequeue *q){
return q->rear == q->front ? 1 : 0;
}

int sequeue_input(sequeue *q,int value){
if(sequeue_is_full(q)){
printf("sequeue_is_full\n");
return -1;
}

q->date[q->rear] = value;
q->rear = (q->rear + 1) % N;
return 0;
}

int sequeue_output(sequeue * q){
if(sequeue_is_empty(q)){
printf("sequeue_is_empty\n");
return -1;
}
int value;
value = q->date[q->front];
q->front = (q->front+1) % N;
return value;
}

void sequeue_show(sequeue* q){

int i = 0;
for(i = q->front;i != q->rear;i = ((i+1) % N)){
printf("%d ",q->date[i]);
}
printf("\n");

}

 


int main(int argc, const char *argv[])
{

sequeue *q =NULL;
q = sequeue_creat();

sequeue_input(q,1);
sequeue_input(q,2);
sequeue_input(q,3);
sequeue_input(q,4);
sequeue_input(q,5);
sequeue_input(q,6);

sequeue_show(q);

printf("%d\n",sequeue_output(q));
sequeue_show(q);

return 0;
}

③ storage forms of implementing the list of team, and the like singly linked list, the pointer to keep the front and priv


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

// Force members structure
typedef struct {linkqueue
int DATE;
struct * Next linkqueue;
} linkqueue;

// Force head and tail pointers structure
typedef struct {lnqueue
struct linkqueue * Front;
struct linkqueue * PRIV;
} lnqueue;

// Create the node function
linkqueue linkqueue_creat * () {
linkqueue head * = NULL;
head = (linkqueue *) the malloc (the sizeof (linkqueue));

head-> Next = NULL;
return head;
}

// pointer queue head and team created
lnqueue lnqueue_creat * () {
linkqueue head * = NULL;
head = linkqueue_creat ();
lnqueue * LN = NULL;
LN = (lnqueue *) the malloc (the sizeof (lnqueue));

ln->front = head;
ln->priv = head;
}

// inserted into the tail
void lnqueue_input (lnqueue * ln, int value) {

linkqueue * temp =NULL;
temp = linkqueue_creat();
temp->date = value;

temp->next = ln->priv->next;
ln->priv->next = temp;
ln->priv = temp;
}

// null bit is determined whether team
int lnqueue_is_empty (lnqueue * LN) {
return of ln-> Front of ln-==> PRIV. 1:? 0;
}

//遍历
int lnqueue_show(lnqueue * ln){
if(lnqueue_is_empty(ln)){
printf("lnqueue_is_empty\n");
return -1;
}
linkqueue *temp = ln->front;
while(temp->next != NULL){
printf("%d ",temp->next->date);
temp = temp->next;
}
printf("\n");
return 0;
}

//出队 头删
int lnqueue_output(lnqueue * ln){
if(lnqueue_is_empty(ln)){
printf("lnqueue_is_empty\n");
return -1;
}

linkqueue * temp =NULL;
int value;

temp = ln->front->next;
value = temp->date;
ln->front->next = temp->next;
if(ln->priv == temp){
ln->priv = ln->front;
}
free(temp);
temp = NULL;

return value;


}

 

int main(int argc, const char *argv[])
{
lnqueue * ln = NULL;
ln = lnqueue_creat();

lnqueue_input(ln,1);
lnqueue_input(ln,2);
lnqueue_input(ln,3);
lnqueue_input(ln,3);
lnqueue_input(ln,3);
lnqueue_input(ln,6);

lnqueue_show(ln);

printf("%d\n",lnqueue_output(ln));
lnqueue_show(ln);

printf("%d\n",lnqueue_output(ln));
lnqueue_show(ln);

printf("%d\n",lnqueue_output(ln));
lnqueue_show(ln);

printf("%d\n",lnqueue_output(ln));
lnqueue_show(ln);

printf("%d\n",lnqueue_output(ln));
lnqueue_show(ln);

printf("%d\n",lnqueue_output(ln));
lnqueue_show(ln);

printf("%d\n",lnqueue_output(ln));
lnqueue_show(ln);
return 0;
}

 

Guess you like

Origin www.cnblogs.com/huiji12321/p/11233942.html