Code of Data Structure

Source: Internet
Author: User

1. linked list Code

#include <stdio.h>#include <stdlib.h>typedef struct Node{    int data;    struct Node *next;}LinkNode,*List,*Position;List init(){    List s;    s = (List)malloc(sizeof(LinkNode));    s->data = 0;    s->next = NULL;    return s;}void destory(List s){    List p;    p = s;    while(p->next)    {        s = s->next;        free(p);        p = s;    }}void insert(List s,int pos,int e){    int i = 1;    //s = s->next;    while(s&&(i!=pos))    {        s = s->next;        i++;    }    List p = (List)malloc(sizeof(LinkNode));    p->data = e;    p->next = s->next;    s->next = p;}void del(List s,int pos,int e){    int i = 1;    //s = s->next;    while(s&&(i!=pos))    {        s = s->next;        i++;    }    List p = s->next;    s->next = p->next;    free(p);}void show(List s){    s = s->next;    while(s)    {        printf("%d",s->data);        s = s->next;    }}int main(){    List s = init();    insert(s,1,5);    insert(s,2,6);    insert(s,3,7);    insert(s,4,9);    insert(s,5,34);    show(s);    return 0;}

2. Linked List Operation

# Include <stdio. h ># include <iostream> typedef int elemtype; typedef struct lnode {elemtype data; struct lnode * Next;} lnode, * linklist; void createlist (linklist & L, int N) {L = (linklist) malloc (sizeof (lnode); L-> next = NULL; For (INT I = N; I> 0; -- I) {linklist P = (linklist) malloc (sizeof (lnode); printf ("input elements inserted into the linked list \ n"); scanf ("% d ", & P-> data); P-> next = L-> next; L-> next = P;} int getelem (linklist & L, int I) {linklist P = L-> next; int J = 1; while (P & J <I) {P = p-> next; ++ J;} If (! P & J> I) Return-1; int e = p-> data; printf ("the % d element obtained is % d \ n", I, e ); return 1;} int insertlist (linklist & L, int I, int e) {linklist P = L; Int J = 0; while (P & J <i-1) {P = p-> next; ++ J;} If (! P & J> I) Return-1; linklist S = (linklist) malloc (sizeof (lnode); s-> DATA = E; s-> next = p-> next; P-> next = s; printf ("insert element LOCATION % d, insert element % d \ n", I, e ); return 1;} int deletelist (linklist & L, int I, int e) {linklist P = L, Q; Int J = 0; while (P & J <i-1) {P = p-> next; ++ J;} If (! P & J> i-1) Return-1; q = p-> next; P-> next = Q-> next; E = Q-> data; printf ("delete element LOCATION % d, delete element % d \ n", I, e); free (Q); return 1;} void length (linklist & L) {int I = 0; linklist P = L-> next; while (p) {++ I; P = p-> next ;} printf ("Length: % d \ n", I);} void desplay (linklist & L) {linklist P = L-> next; while (P) {printf ("the element in the linked list is % d \ n", p-> data); P = p-> next ;}} // to merge two linked lists, enter void mergelist (linklist & la, linklist & Lb, linklist & lc) in reverse order {linklist Pa = La-> next; linklist Pb = LB-> next; linklist Pc = La; lc = pc; while (PA & Pb) {If (Pa-> data <Pb-> data) {PC-> next = PA; Pc = PA; Pa => next;} else {PC-> next = Pb; Pc = Pb; PB = Pb-> next ;}} PC-> next = pa? PA: Pb; printf ("merged linked list \ n"); free (LB);} // reverse a single-chain table void reverselink (linklist & L) // a bit of problem {If (L = NULL) Exit (-1); linklist curr, Prev = NULL, temp; curr = L; while (curr-> next) {temp = curr-> next; curr-> next = Prev; Prev = curr; curr = temp;} curr-> next = Prev; desplay (curr ); free (temp) ;}void main () {linklist L; linklist LB, LC; int n = 0; printf ("input the number of elements inserted to the linked list \ n "); scanf ("% d", & N); createlist (L, n); printf ("number of elements inserted into the linked list \ n"); scanf ("% d ", & N); createlist (LB, n); mergelist (L, LB, LC); desplay (LC); printf ("reverse a single-chain table \ n "); reverselink (l); insertlist (L, 1, 1); insertlist (l, 2, 3); insertlist (L, 3, 5); deletelist (L, 1, 1); getelem (L, 1 ); desplay (l); length (L); System ("pause ");}

