#include<iostream.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}NODE,*PNODE; //NODE代表了struct Node类型 PNODE代表了struct Node*类型
{
int data;
struct Node *next;
}NODE,*PNODE; //NODE代表了struct Node类型 PNODE代表了struct Node*类型
PNODE Create_Node(); //进行链表的创建
void Reverse_Node(PNODE pHead); //进行链表的输出
int length(PNODE pHead); //求链表的长度
bool Is_empty(PNODE pHead); //判断链表是否为空
void Sort_Node(PNODE pHead); //进行排序
bool Insert(PNODE,int ,int);
bool Delete(PNODE,int,int *);
void Reverse_Node(PNODE pHead); //进行链表的输出
int length(PNODE pHead); //求链表的长度
bool Is_empty(PNODE pHead); //判断链表是否为空
void Sort_Node(PNODE pHead); //进行排序
bool Insert(PNODE,int ,int);
bool Delete(PNODE,int,int *);
int main()
{
PNODE pHead = Create_Node();
cout<<"Output the number:"<<endl;
Reverse_Node(pHead);
{
PNODE pHead = Create_Node();
cout<<"Output the number:"<<endl;
Reverse_Node(pHead);
int val;
Delete(pHead,2,&val);
cout<<endl<<val<<endl;
Reverse_Node(pHead);
/*int len = length(pHead);
cout<<endl<<"The length is:"<<len<<endl;
Sort_Node(pHead);*/
// Insert(pHead,3,33);
// Reverse_Node(pHead);
return 0;
}
Delete(pHead,2,&val);
cout<<endl<<val<<endl;
Reverse_Node(pHead);
/*int len = length(pHead);
cout<<endl<<"The length is:"<<len<<endl;
Sort_Node(pHead);*/
// Insert(pHead,3,33);
// Reverse_Node(pHead);
return 0;
}
PNODE Create_Node()
{
int len;
cout<<"Enter the length of node";
cin>>len;
int val;
PNODE pHead = (PNODE)malloc(sizeof(NODE));
if(NULL == pHead)
{
cout<<"The memory is error!"<<endl;
exit(-1);
}
PNODE r = pHead; //define a variable to link
r->next = NULL;
for(int i=0; i<len; ++i)
{
cout<<"Enter "<<i+1<<" number:";
cin>>val;
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if(NULL == pNew)
{
cout<<"The memory is error!"<<endl;
exit(-1);
}
r->next = NULL;
for(int i=0; i<len; ++i)
{
cout<<"Enter "<<i+1<<" number:";
cin>>val;
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if(NULL == pNew)
{
cout<<"The memory is error!"<<endl;
exit(-1);
}
pNew->data = val;
pNew->next = NULL;
pNew->next = NULL;
r->next = pNew;
r = pNew;
}
return pHead;
}
r = pNew;
}
return pHead;
}
void Reverse_Node(PNODE pHead)
{
PNODE pNode = pHead->next;
if(pNode == NULL)
{
cout<<"No Data!"<<endl;
exit(-1);
}
while(pNode != NULL)
{
cout<<pNode->data <<" ";
pNode = pNode->next;
}
return ;
}
{
PNODE pNode = pHead->next;
if(pNode == NULL)
{
cout<<"No Data!"<<endl;
exit(-1);
}
while(pNode != NULL)
{
cout<<pNode->data <<" ";
pNode = pNode->next;
}
return ;
}
int length(PNODE pHead)
{
PNODE pNode = pHead->next;
int i=0;
while(pNode != NULL)
{
pNode = pNode->next;
i++;
}
return i;
}
{
PNODE pNode = pHead->next;
int i=0;
while(pNode != NULL)
{
pNode = pNode->next;
i++;
}
return i;
}
bool Is_empty(PNODE pHead)
{
if(pHead->next == NULL)
return true;
else
return false;
}
{
if(pHead->next == NULL)
return true;
else
return false;
}
void Sort_Node(PNODE pHead)
{
//利用泛型的思想 (使用指针实现)
int i,j,t;
PNODE p,q;
int len = length(pHead);
// p = pHead->next; 这样无法执行 因为内存的原因
// q = p->next;
for(i=0,p = pHead->next ; i<len-1; ++i,p=p->next)
{
for(j=i+1,q = p->next; j<len; ++j,q=q->next)
if(p->data > q->data)
{
/* t = a[i];
a[i] = a[j];
a[j] = t;*/
t = p->data;
p->data = q->data;
q->data = t;
}
}
return ;
}
{
//利用泛型的思想 (使用指针实现)
int i,j,t;
PNODE p,q;
int len = length(pHead);
// p = pHead->next; 这样无法执行 因为内存的原因
// q = p->next;
for(i=0,p = pHead->next ; i<len-1; ++i,p=p->next)
{
for(j=i+1,q = p->next; j<len; ++j,q=q->next)
if(p->data > q->data)
{
/* t = a[i];
a[i] = a[j];
a[j] = t;*/
t = p->data;
p->data = q->data;
q->data = t;
}
}
return ;
}
bool Insert(PNODE pHead,int pos,int val)
{
int i=0;
PNODE p=pHead;
while(i<pos-1 && NULL != p)
{
++i;
p = p->next;
}
{
int i=0;
PNODE p=pHead;
while(i<pos-1 && NULL != p)
{
++i;
p = p->next;
}
if(i > pos-1 || p==NULL)
return false;
return false;
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if(pNew == NULL)
{
cout<<"Allocate the memory failure."<<endl;
exit(-1);
}
pNew->data = val;
pNew->next = NULL;
if(pNew == NULL)
{
cout<<"Allocate the memory failure."<<endl;
exit(-1);
}
pNew->data = val;
pNew->next = NULL;
pNew->next = p->next;
p->next = pNew;
p->next = pNew;
return true;
}
}
bool Delete(PNODE pHead,int pos,int *val)
{
int i=0;
PNODE p=pHead;
while(i<pos-1 && NULL != p->next)
{
++i;
p = p->next;
}
if(i > pos-1 || p->next==NULL)
return false;
return false;
PNODE r =p->next;
*val = r->data;
p->next = p->next->next;
free(r);
return true;
}
*val = r->data;
p->next = p->next->next;
free(r);
return true;
}