Basic Data Structure linked list, stack, queue

Source: Internet
Author: User

Data structure is a very important part in programming. The basic data structure includes linked list, stack and queue. Of course, tree and graph are also advanced, in fact, the linked list, stack, and queue are both linear tables, but they are different in operation and representation. A linear table is represented by a sequential table in a sequential structure and a chain table in a chain structure, if you restrict operations on a linear table, you can only insert and delete elements at the end of the table, which becomes a stack. If you can only Insert elements from the end of the table and delete the header, this 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; // define the node typedef struct lnode {elemtype data; lnode * Next;} lnode, * linklist;/** to create a single-chain table with a length of size, return the head node * of the linked list and insert new elements from the beginning. Each new element is added to the end of the header node */linklist createlist (INT size) {If (size <= 0) return NULL; // define the first node linklist list = new lnode [1]; List-> next = NULL; For (INT I = size; I> = 1; I --) {lnode * node = N EW lnode [1]; CIN> node-> data; node-> next = List-> next; List-> next = node;} return list ;} /** get the POs element from the list of linked lists and pay the element value 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 ++;} // determines whether the second POS element exists, or the POs value input is illegal (negative value or 0) if (! P | j> POS) {return status: error;} ELEM = p-> data; return status: OK ;} /** Insert the element ELEM to the position POs in the list of linked lists * to insert the element at the position POs, first find the * pos-1 location */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;}/** Delete the POs element from the list of linked lists, and assign this element value to ELEM * first find the first pos-1 element */status deletelist (linklist list, int POs, elemtype & ELEM) {lnode * P = List; int J = 0; // locate 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;}/** merge linked lists list1 and list2, return the merged linked list header nodes * list1 and list2 are arranged in a non-descending order of values */linklist mergelist (linklist list1, linklist list2) {linklist 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 * Sequence Structure */# include <iostream> # include <cstdlib> using namespace STD; # define stack_init_size 100 # define stack_increment 10 Enum status {error =-1, OK = 0, true = 1, false = 2}; typedef int elemtype; typedef struct stack {elemtype * Top; // point to elemtype * base at the previous position of the element on the top of the stack; // point to the int size of the element at the bottom of the stack; // stack space length} stack; /** initialize stack operation */status initstack (stack * & stack) {// the reference of the pointer must be used for the parameter, because the value of the pointer may be changed, instead of pointing to the object value 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 ;} /** get the top stack element */status gettop (stack * stack, elemtype & ELEM) {If (! Stack | stack-> base = stack-> top) {return status: error;} ELEM = * (Stack-> top-1); Return status :: OK;}/** press new elements to the top of the stack */status push (stack * stack, elemtype ELEM) {If (! Stack) {return status: error;} // first judge whether 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;}/** determines whether the stack is empty */status isempty (stack * stack) {If (! Stack | stack-> base = stack-> top) {return status: true;} return status: false ;} /** top stack 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 the 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 whether the queue is empty */status isempty (linkqueue * Queue) {If (! Queue | queue-> front = queue-> rear) return status: true; return status: false;}/** team out */status dequeue (linkqueue * queue, elemtype & ELEM) {If (isempty (Queue) = Status: True) {return status: 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 ;}

/** Data Structure queue * sequence structure, cyclic queue * because the queue deletes elements from the queue header and inserts elements from the end * after the elements are deleted, it is impossible to move all the elements in the queue forward. * only one position of the head pointer of the queue can be reduced backward. The space is left blank. To use the space, * the cyclic queue is used, connect the team head and the team end. In this way, the real team head is not necessarily the first address of the requested Ordered Space. The team end is not necessarily the end address of the ordered space, therefore, after the cyclic queue is full, * you cannot use realloc to allocate new space, because this will break the original queue structure, the new queue space may be inserted to any position in the queue, and operations on the queue's head and end cannot be performed. Therefore, you can only set a maximum queue length * For the cyclic queue and cannot dynamically allocate it, if the maximum length is not determined, the chain queue is better */# include <iostream> using namespace STD; # define maxsize 100 Enum status {error =-1, OK = 0, True = 1, false = 2}; typedef int elemtype; typedef struct circlequeue {elemtype * base; int front; int rear;} circlequeue; /** initialize the queue */status initqueue (circlequeue * & Queue) {If (! Queue) {queue = (circlequeue *) malloc (sizeof (circlequeue);} 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 ;} /** get queue length */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 element to distinguish the sign of full queue and empty 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 whether the queue is empty */status isempty (circlequeue * Queue) {If (! Queue |! Queue-> base | queue-> front = queue-> rear) {return status: true;} return status: false ;} /** departure */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 ;}



Basic Data Structure linked list, stack, queue

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.