Underlying data structure list, stack, queue

Source: Internet
Author: User
Tags int size

Data structure is a very important part of the program design, the basic information structures include lists, stacks and queues, of course, the higher level of the tree, graph, and so on, in fact, the list, stack and queue are linear tables, only in the operation and representation of the different, linear table with the order of the structure is a sequential table, chain structure is linked list If you restrict the operation of a linear table, you can only insert and delete elements at the end of the table, which becomes a stack, and if you can only allow the elements to be inserted from the end of the table and the header is deleted, it becomes a queue.


Linked list

/* * Data structure linked list * chain structure */#include <iostream> using namespace std;

Enum Status {ERROR =-1, OK = 0};
typedef int ELEMTYPE;
	Defines the node of the linked list typedef struct LNODE {elemtype data;
Lnode *next;


} Lnode, *linklist; /* Create a single linked list with length size and return the link header node * from the previous insert new element, each time the new element is added to the end of the head node */linklist createlist (int size) {if (size < 0) Retu

	RN NULL;
	Define head node Linklist list = new Lnode[1];

	List->next = NULL;
		for (int i=size; i>=1; i--) {Lnode *node = new Lnode[1];
		cin>>node->data;
		Node->next = list->next;
	List->next = node;
} return list;
	}/* * Take the POS element from the list of lists and pay the value of the element to Elem */Status Getelem (linklist list, int pos, elemtype &elem) {if (! List)
	{return status::error;
	} Lnode *p = list->next;

	int j = 1;
		while (P && j<pos) {p = p->next;
	j + +;
	}//To determine if there is a POS element, or if the POS value is entered in a non-method (negative or 0) if (! P | | J > POS) {return status::error;
	} Elem = p->data;
return Status::ok; }/* * Insert element Elem into the list of linked listPOS position in * To insert an element in POS position, you need to find the * pos-1 Location First * * * Status insertlist (linklist list, int pos, elemtype elem) {Lnode *p =
	List
	int j = 0;
		while (P && j<pos-1) {p = p->next;
	j + +;
	} if (! p | | J > pos-1) {return status::error;
	} lnode *node = new Lnode[1];
	Node->data = Elem;
	Node->next = p->next;

	P->next = node;
return Status::ok; 
}/* * Remove the POS element from the list of lists and assign the element value to Elem * First FIND element pos-1 * * Status deletelist (linklist list, int pos, elemtype &elem)
	{Lnode *p = list;
	int j = 0;
		Find the Pos-1 element while (P && j<pos-1) {p = p->next;
	j + +;
	} if (! p | | j>pos-1 | |! p->next) {return status::error;
	} Lnode *q = p->next;
	Elem = q->data;
	P->next = q->next;

	Delete q;
return Status::ok; }/* * Merges the linked list List1 and List2, and returns the merged list header nodes * List1 and List2 are in non-descending order by value */linklist mergelist (linklist list1, linklist list2) {Li

	Nklist list;
		if (! List1 | |! list2) {list = List2==null? list1:list2; ReTurn list;
	
	} list = List1;
	linklist p = list, p1 = list1->next, p2 = list2->next;
			while (P1 && p2) {if (P1->data <= p2->data) {p->next = P1;
		P1 = p1->next;
			} else {p->next = p2;
		P2 = p2->next;
	} p = p->next; } P->next = P1?
	P1:P2;

	Delete List2;
return list;

	} void Printlist (linklist list, char *str= "") {cout<<str<<endl;

	if (list = = NULL) return;
	Lnode *p = list->next;
		while (p) {cout<<p->data<< "";
	p = p->next;
} cout<<endl;
	} void Destroylist (linklist list) {if (! list) {return;
		} while (List->next) {Lnode *p = list->next;
		List->next = p->next;
	Delete p;
} Delete list; }

Stack

/* Data structure stack * sequential structure * * #include <iostream> #include <cstdlib> using namespace std;

#define STACK_INIT_SIZE #define Stack_increment enum Status {ERROR =-1, OK = 0, TRUE = 1, FALSE = 2};

typedef int ELEMTYPE;

The typedef struct Stack {Elemtype *top;//points to the previous position of the top element of the stack elemtype *base;//a stack that points to the length of the base element int size;//stack space}; /* Initialize stack operation */Status Initstack (stack* &stack) {//The formal parameter here must use a reference to the pointer, as it may change the value of the pointer itself, not the value of the object it points to if (! Stack) {stack
	= (stack*) malloc (sizeof (Stack));
	} stack->base = (elemtype*) malloc (stack_init_size * sizeof (elemtype));
	if (! Stack->base) {return status::error;
	} stack->top = stack->base;

	Stack->size = stack_init_size;
return Status::ok;
		}/* Gets the top element of the stack */Status getTop (Stack *stack, Elemtype &elem) {if (! Stack | | stack->base = = stack->top) {
	return status::error;
	} elem = * (stack->top-1);
return Status::ok; }/* * Press the new element into the top of the stack * * Status push (Stack *stack, elemtype elem) {if (! Stack)
	{return status::error; }//First determine if the stack is full if (stack->top-stack->base >= stack->size) {stack->base = (elemtype*) realloc (stack->
		Base, (stack->size+stack_increment) *sizeof (elemtype));
		if (! Stack->base) {return status::error;
		} stack->top = Stack->base + stack->size;
	Stack->size + = stack_increment;
	} *stack->top = Elem;

	stack->top++;
return Status::ok; }/* * Determine if the stack is empty */Status isEmpty (Stack *stack) {if (! Stack | | stack->base = = stack->top) {return status::t
	RUE;
} return status::false; }/* Popup top element */Status Pop (stack* stack, elemtype &elem) {if (IsEmpty (Stack) = = Status::true) {return status
	:: ERROR;
	} stack->top--;

	Elem = *stack->top;
return Status::ok;

	} Status destroystack (Stack *stack) {if (! stack) return status::ok;
		if (stack->base) {free (stack->base);
	Stack->base = Stack->top = NULL;
} return Status::ok; } void Printstack (Stack *stack, char *str = "") {if (isEmpty (stack) = = status::true) {return;
	} cout<<str<<endl;
	Elemtype *p = stack->base;
		while (P! = stack->top) {cout<<*p<< "";
	p++;
} cout<<endl; }

