Some of the C language data structure codes that can be run

Source: Internet
Author: User
Tags clear screen

There are many C language data structure code on the net, some can't run, and some can run and run;


1 queues

#include <stdio.h> #include <stdlib.h> #define queue_size typedef struct SEQQUEUE {int data[queue_s      IZE];      int front;    int rear;    }queue;      Queue *initqueue () {Queue *q = (Queue *) malloc (sizeof (queue));          if (q = = NULL) {printf ("Malloc failed!\n");      Exit (-1);      } q->front = 0;        q->rear = 0;  return q;  } int Isfull (Queue *q) {return ((q->rear+1)%queue_size = = Q->front);  } int IsEmpty (Queue *q) {return (Q->front = = q->rear);      } void Enqueue (Queue *q,int N) {if (Isfull (q)) {return;      } Q->data[q->rear] = n;  Q->rear = (q->rear+1)%queue_size;      } int Dequeue (Queue *q) {if (IsEmpty (q)) {return 0;      } int tmp = q->data[q->front];      Q->front = (q->front+1)%queue_size;  return TMP;      } void Main () {Queue *q = Initqueue ();      int i; for (i=0;i<10;i++) {          Enqueue (Q,i*i); } while (!          IsEmpty (q)) {int data = Dequeue (q);      printf ("%d-->", data); }  }



2 stacks

#include <stdio.h> #include <stdlib.h> #include <malloc.h>//define a node's structure typedef struct node{int Membe            R            Data domain struct node * pnext;//pointer field}node,*pnode;//defines a stack structure typedef struct stack{Pnode Top;        Stack top Pnode Bottom;        Stack bottom}stack,* pstack;void initstack (pstack);            Initialize the stack's function bool Push (pstack, int);    function void Traversestack (pstack) for the stack operation;            Traversing the Stack function bool Empty (pstack);                the function int Pop (pstack) that determines whether the stack is empty;            function void Clear (pstack) for the stack operation;                        Empty stack's function int main (void) {stack s;    Define a stack int i;    int num;                        int data;                        Temporarily save the data entered by the user int re_num;    Save the return value of the pop function initstack (&s);    printf ("You want to enter a few data ah:");    scanf ("%d", &num);        for (i = 0;i < num;i++) {printf ("Number of%d:", i+1);        scanf ("%d", &data);        if (push (&s,data))//Call the Push function{continue; } else {printf ("failed in the stack operation!            \ n ");        Exit (-1);                }} traversestack (&s);    Call the Traversal function printf ("You want to get rid of several numbers AH:");    scanf ("%d", &data);    printf ("The number you removed is:");            for (i = 0; i < data;i++) {Re_num = Pop (&s);        Call the POP function and assign the return value to Re_num;    printf ("%d", re_num);    } printf ("See what else after deletion:");    Traversestack (&s);    printf ("\ n");                        Clear (&s); Call Empty stack function printf ("traverse the stack to see if it clears    \ n ");    Traversestack (&s);    printf ("\ n"); return 0;}        The initialized function of the stacks is void Initstack (Pstack ps) {ps->top = (pnode) malloc (sizeof (Node));        Allocates memory space to stack top if (NULL = = ps->top) {printf ("Dynamically allocated memory failed \ n");    Exit (-1);                    } else {ps->bottom = ps->top;                    So that the bottom of the stack also points to the stack top space ps->top->pnext = NULL; The top pointer of the stack is null;} return;} The function of the stack operation BOOL Push (Pstack PS, int data) {Pnode pnew = (pnode) malloc (sizeof (Node));    Define a new node and allocate memory space if (NULL = = Pnew) {return false;                        } pnew->member = data;                        Assign the data to the stack to the member member of the new node Pnew->pnext = ps->top;                                The pointer to the new node points to the top of the stack ps->top = pnew; Put the new node as the new stack top return true;}    function of traversing stack void Traversestack (Pstack PS) {Pnode pnew = ps->top;            while (pnew!= Ps->bottom)//As long as the top of the stack is not equal to the bottom of the stack, loop {printf ("%d", pnew->member);                Print stack top member member Pnew = pnew->pnext; The top pointer of the stack moves down once} return;    Determine if the stack is empty bool empty (Pstack PS) {if (ps->top = = ps->bottom)//stack top is equal to the bottom of the stack, not the stack is not data {return true;    } else {return false;                }}//the stack operation function int Pop (pstack PS) {pnode pswap = NULL;    int return_val;    if (empty (PS))//Determine whether the stack is empty, empty can not be a stack operation {exit (-1); } Else {return_val = ps->top->member;                Assigns the value of the member member of the top of the stack to Return_val as the function return value Pswap = ps->top;        Make Pswap point to stack top ps->top = ps->top->pnext;                    Point the top of the stack to the next node on the top of the stack free (PSWAP);    Release the previous stack top space return return_val;        }}//empty stack function void Clear (Pstack PS) {pnode pnew = NULL;                    while (ps->top! = ps->bottom)//stack top and bottom stack, loop {pnew = ps->top;        Point a new node and the top of the stack to the same space ps->top = ps->top->pnext;                        Point the top of the stack to the next node on the top of the stack free (pnew); Release the previous stack top space} return;



