您的位置:首页 > 其它

链表的基本操作

2016-10-11 23:16 239 查看
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

typedef struct node
{
int data;
node* next;
}Node;

Node* head = NULL;

bool buildlist()
{
//建立一个新的链表
head = (Node*)malloc(sizeof(Node));
if(head == NULL)
{
return false;
}
else
{
head->data = 0;
head->next = NULL;
return true;
}
}

bool insertnode(Node* node)
{
//加入新节点
if(head == NULL)
{
return false;
}
Node* p = head->next;
Node* q = head;

while(p != NULL)
{
q = p;
p = p->next;
}
q->next = node;
node->next = NULL;
return true;
}

bool delnode(int front)
{
//删除节点
if(head == NULL)
{
return false;
}
Node* p = head->next;

int length = 0;
while(p != NULL)
{
length++;
p = p->next;
}

if(length < front)
{
return false;
}
else
{
Node*q = head;
p = head->next;
for(int i = 0; i < front; i++)
{
q = p;
p = p->next;
}
Node* t = p->next;
q->next = t;
free(p);
return true;
}
}

void reverselist()
{
//逆序排列,非倒序
if(head == NULL)
return ;

if(head->next == NULL)
return ;

Node* p = head->next;
Node* q = p ->next;
Node* t = NULL;

while(q != NULL)
{
t = q->next;
q->next = p;
p = q;
q = t;
}
head->next->next = NULL;
head->next = p;

}

void sortlist()
{
//排序
Node* Head = head;
if(head == NULL)
return ;

if(Head->next == NULL)
return ;

Node* pi = Head->next;
Node* pj = pi->next;

for(; pi != NULL; pi = pi->next)
{
for(pj = pi->next; pj != NULL; pj = pj->next)
{
if(pi->data > pj->data)
{
int temp = pi->data;
pi->data = pj->data;
pi->data = temp;
}
}
}
}

void destroylist()
{
//删除链表
if(head == NULL)
return;

if(head->next == NULL)
{
free(head);
head = NULL;
return;
}

Node* p = head->next;
while(p != NULL)
{
Node* tmp = p;
p = p->next;
free(tmp);
}
free(head);
head = NULL;
}

void printlist()
{
//打印链表
node* a = head;
node* b = a->next;

while(b != NULL)
{
cout<<b->data;
a = b;
b = b->next;
}
}

void deleterepeat()
{
//删除重复的元素
Node* p1 = head;
Node* p2 = p1->next;
Node* p3 = NULL;

while(p2 != NULL)
{
if(p1->data == p2->data)
{
p3 = p2->next;
p1->next = p3;
}
p1 = p1->next;
p2 = p2->next;
}
}

int listlength()
{
//返回链表的长度
int num = 0;
Node* x = head->next;

while(x != NULL)
{
x = x->next;
num++;
}
return num;
}

node* mergel(node* list1, node*list2)
{
//合并两个链表;
if(list1 == NULL)
{
cout<<"Error"<<endl;
return list2;
}
else if(list2 == NULL)
{
return list1;
}

node* temp2 = NULL;

if(list1->data < list2->data)
{
temp2 = list1;
temp2->next = mergel(list1->next, list2);
}
else
{
temp2 = list2;
temp2->next = mergel(list1, list2->next);
}

return temp2;
}

int main()
{
if(buildlist())
{
Node* node1 = (Node*)malloc(sizeof(Node));
node1->data = 1;
node1->next = NULL;

Node* node2 = (Node*)malloc(sizeof(Node));
node2->data = 2;
node2->next = NULL;

Node* node3 = (Node*)malloc(sizeof(Node));
node3->data = 3;
node3->next = NULL;

Node* node4 = (Node*)malloc(sizeof(Node));
node4->data = 4;
node4->next = NULL;

insertnode(node1);
insertnode(node2);
insertnode(node3);
insertnode(node4);

reverselist();

printlist();

sortlist();

delnode(3);

destroylist();

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: