Linear table operations in data structures, linear data structures

Source: Internet
Author: User

Linear table operations in data structures, linear data structures

# Include <stdio. h>
# Include <stdlib. h>
// The data in this linked list is int type
Typedef struct Link_list {
Int date;
Struct Link_list * next;
} Link;

Int main ()
{
Link * Link_creat ();
Void Link_print (Link * head );
Void ClearList (Link * head );
Bool ListEmpty (Link * head );
Int ListLength (Link * head );
Int GetElem (Link * head, int n );
Bool ListDelete (Link * head, int locate );
Bool ListInsert (Link * head, int number, int locate );
Link * Line;
Line = Link_creat ();
// If (ListDelete (Line, 2) Link_print (Line );
// Printf ("% d", GetElem (Line, 6 ));
Return 0;
}

Link * Link_creat ()
{
Link * head = NULL;
Link * tail = head;
Int number;
Do {
Scanf ("% d", & number );
If (number =-1 ){
Break;
}
Link * p = (Link *) malloc (sizeof (Link ));
P-> date = number;
P-> next = NULL;
If (! Tail ){
Head = p;
Tail = head;
}
Else {
While (tail-> next)
{
Tail = tail-> next;
}
Tail-> next = p;
}
} While (number! =-1 );
Return head;
}

Void Link_print (Link * head)
{
Link * p = head;
While (p)
{
Printf ("% d", p-> date );
P = p-> next;
}
}

Void ClearList (Link * head)
{
Link * p = head;
Link * pt = head-> next;
While (pt)
{
Free (p );
P = pt;
Pt = pt-> next;
}
Free (p );
}

Bool ListEmpty (Link * head)
{
Link * p = head;
If (! P) return false;
Else return true;
}

Int ListLength (Link * head)
{
Link * p = head;
Int ans = 0;
While (p)
{
Ans ++;
P = p-> next;
}
Return ans;
}
Int GetElem (Link * head, int n)
{
Link * p = head;
If (n> ListLength (p) return-1;
For (int I = 1; I <n; I ++)
{
P = p-> next;
}
Return p-> date;
}

Bool ListInsert (Link * head, int number, int locate)
{
If (locate> ListLength (head) return false;
Link * p = head;
Link * temp = (Link *) malloc (sizeof (Link ));
Temp-> date = number;
For (int I = 1; I <locate-1; I ++)
{
P = p-> next;
}
Temp-> next = p-> next;
P-> next = temp;
Return true;
}

Bool ListDelete (Link * head, int locate)
{
If (locate> ListLength (head) return false;
Link * p = head;
For (int I = 1; I <locate-1; I ++)
{
P = p-> next;
}
P-> next = (p-> next)-> next;
Return true;
}

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.