3 Trees

#include <stdio.h> #include <stdlib.h>typedefintelemtype;//data type typedefintstatus;//return value type// Define binary tree structure typedef struct bitnode{elemtypedata;//data domain struct Bitnode*lchild, *rchlid;//left and right subtree field}bitnode, *bitree;//  First order Create two fork tree status Createbitree (Bitree *t) {elemtype ch;  Elemtype temp;  scanf ("%d", &ch);  temp = GetChar ();  if ( -1 = = ch) *t = NULL;    else {*t = (bitree) malloc (sizeof (Bitnode)); if (! (    *t)) exit (-1);    (*t)->data = ch;    printf ("Input%d left child node:", ch);    Createbitree (& (*t)->lchild);    printf ("Right child node of input%d:", ch);  Createbitree (& (*t)->rchlid); } return 1;}  First Order traversal binary tree void Traversebitree (Bitree t) {if (NULL = = t) return;  printf ("%d", t->data);  Traversebitree (T->lchild); Traversebitree (t->rchlid);}  Middle order traversal binary tree void Inorderbitree (Bitree t) {if (NULL = = t) return;  Inorderbitree (T->lchild);  printf ("%d", t->data); Inorderbitree (t->rchlid);}  Post-traversal binary tree void Postorderbitree (Bitree t) {if (NULL = = t) return; Postorderbitree (T->lchild);  Postorderbitree (T->rchlid); printf ("%d", T->data);}  Binary Tree Depth int treedeep (Bitree T) {int deep = 0;    if (T) {int leftdeep = Treedeep (t->lchild);    int rightdeep = Treedeep (t->rchlid);  Deep = leftdeep>=rightdeep?leftdeep+1:rightdeep+1; } return deep;}      Find binary tree leaf node number int leafcount (bitree t,int &num) {if (T) {if (T->lchild ==null &&t->rchlid==null)    num++;    Leafcount (T->lchild,num);  Leafcount (T->rchlid,num); } return num;}  Main function int main (void) {Bitree T;  Bitree *p = (bitree*) malloc (sizeof (bitree));  int deepth,num=0;  printf ("Please enter the value of the first node, 1 means no leaf node: \ n");  Createbitree (&t);  printf ("Sequential traversal of binary tree: \ n");  Traversebitree (T);  printf ("\ n");  printf ("middle order traversal binary tree: \ n");  Inorderbitree (T);  printf ("\ n");  printf ("Post-secondary traversal of binary tree: \ n");  Postorderbitree (T);  printf ("\ n");  Deepth=treedeep (T);  printf ("Depth of tree:%d", deepth);  printf ("\ n");  Leafcount (T,num);  printf ("Tree leaves node Number:%d", num);  printf ("\ n"); return 0;}



4 figure