3. Sequence Table

# Include <stdio. h >#include <iostream> typedef int elemtype; # define list_inin_size 100 # define list_increase 10 typedef struct {elemtype * ELEM; int length; int listsize;} sqlist; int initlist (sqlist & L) {L. ELEM = (elemtype *) malloc (sizeof (elemtype) * list_inin_size); If (! L. ELEM) Exit (-1); L. length = 0; L. listsize = list_inin_size; return 1;} int insertlist (sqlist & L, int I, elemtype e) {if (I <1 | I> L. length + 1) Return-1; if (I> = L. listsize) {elemtype * newbase = (elemtype *) realloc (L. ELEM, (L. listsize + list_increase) * sizeof (elemtype); If (! Newbase) Exit (-1); L. ELEM = newbase; L. listsize + = list_increase;} elemtype * P, * q; q = & L. ELEM [I-1]; for (P = & L. ELEM [L. length-1]; P> = Q; -- p) * (p + 1) = * P; * q = E; ++ L. length; printf ("insert position % d, insert element % d, linear table length % d \ n", I, e, L. length); return 1;} int deletelist (sqlist & L, int I, elemtype e) {if (I <1 | I> L. length) Return-1; elemtype * P, * q; P = & L. ELEM [I-1]; E = * P; q = L. ELEM + L. length-1; for (+ + P; P <q; ++ p) * (p-1) = * P; -- l. length; printf ("delete LOCATION % d, delete element % d, linear table length % d \ n", I, e, L. length); return 1;} void main () {sqlist L; initlist (l); insertlist (L, 1, 1); insertlist (l, 2, 2); insertlist (L, 3, 3); deletelist (L, 1, 1); deletelist (l, 2, 2); System ("pause ");}

4. Stack

# Include <stdio. h ># include <iostream> typedef int elemtype; # define stack_inin_size 100 # define stack_increase 10 typedef struct {elemtype * base; elemtype * Top; int stacksize;} sqstack; int initstack (sqstack & S) {S. base = (elemtype *) malloc (sizeof (elemtype) * stack_in_size); If (! S. base) Return-1; S. top = S. base; S. stacksize = stack_inin_size; return 1;} void gettop (sqstack s) {If (S. top = S. base) Exit (-1); int e = * (S. top-1); printf ("top stack element % d \ n", e);} void push (sqstack & S, int e) {If (S. top-S.base> = stack_in_size) {S. base = (elemtype *) realloc (base, sizeof (elemtype) * (stack_in_size + S. stacksize); S. top = S. stacksize + S. base; S. stacksize + = stack_increase;} * s. top ++ = E; printf ("element % d into Stack \ n", e);} void POP (sqstack & S) {If (S. top = S. base) Exit (-1); int e = * -- S. top; printf ("element % d output stack \ n", e);} void main () {sqstack s; initstack (s); push (S, 0 ); push (S, 2); push (s, 3); POP (s); gettop (s); System ("pause ");}

5. Queue

# Include <stdio. h ># include <iostream> typedef int elemtype; typedef struct qnode {struct qnode * Next; elemtype data;} qnode, * queueptr; typestrudef CT {queueptr front; queueptr rear ;} linkqueue; void initqueue (linkqueue & Q) {q. front = Q. rear = (queueptr) malloc (sizeof (qnode); If (! Q. front) Exit (-1); q. front-> next = NULL;} int enqueue (linkqueue & Q, int e) {queueptr P = (queueptr) malloc (sizeof (qnode); If (! P) Return-1; p-> DATA = E; P-> next = NULL; q. rear-> next = p-> next; q. rear = P; printf ("element % d into the queue \ n", e); return 1;} int dequeue (linkqueue & Q, int e) {If (Q. front = Q. rear) Return-1; queueptr P = Q. front-> next; q. front-> next = p-> next; // If (Q. rear = P) Q. rear = Q. front; printf ("element % d team lead \ n", e); free (p); return 1;} void main () {linkqueue Q; initqueue (Q ); enqueue (Q, 1); enqueue (Q, 2); dequeue (Q, 1); System ("pause ");}

