c++ 单链表的基本操作

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值