ctve facetest 链表删除save other list

来源:互联网 发布:网络流行照片 编辑:程序博客网 时间:2024/06/06 18:20


#include <iostream>  


using namespace std;


struct Node
{


char data;
Node* next;
};


Node* create()
{
Node* head = NULL;
Node* rear = head;
Node* p; // The pointer points to new created node.  
char tmp;


do
{
cout << "Please input integer or char '#':";
cin >> tmp;
if (tmp != '#')
{
p = new Node;
p->data = tmp;
p->next = NULL;


if (head == NULL)
{
head = p;
}
else
{
rear->next = p;
}


rear = p;
}
} while (tmp != '#');


return head;
}


void print(Node* head)
{
Node* p = head;


if (head != NULL)
{
do
{
cout << p->data << ' ';
p = p->next;
} while (p != NULL);
}


cout << endl;
}


unsigned int del(Node* l1, Node* l2)
{
static int cnt = 0;
if (!l1 || !l2)
{
return NULL;
}


Node *p = new Node;
p->next = l1;
Node *pL1 = l1, *pL2 = l2, *head = p;


while (pL1 && pL2)
{
if (pL1->data < pL2->data)
{
pL1 = pL1->next;
p = p->next;
cnt++;
}
else if (pL1->data > pL2->data)
{
pL2 = pL2->next;
//cnt++;
}
else
{
pL1 = pL1->next;
p->next = pL1;
cnt++;
}
}


l1 = head->next;


return  cnt;
}


int main()
{
cout << "Please input the first list:" << endl;
Node* l1 = create();


cout << endl << "Please input the second list:" << endl;
Node* l2 = create();


cout << endl << "--------------------------" << endl;
cout << "The first list is: ";
print(l1);
cout << "The second list is: ";
print(l2);


cout << del(l1, l2);
//cout << endl << "The result list is: ";
//print(l);


return 0;

}


这个程序是返回一个整数,该整数是列表中删除的另一表中的个数。

http://blog.csdn.net/troubleshooter/article/details/7716815


0 0