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;
}