您的位置:首页 > 其它

单链表基本操作的简单实现

2012-12-06 14:59 369 查看
#include <iostream>
using namespace std;

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

node *createList()
{
node *head, *p;
p = (node *)malloc(sizeof(node));
head = p;
int x;
while(1)
{
cout<<"Input a number(0 to
exit):"<<endl;
cin>>x;
if(x != 0)
{
p->next = (node *)malloc(sizeof(node));
p = p->next;
p->data = x;
}
else
{
cout<<"End of
input!"<<endl;
p->next = NULL;
break;
}
}
head = head->next;
return head;
}

void displayList(node *head)
{
node *p = head;
if(p != NULL)
{
cout<<"The list
elements:"<<endl;
while(p->next != NULL)
{

cout<<p->data<<endl;
p = p->next;
}

cout<<p->data<<endl;
}
else
{
cout<<"Empty
List!"<<endl;
}
}

int lengthList(node *head)
{
int i = 0;
node *p = head;
for(;;i++)
{
if(p == NULL)
break;
p = p->next;
}
return i;
}

node *deleteElement(node *head, int x)
{
if(head == NULL)
{
cout<<"Empty
List!"<<endl;
return head;
}
node *p = head;
if(p->data == x)//delete the first
element
{
cout<<"delete head
element"<<endl;
head = head->next;
}
else
{
while(1)
{
if(p->next->data == x)//delete
other element
{
cout<<"delete other
element"<<endl;
p->next  =
p->next->next;
return head;
}
p = p->next;
if(p->next == NULL)
{
cout<<"not exist
element"<<endl;
return head;
}
}
}
return head;
}

node *insertElement(node *head, int x)//the list was
sorted
{
node *p1, *p2, *temp;
temp = (node *)malloc(sizeof(node));
temp->data = x;
if(temp->data <
head->data)//insert before the head
{
temp->next = head;
return temp;
}
p1 = head;
p2 = p1->next;
while(p2->data <
temp->data &&
p2->next != NULL)
{
p2 = p2->next;
p1 = p1->next;
}
if(p2->data >=
temp->data)//insert inner the list
{
temp->next = p2;
p1->next = temp;
return head;
}
else//insert after the last element
{
temp->next = NULL;
p2->next = temp;
return head;
}
}

node *sortList(node *head)//descending order
{
if(head == NULL)
return head;
node *p;
int len, i, j;
len = lengthList(head);
for(i = 0; i < len; i++)
{
p = head;
for(j = 0; j < len - 1; j++)
{
if(p->next->data
< p->data)
{
swap(p->next->data,
p->data);
}
p = p->next;
}
}
}

node *reserveList(node *head)
{
node *p1, *p2, *p3;
if(head == NULL || head->next == NULL)
{
return head;
}
p1 = head;
p2 = p1->next;
while(p2 != NULL)
{
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
}
head->next = NULL;
head = p1;
return head;
}

void swap(int &a, int
&b)
{
int temp;
temp = a;
a = b;
b = temp;
}
int main()
{
node *head = createList();
displayList(head);
cout<<"Length of
list:"<<endl<<lengthList(head)<<endl;
cout<<"Input
deleteElement:"<<endl;
int del;
cin>>del;
head = deleteElement(head, del);
cout<<"After
deleteElement:"<<endl;
displayList(head);
int ins;
cout<<"Input
insertElement:"<<endl;
cin>>ins;
head = insertElement(head, ins);
cout<<"After
insertElement:"<<endl;
displayList(head);
sortList(head);
cout<<"After
sortList:"<<endl;
displayList(head);
cout<<"Before
reserveList:"<<endl;
displayList(head);
head = reserveList(head);
cout<<"After
reserveList:"<<endl;
displayList(head);
int end;
cout<<"Input a number to
end..."<<endl;
cin>>end;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: