单链表的基本操作

#include <stdio.h>

#include <stdlib.h>

typedef struct node

{

 int data;

 struct node * next;

}node;

 

node *creat()  //创建单链表

{  

node *head;//头结点  

node *cur;//当前结点  

node *temp;//临时结点  

int x;//数据  

int cycle = 1;  

head = (node*)malloc(sizeof(node));//为头结点分配内存  

head->next = NULL; //初始head->next为空  

cur = head;  

while(cycle)  

{    

printf("please input a data(end to 0):\n");  

scanf("%d",&x);

 if(x!=0)  

{      

temp = (node*)malloc(sizeof(node));      

temp->data = x;    

 temp->next = cur->next;//尾结点置空      

cur->next = temp;      

cur=temp;  

}  

else  

{   

cycle = 0;

 }  

}

  return head;  

}

 

int length(node *head)//单链表长度

{

 int n = 0;  

node *cur;  

cur = head;    

while ((cur->next)!=NULL)  

{      

cur=cur->next;   

n++;  

}    

return n;

}

 

void print(node *head)//单链表的打印

{   

node *cur;   

int n = length(head);

  if(head==NULL||head->next==NULL)   

{    

printf("There is no data!\n");    

return;   

}   

printf("These %d datas are:\n",n);

cur=head->next;   

while(cur!=NULL)   

{      

printf("%d   ",cur->data);   

cur=cur->next;   

}   

printf("\n");

}

 

node * del(node *head,int num)//单链表的删除

{  

node *cur;

 node *per;  

cur = head->next;//当前结点

 per = head;//当前结点的前结点    

if(head==NULL||head->next==NULL)//头结点为空或者头结点后无结点 直接返回  

{   

return head;

 }

 while(num!=(cur->data)&&(cur->next)!=NULL)  

{   

per=cur;   

cur = cur->next;

 }

 if (num==(cur->data))  

{  

 per->next = cur->next;  

 free(cur);  

}

 return head;

}

 

node *sort(node *head)//单链表的排序

{  

node *cur;  

int n = length(head);  

int temp;

 if(head==NULL||head->next==NULL)//头结点为空或者头结点后无结点 直接返回  

{  

 return head;  

}

 bool exchanged;

 for (int i = 0;i<n-1;i++)//冒泡排序

 {  

 exchanged = false;   

cur = head->next; //当前结点   

for(int j = 0;j<n-1-i;j++)   

{   

 if ((cur->data) > (cur->next->data))   

 {              

temp = cur->data;     

cur->data = cur->next->data;     

cur->next->data = temp;     

exchanged = true;    

}   

 cur = cur->next;  //向下移动一个结点   

}  

 if (!exchanged)  

 {    

return head;

  }     

}  

return head;

}

node *insert(node *head,int num)//单链表的插入
{
 node *cur;
 node *per;
 cur = head->next;
 per = head ;
 if(head==NULL||head->next==NULL)//头结点为空或者头结点后无结点 直接返回
 {
  return head;
 }
 node *temp = (node*)malloc(sizeof(node));
 temp->data = num;
 while((temp->data)>(cur->data)&&(cur->next!=NULL))
 {
  per = cur;
  cur = cur->next;
 }

 if ((temp->data) <= (cur->data))//中间插入
 {
      temp->next = cur;
   per->next = temp;
 }

else  //比链表里的所以数据都大  插到尾结点后
 {
   temp->next = cur->next;
   cur->next = temp;
 }
 return head;
}

 

 

node * reverse(node *head)

{

 node *per,*cur,*temp;

 if(head==NULL||head->next==NULL)//头结点为空或者头结点后无结点 直接返回

 {   

return head;

 }

 per = NULL;//前一个结点

 cur = head->next;//当前结点

 while(cur)

 {      

temp = cur->next;//临时结点保存下一个结点指针   

cur->next = per;//当前结点的next指针改为指向前一个结点   

per = cur;//前一个结点向后移动   

cur = temp;//当前结点向后移动

 }  

head->next = per;//头结点的next指向最后一个结点  此时cur==NULL  

return head;

}

 

void RemoveHead(node *head)//删除头元素

{  

node *temp = head->next;

 head->next = temp->next;

 free(temp);

}

 

int main(int argc, char* argv[])

{

 int x;  

 node * head = creat();

 print(head);

 

 printf("the length of the link:%d\n",length(head));

 

 printf("oder the link:\n");  

 sort(head);  

 print(head);

 

 printf("please input the data you want to delete:\n");

 scanf("%d",&x);

 del(head,x);  

 print(head);

 

 printf("please input the data you want to insert:\n");

 scanf("%d",&x);

 insert(head,x);

 print(head);

 

 printf("reverse the link:\n");

 reverse(head);

 print(head);

 

 RemoveHead(head);
 print(head);

 return 0;

}

 

 

posted @ 2013-11-22 17:10  我没有领悟  阅读(263)  评论(0编辑  收藏  举报