Single-chain table processing frequently used in interviews

Source: Internet
Author: User

[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Include <assert. h>
 
Struct node
{
Int data;
Struct node * next;
} Linknode;
 
Typedef struct node * LinkNode;
LinkNode head = NULL;
 
LinkNode createNode (int data)
{
LinkNode node = NULL;
Node = (LinkNode) malloc (sizeof (linknode ));
Node-> data = data;
Node-> next = NULL;
Return node;
}
// In the insert function, the value of the head after the head is returned will not be empty, but the value of the head will be returned if the head is not returned.
LinkNode insert (LinkNode head, int data)
{
If (head = NULL)
{
Head = createNode (data );
Return head;
}
LinkNode node = NULL;
Node = createNode (data );
Node-> data = data;
Node-> next = head-> next;
Head-> next = node;
Return head;
}
 
Void traverse (LinkNode head)
{
LinkNode p = NULL;
P = head;
While (NULL! = P)
{
Printf ("% d", p-> data );
P = p-> next;
}
}
 
Void linkListFree (LinkNode head)
{
Assert (head! = NULL );
LinkNode p = head;
LinkNode q;
While (NULL! = P)
{
Q = p-> next;
Free (p );
P = NULL;
P = q;
}
}
 
LinkNode reverse (LinkNode head)
{
Assert (head! = NULL );
LinkNode ptr = createNode (-1 );
Ptr-> next = head;
LinkNode p = head-> next;
Head-> next = NULL; // The head-> next must be left empty; otherwise, a loop occurs.
LinkNode q = NULL;
While (p! = NULL)
{
Q = p-> next;
P-> next = ptr-> next;
Ptr-> next = p;
P = q;
}
Return ptr-> next;
}
 
// Delete a node from a single-chain table with no Headers
Void deleteRandomNode (LinkNode p)
{
Assert (p! = NULL );
LinkNode n = p-> next;
If (n! = NULL)
{
P-> data = n-> data;
P-> next = n-> next;
Free (n );
N = NULL;
}
}
 
// Determine whether two linked lists are intersecting
Bool isIntersect (LinkNode headone, LinkNode headtwo)
{
Assert (headone! = NULL) & (headtwo! = NULL ));
LinkNode p = headone;
While (p-> next! = NULL)
{
P = p-> next;
}
LinkNode q = headtwo;
While (NULL! = Q) & (q! = P ))
{
Q = q-> next;
}
If (q = NULL)
Return false;
Return true;
}
 
 
// If the intersection finds the first node of the intersection
LinkNode firstIntersectNode (LinkNode headone, LinkNode headtwo)
{
Assert (NULL! = Headone) & (NULL! = Headtwo ));
Int lenone = 0, lentwo = 0;
LinkNode p = headone;
While (NULL! = P)
{
Lenone ++;
P = p-> next;
}
P = headtwo;
While (NULL! = P)
{
Lentwo ++;
P = p-> next;
}
If (lenone <lentwo)
{
P = headtwo;
For (int I = 0; I <(lentwo-lenone); I ++)
{
P = p-> next;
}
LinkNode q = headone;
While (NULL! = P) & (NULL! = Q ))
{
If (p = q)
Return p;
Else
{
P = p-> next;
Q = q-> next;
}
}
}
Else if (lenone> lentwo)
{
P = headone;
For (int I = 0; I <(lenone-lentwo); I ++)
{
P = p-> next;
}
LinkNode q = headtwo;
While (NULL! = P) & (NULL! = Q ))
{
If (p = q)
Return p;
Else
{
P = p-> next;
Q = q-> next;
}
}
}
Else
{
P = headone;
LinkNode q = headtwo;
While (NULL! = P) & (NULL! = Q ))
{
If (p = q)
Return p;
Else
{
P = p-> next;
Q = q-> next;
}
}
}
}
 
 
LinkNode createLinkList (int n)
{
LinkNode h = NULL;
For (int I = 0; I <n; I ++)
H = insert (h, I + 1 );
Return h;
}
 
Void test ()
{
Head = createLinkList (10 );
Traverse (head );
Printf ("\ n ");
LinkNode h = reverse (head );
LinkNode p = h;
Traverse (h );
}
 
Int main ()
{
Test ();
Return 0;
}

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.