Queue

/* * Data structure queue * chain structure */#include <iostream> using namespace std;

Enum Status {ERROR =-1, OK = 0, TRUE = 1, FALSE = 2};

typedef int ELEMTYPE;
	typedef struct QNODE {elemtype data;
Qnode* Next;

} Qnode;
	typedef struct LINKQUEUE {qnode* front;
qnode* Rear;

} Linkqueue; /* * Initialize Queue * * Status initqueue (linkqueue* &queue) {if (! queue) {queue = (linkqueue*) malloc (sizeof (Linkqueue))
	;
	} Queue->front = Queue->rear = (qnode*) malloc (sizeof (Qnode));
	if (! Queue->front) {return status::error;
	
	} queue->front->next = NULL;
return Status::ok;
	}/* * Queue * * Status enQueue (linkqueue* queue, elemtype elem) {if (! Queue) {return status::error;
	} qnode* node = (qnode*) malloc (sizeof (Qnode));
	if (! Node) {return status::error;
	} node->data = Elem;
	Node->next = NULL;
	Queue->rear->next = node;

	queue->rear = node;
return Status::ok; }/* * Determine if the queue is empty */Status IsEmpty (linkqueue* queue) {if (! queue | | qUeue->front = = queue->rear) return status::true;
return status::false; }/* * out of Team * * Status deQueue (linkqueue* queue, Elemtype &elem) {if (isEmpty (queue) = = Status::true) {return St
	Atus::error;
	} qnode* p = queue->front->next;
	Elem = p->data;
	Queue->front->next = p->next;
	if (queue->rear = = p) queue->rear = queue->front;

	Free (p);
return Status::ok;

	}/* * Destroy queue * * Status destroyqueue (linkqueue* queue) {if (! Queue) return Status::ok;
		while (queue->front) {queue->rear = queue->front->next;
		Free (Queue->front);
	Queue->front = queue->rear;
} return Status::ok;
	} void PrintQueue (linkqueue* queue, char* str) {if (isEmpty (queue) = = Status::true) {return;

	} cout<<str<<endl;
	qnode* p = queue->front->next;
		while (p) {cout<<p->data<< "";
	p = p->next;
} cout<<endl; }


Since the queue is to remove elements from the team header, inserting elements from the end of the queue, after deleting the elements, it is not possible to let all the elements in the queues move forward, only to shrink the team head pointer back one position, the front empty space, in order to use the space, you need to use the loop queue, the team head and the end of the Such a real team head is not necessarily the order of the application to the first address of the space, the tail is not necessarily the end of the order space, so after the loop queue is full, you cannot use realloc to reallocate new space use, because this will break the original queue structure, The new queue space may be inserted anywhere in the queue, and the tail-to-kinsoku operation of the queue is also not possible, so you can only set a maximum queue length for the loop queue, cannot be allocated dynamically, and if the maximum length is indeterminate, it is better to use a chained queue.


/* * Data structure queue * sequential structure, cyclic queue */#include <iostream> using namespace std;

#define MAXSIZE enum Status {ERROR =-1, OK = 0, TRUE = 1, FALSE = 2};

typedef int ELEMTYPE;
	typedef struct CIRCLEQUEUE {elemtype* base;
	int front;
int rear;

} Circlequeue; /* * Initialize Queue * * Status initqueue (circlequeue* &queue) {if (! queue) {queue = (circlequeue*) malloc (sizeof (Circleq
	Ueue));

	} if (! Queue) return status::error;
	Queue->base = (elemtype*) malloc (MAXSIZE * sizeof (elemtype));
	if (! Queue->base) {return status::error;

	} queue->front = Queue->rear = 0;
return Status::ok;
	}/* Gets the length of the queue */int getlength (circlequeue* queue) {if (! Queue) {return 0;
} return (Queue->rear-queue->front + MAXSIZE)% MAXSIZE; }/* * Queue * * Status enQueue (circlequeue* queue, elemtype elem) {//reserve a space without elements to distinguish the queues full and empty flag if (! Queue | |
	(queue->rear+1)%maxsize = = Queue->front) {return status::error; } Queue->base[queue->rear] = EleM

	Queue->rear = (queue->rear + 1)% MAXSIZE;
return Status::ok; }/* * Determine if the queue is empty */Status IsEmpty (circlequeue* queue) {if (! queue | |! queue->base | | queue->front = QUEUE-&G
	T;rear) {return status::true;
} return status::false; }/* * out of Team * * Status deQueue (circlequeue* queue, Elemtype &elem) {if (isEmpty (queue) = = Status::true) {return
	Status::error;
	} Elem = queue->base[queue->front];

	Queue->front = (queue->front + 1)% MAXSIZE;
return Status::ok;
	}/* * Destroy queue * * Status destroyqueue (circlequeue* queue) {if (! Queue) {return status::ok;
		} if (queue->base) {free (queue->base);
	Queue->base = NULL;
} return Status::ok;

	} void PrintQueue (circlequeue* queue, char* str) {if (isEmpty (queue) = = Status::true) return;

	cout<<str<<endl;
	int pos = queue->front;
		while (pos! = queue->rear) {cout<<queue->base[pos]<< "";
	pos = (pos+1)%maxsize;
} cout<<endl; }



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.