简单链表(C实现)


 
#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*类型
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 *);
int main()
{
    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;
}

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);
    }
    pNew->data = val;
    pNew->next = NULL;
    r->next = pNew;
    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 ;
}
int length(PNODE pHead)
{
 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;
}
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 ;
}
bool Insert(PNODE pHead,int pos,int val)
{
 int i=0;
 PNODE p=pHead;
 
 while(i<pos-1 && NULL != p)
 {
  ++i;
  p = p->next;
 }
 if(i > pos-1 || p==NULL)
  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;
 pNew->next = p->next;
 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;
 PNODE r =p->next;
 *val = r->data;
 p->next = p->next->next;
 free(r);
 return true;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值