Realization of---Linear table of data structure learning

Source: Internet
Author: User

Linked list

The first is the structural body

typedef int ElementType; To make it easier to modify the type of elements in the list typedef struct NODE{ELEMENTTYPE date;struct node* Next;} List; List L,*ptrl;
A function to find the length of a chain table

int Length (List *ptrl) {list* P =ptrl;int j=0;while (p) {p=p->next;j++;} return J;}

Find by Location

list* findkth (int k,list *ptrl) {List *p=ptrl;int i=0;while (p!=null && i<k) {p=p->next;i++;} if (i = = k) return p;else return NULL;}
Find by value
list* Find (ElementType X, list* Ptrl) {list* P =ptrl;while (p!=null && p->date!=x) {p=p->next;} return p;}

Insert

/* Construct node, find I-1 nodes, insert node */list* Insert (ElementType x,int i, list* Ptrl) {list* s, * p;        if (i==1) {s= (list*) malloc (sizeof (List));        S->date = X;        S->next = Ptrl;    return s;    } p = findkth (I-1,ptrl);        if (p== NULL) {cout<< "parameter wrong" <<endl;    return NULL;        } else{s= (list*) malloc (sizeof (List));        S->date = X;    S->next = p->next;//position cannot be reversed p->next = s;        return Ptrl; }}
Delete

list* Delete (int i,list* ptrl) {List *p, *s;if (i==1) {//Delete header node S=ptrl;if (ptrl!=null) ptrl=ptrl->next;else return NULL; Free (s); Don't forget to release the space return Ptrl;} p = findkth (I-1,ptrl), if (p== NULL) {printf ("%d node does not exist", i-1); return NULL;} else if (P->next = = NULL) {printf ("%d node does not exist", I); return NULL;} else{s=p->next;//with the newly created pointer to P->next = s->next; free (s), return Ptrl;}
"Main function of the test"

int main () {list* p= (list*) malloc (sizeof (List));p->next = Null;p->date=2;p=insert (5,1,p);p =insert (3,2,p);p = Insert (5,2,p);cout<< "Length:  " <<length (p) <<endl; list* po = findkth (2,p); if (po==null) cout<< "does not exist" <<ENDL;ELSE{COUT<<PO->DATE<<ENDL;} P=delete (2,p);p o = findkth (2,p), if (po==null) cout<< "not Present" <<endl;else{cout<<po->Date<< Endl;} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Realization of---Linear table of data structure learning

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.