#include <iostream>
#include <cassert>
using namespace std;
struct node{
int data; // data
struct node *next; // link to next
};
int Length(struct node* head);
struct node* BuildOneTwoThree(int iNodeCount);
void Push(struct node** headRef, int newData);
struct node* AppendNode(struct node** headRef, int num);
int Count(struct node* head, int searchFor);
int GetNth(struct node* head, int index);
void DeleteList(struct node** headRef) ;
int Pop(struct node** headRef) ;
void InsertNth(struct node** headRef, int index, int data) ;
void SortedInsert(struct node** headRef, struct node* newNode);
void Append(struct node** aRef, struct node** bRef) ;
void FrontBackSplit(struct node* source,
struct node** frontRef, struct node** backRef);
void RemoveDuplicates(struct node* head) ;
void MoveNode(struct node** destRef, struct node** sourceRef);
struct node* ShuffleMerge(struct node* a, struct node* b) ;
static void Reverse(struct node** headRef) ;
void main()
{
struct node* iLink = BuildOneTwoThree(6);
Reverse( &iLink);
// InsertNth( &iLink,20,18);
int iLength = Length(iLink);
cout << iLength<<endl;
return ;
}
void Reverse(struct node** headRef)
{
struct node* current = *headRef;
struct node* next = NULL;
struct node* result = NULL;
while( current != NULL)
{
next = current->next ;
current->next = result;
result = current;
current = next;
}
*headRef = result;
}
struct node* ShuffleMerge(struct node* a, struct node* b)
{
struct node* ResultNode = NULL;
while(1)
{
if ( a == NULL)
{
ResultNode->next = b;
break;
}else if ( b == NULL )
{
ResultNode->next = a;
break;
}else
{
ResultNode = a;
a = a->next;
ResultNode = ResultNode->next;
ResultNode = b;
b = b->next;
ResultNode = ResultNode->next;
}
}
return (ResultNode);
}
void MoveNode(struct node** destRef, struct node** sourceRef)
{
if ( *sourceRef == NULL )
{
return ;
}
struct node* head = *sourceRef;
struct node* insertNode = *sourceRef;
insertNode->next = *sourceRef;
*destRef = insertNode;
}
void RemoveDuplicates(struct node* head)
{
struct node* current = head;
if ( current == NULL || current->next == NULL)
{
return ;
}
while( current->next != NULL)
{
if ( current->data == current->next->data )
{
struct node* freeNode = current->next;
current->next = current->next->next;
free(freeNode);
}else
{
current = current->next;
}
}
}
void FrontBackSplit(struct node* source,
struct node** frontRef, struct node** backRef)
{
struct node* current = source;
int ilength = Length(source);
if ( ilength < 2 )
{
*frontRef = source;
*backRef = NULL;
}else
{
int hopcount = (ilength - 1)/2;
for ( int i = 0;i < hopcount;i++)
{
current = current->next;
}
*frontRef = source;
*backRef = current->next;
current->next = NULL;
}
}
void Append(struct node** aRef, struct node** bRef)
{
if ( *aRef == NULL)
{
*aRef = *bRef;
}
struct node *aCurrent = *aRef;
while(aCurrent)
{
aCurrent = aCurrent->next;
}
aCurrent = *bRef;
*bRef = NULL;
}
void SortedInsert(struct node** headRef, struct node* newNode)
{
struct node* current = *headRef;
if ( current == NULL || current->next->data > newNode->data )
{
newNode->next = current;
current = newNode ;
}else
{
while( current->next != NULL && current->next->data < newNode->data)
{
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
void InsertNth(struct node** headRef, int index, int data)
{
struct node* current = *headRef;
struct node* Node = NULL;
if ( current == NULL )
{
Node = (struct node*)malloc(sizeof(struct node));
Node->data = data;
Node->next = NULL;
current = Node;
}
struct node* PreNode = NULL;
int nth = 0;
while( current != NULL )
{
if( nth++ == index -1 )
{
Node = (struct node*)malloc(sizeof(struct node));
Node->data = data;
Node->next = current->next;
current->next = Node;
break;
}
PreNode = current;
current = current->next;
}
if ( nth < index-1 )
{
Node = (struct node*)malloc(sizeof(struct node));
Node->data = data;
Node->next = NULL;
PreNode->next = Node;
}
}
int Pop(struct node** headRef)
{
struct node* current = *headRef;
assert(current != NULL);
int data = current->data;
struct node* head = current->next;
free(current);
*headRef = head;
return data;
}
void DeleteList(struct node** headRef)
{
struct node* current = *headRef;
struct node* temp;
while( current )
{
temp = current;
current = current->next;
free(temp);
}
*headRef = NULL;
}
int GetNth(struct node* head, int index)
{
struct node* current = head;
int nth = 0;
while( current )
{
if ( nth++ == index )
{
return current->data;
}
current = current->next;
}
return -1;
}
int Count(struct node* head, int searchFor)
{
int count = 0;
if ( head == NULL )
{
return count;
}
struct node* current = head;
while( current )
{
if ( current->data == searchFor )
{
count++;
}
current = current->next;
}
return count;
}
int Length(struct node* head)
{
struct node *current = head;
int iCount = 0;
while( current != NULL )
{
iCount ++;
current = current->next;
}
return iCount;
}
struct node* BuildOneTwoThree( int iNodeCount )
{
struct node *head = NULL;
struct node *tail = NULL;
Push(&head,1);
tail = head;
int iIndex = 2;
while( iIndex <= iNodeCount )
{
Push(&(tail->next),iIndex);
tail = tail->next;
iIndex++;
}
return (head);
}
void Push(struct node** headRef, int newData)
{
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = newData;
newNode->next = *headRef;
*headRef = newNode;
}
struct node* AppendNode(struct node** headRef, int num)
{
struct node* current = *headRef;
struct node* newNode;
newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = num;
newNode->next = NULL;
if ( current == NULL )
{
*headRef = newNode;
}else
{
while( current != NULL)
{
current = current->next;
}
}
current = newNode;
return(*headRef);
}
单链表相关操作代码
最新推荐文章于 2023-05-30 00:16:17 发布