6. Binary Tree

#include <iostream>using namespace std;typedef char EmleType;typedef struct BitNode{EmleType data;struct BitNode *rchild,*lchild;}BitNode,*BiTree;void CreateTree(BiTree &T){char ch;//abc  de  g  fscanf("%c",&ch);if(ch=='*')T=NULL;else{T=(BiTree)malloc(sizeof(BitNode));if(!T)exit(-1);T->data=ch;CreateTree(T->lchild);CreateTree(T->rchild);}}void PreOrder(BiTree T){if(T){printf("%c",T->data);PreOrder(T->lchild);PreOrder(T->rchild);}}void InOrder(BiTree T){if(T){PreOrder(T->lchild);printf("%c",T->data);PreOrder(T->rchild);}}void PosOrder(BiTree T){if(T){PreOrder(T->lchild);PreOrder(T->rchild);printf("%c",T->data);}}void main(){BiTree T;CreateTree(T);PreOrder(T);InOrder(T);PosOrder(T);system("pause");}

7. Circular linked list judgment

# Include <stdio. h> # include <iostream> typedef struct lnode // judge whether the linked list has a ring {char data; struct lnode * Next;} * link; int linkcircle (link head) {link P = head; Link p1 = head-> next; If (Head = NULL | head-> next = NULL) return 0; if (Head = head-> next) return 1; while (P! = P1 & P! = NULL & P1-> next! = NULL) {P = p-> next; P1 = p1-> next;} If (P = p1) return 1; else return 0 ;} void main () {link P1, P2, P3, P4; P1 = (Link) malloc (sizeof (lnode); P2 = (Link) malloc (sizeof (lnode )); p3 = (Link) malloc (sizeof (lnode); P4 = (Link) malloc (sizeof (lnode); P1-> next = P2; P2-> next = P3; p3-> next = P4; P4-> next = p1; If (linkcircle (P1) printf ("is circle \ n "); elseprintf ("is not circle \ n"); System ("pause ");}

8. Determine whether two single-chain tables are crossover and locate the intersection.
Question 1: How do I determine whether two single-chain tables (both single-chain tables have no rings) are cross-linked?
The first link is traversed to the end, and the node pointer is P1. The second link is also traversed to the end, and the node pointer is P2. If P1 and P2 are equal at this time, the two are single-stranded and cross-linked. The function can be written as follows:

#include <stdio.h>struct LNode{int data;struct LNode *next;}; void IsCross(LNode *head1,LNode *head2){LNode *p1=head1->next;while(p1->next){p1=p1->next;}LNode *p2=head2->next;while(p2->next){p2=p2->next;}if(p1=p2){printf("Two link lists intersecting.");}else{printf("Two link lists are not intersecting.");}}
Question 2: How can I find the intersection if it is crossover? If the longer-chain table is head1. Set the two pointers P1 and P2 to traverse the linked lists head1 and head2 from the beginning. The step size is 1. Let P1 move nlen1-nlen2 first, and then let P1 and P2 move at the same time, the intersection of P1 and P2 is the intersection. The function can be written as follows:
LNode *findCross(LNode *head1, int nlen1,LNode *head2, int nlen2){LNode *p1=head1, *p2=head2;int diffLen = nlen1-nlen2;if(diffLen>0){while(diffLen>0){p1=p1->next;diffLen--;}}else{while(diffLen<0){p2=p2->next;diffLen--;}}while(p1!=p2){p1=p1->next;p2=p2->next;}return p1;}

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.