#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
// only for the 1st Node
void initNode(struct Node *head,int n){
head->data = n;
head->next = NULL;
}
// apending
void addNode(struct Node *head, int n) {
Node *cur = head;
while( cur->next != NULL )
{
cur = cur->next;
}
Node *newNode = new Node;
newNode->data = n;
newNode->next = NULL;
cur->next = newNode;
}
void insertFront(struct Node **head, int n) {
Node *newNode = new Node;
newNode->data = n;
newNode->next = *head;
*head = newNode;
}
struct Node *searchNode(struct Node *head, int n) {
Node *cur = head;
while( cur != NULL)
{
if ( cur->data == n)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
bool deleteNode(struct Node **head, Node *ptrDel) {
Node *cur = *head;
if ( ptrDel == cur)
{
cur = cur->next;
delete ptrDel;
*head = cur;
return true;
}
while( cur->next != NULL )
{
if ( cur->next == ptrDel )
{
cur->next = cur->next->next;
delete ptrDel;
return true;
}
cur = cur->next;
}
return false;
}
/* reverse the list */
struct Node* reverse(struct Node** List)
{
Node* parent = *List;
if ( parent == NULL || parent->next == NULL)
{
return *List;
}
Node *me = parent->next;
if ( me->next == NULL)
{
me->next = *List;
*List = me;
return *List;
}
Node *child = me->next;
parent->next = NULL;
while(child)
{
me->next = parent;
parent = me;
me = child;
child = child->next;
}
me->next = parent;
*List = me;
return *List;
}
/* Creating a copy of a linked list */
void copyLinkedList(struct Node *node, struct Node **pNew)
{
if ( node != NULL )
{
*pNew = new Node;
(*pNew)->data = node->data;
(*pNew)->next = NULL;
copyLinkedList(node->next,&(*pNew)->next);
}
}
/* return value: same(1), different(0) */
int compareLinkedList(struct Node *node1, struct Node *node2)
{
static int flag;
if ( node1 == NULL || node2 == NULL )
{
flag = 1;
return flag;
}
if ( (node1 == NULL && node2 != NULL) ||( node1 != NULL && node2 == NULL))
{
flag = 0;
return flag;
}
node1 = node1->next;
node2 = node2->next;
compareLinkedList(node1,node2);
}
void deleteLinkedList(struct Node **node)
{
Node *cur = *node;
while(cur)
{
Node *temp = cur;
cur = cur->next;
delete temp;
}
}
void display(struct Node *head) {
Node *list = head;
while(list) {
cout << list->data << " ";
list = list->next;
}
cout << endl;
cout << endl;
}
int main()
{
struct Node *newHead;
struct Node *head = new Node;
initNode(head,10);
display(head);
addNode(head,30);
display(head);
addNode(head,35);
display(head);
addNode(head,40);
display(head);
insertFront(&head,5);
display(head);
int numDel = 35;
Node *ptrDelete = searchNode(head,numDel);
if(deleteNode(&head,ptrDelete))
cout << "Node "<< numDel << " deleted!\n";
display(head);
cout << "The list is reversed\n";
reverse(&head);
display(head);
cout << "The list is copied\n";
copyLinkedList(head,&newHead);
display(newHead);
cout << "Comparing the two lists...\n";
cout << "Are the two lists same?\n";
if(compareLinkedList(head,newHead))
cout << "Yes, they are same!\n";
else
cout << "No, they are different!\n";
cout << endl;
return 0;
}
c++ 单链表的基本操作
最新推荐文章于 2022-10-12 00:16:30 发布