#include <stdio.h> #include <malloc.h> #include <stdlib.h> #define MAX 20//Figure the maximum number of nodes that can be stored is 20;struct tnode{struct Tnode * next;//points to the next pro node int data;//stores the location of the neighboring node in the array};struct node{int valu;//The value of the storage node struct tnode * l    ink;//points to the neighboring node};struct picture{struct node Nd[max];  Declares an array of nodes int count; Number of nodes in the graph char A; The type of diagram created};struct picture * createpicture (); int search (struct picture *p,int value);//Find the position of value in the nd array void bfs (struct Picture * Q,int I,int visit[]); Breadth-first traversal of void Dfs (struct picture * q,int i,int visit[]);//depth-first traversal of void Traversedfs (struct picture *p); void Traversebfs (    struct picture *p); int main () {char A;    struct picture *p;    P=createpicture ();        while (1) {GetChar ();        printf ("Now will traverse the graph, if the use of breadth-first traversal, please enter a, if the use of depth-first traversal Please enter B, clear screen Please enter C, exit Please enter d:\n");        scanf ("%c", &a);        if (a== ' a ') {printf ("depth-first traversal follows: \ n");        TRAVERSEBFS (P);        } if (a== ' B ') {printf ("breadth-first traversal follows: \ n");     Traversedfs (P);   } if (a== ' C ') system ("CLS");    if (a== ' d ') exit (0); } return 0;}    struct picture * Createpicture () {int i,j,k,l;//l holds the position of the returned node in the array, char A;    struct picture *p;    p= (struct picture *) malloc (sizeof (struct picture));    struct tnode * t;    printf ("Enter the number of nodes in the graph you want to build and the type of the graph (a indicates that the graph B is a graph): \ n");    scanf ("%d%c", &i,&a);    p->count=i;    p->a=a;    printf ("Enter the values of each node in turn (the value of each node is lost by the return end): \ n");        for (i=0;i<p->count;i++) {scanf ("%d", &j);        p->nd[i].valu=j;    p->nd[i].link=null; } for (i=0;i<p->count;i++) {printf ("Enter the data value of the node adjacent to the node with a data value of%d (the value of each node is lost by the carriage return end), if no longer contain adjacent values, enter -1\n", P->nd[i]        . Valu);            while (1) {scanf ("%d", &k);            if (k==-1) break;            L=search (P,K);                if (l!=-1) {t= (struct tnode *) malloc (sizeof (struct tnode));                t->data=l;                t->next=p->nd[i].link;p->nd[i].link=t;            } else printf ("No This data value!\n");        GetChar (); }} return p;}    int search (struct picture *p,int value) {int i;        for (i=0;i<p->count;i++) {if (Value==p->nd[i].valu) {return i; }} return-1;}    void Traversedfs (struct picture *p) {int i;    int visit[max];//declares an array of flags whose initial value is set to 0,0 to indicate that the node has not been accessed, and that 1 indicates that the node has been accessed for (i=0;i<p->count;i++) {visit[i]=0;        } for (i=0;i<p->count;i++) {if (visit[i]==0) {DFS (p,i,visit); }}//getchar ();}    void Dfs (struct picture * q,int i,int visit[])//i indicates that the subscript value of the array visit the subscript with the subscript in P is one by one the corresponding relationship {struct tnode * w;    printf ("%d\n", Q->nd[i].valu);    Visit[i]=1;    w=q->nd[i].link;        while (W!=null) {if (visit[w->data]==0) {DFS (q,w->data,visit);        } else {w=w->next;  }}}void Traversebfs (struct picture *p) {  int i;    int visit[max];//declares an array of flags whose initial value is set to 0,0 to indicate that the node has not been accessed, and that 1 indicates that the node has been accessed for (i=0;i<p->count;i++) {visit[i]=0;        } for (i=0;i<p->count;i++) {if (visit[i]==0) {BFS (p,i,visit); }}//getchar ();}    void BFs (struct picture * q,int I,int visit[]) {struct tnode *w;    int a[max];//declares a queue int f,r;    int V;    f=r=0;    Visit[i]=1;    printf ("%d\n", Q->nd[i].valu);    A[r]=i;        r++;//the queue operation while (F!=R) {v=a[f];        f++;//Team Operation w=q->nd[v].link;            while (W!=null) {if (visit[w->data]==0) {visit[w->data]=1;            printf ("%d\n", Q->nd[w->data].valu);            a[r]=w->data;            r++;        } w=w->next; }    }}




Above project download

Http://pan.baidu.com/s/1o8qyWLs
Filename

Sjjgdemo-c


5 C Data Structure resource link


Http://outofmemory.cn/code-snippet/7235/C-language-scheme-create-and-BFS-DFS-bianli


http://blog.csdn.net/Golden_Shadow/article/category/745332


Http://www.oschina.net/code/list/?lang=cpp&catalog=programming-base&show=time


Some of the C language data structure codes that can be run

Related Article

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.