注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Aspirer's blog

停止维护,新博客地址:http://aspirer.wang/

 
 
 

日志

 
 

链表操作  

2009-07-14 10:04:13|  分类: 学习心得 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

链表逆序 - aspirer - Aspirers blog

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
 int num;
 struct Node *next;
}Node;

Node *Init(void);
void Print(Node *head);
void Free(Node *head);
Node *Inv(Node *head);

int main()
{
 Node *list = Init();
 Print(list);
 list = Inv(list);
 Print(list);
 Free(list);
 return 0;
}

Node *Init(void)
{
 int n;
 Node *head = NULL, *tmp;
 Node *trail;
 puts("input n.");
 fflush(stdin);
 scanf("%d", &n);
 while (n)
 {
  if (NULL == head)
  {
   head = (Node *)malloc(sizeof(Node));
   head->num = n;
   head->next = NULL;
   trail = head;
  }
  else
  {
   tmp = (Node *)malloc(sizeof(Node));
   trail->next = tmp;
   tmp->num = n;
   tmp->next = NULL;
   trail = tmp;
  }
  puts("input n.");
  fflush(stdin);
  scanf("%d", &n);
 }

 return head;
}

void Print(Node *head)
{
 while (head)
 {
  printf("%d", head->num);  
  head = head->next;
  if (head)
  {
   printf(" -> ");
  }
 }
 puts("");
}

void Free(Node *head)
{
 Node *tmp;
 while (head)
 {
  tmp = head;  
  head = head->next;
  free(tmp);
 }
}

Node *Inv(Node *head)
{
 Node *next = NULL, *newhead = NULL;
 while (head)
 {
  newhead = head;
  head = head->next;
  newhead->next = next;
  next = newhead;  
 }

 return newhead;
}


#include <iostream>
#include <stdio.h>
#include <cmath>
#include <assert.h>
using namespace std;

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

Node *Creat()
{
 Node *head, *p, *t;
 int d;
 cout << "input data." << endl;
 fflush(stdin);
 cin >> d;
 if (d)
 {
  head = (Node *)malloc(sizeof(Node));
  assert(head);
  head->data = d;
 }
 else
 {
  return NULL;
 }
 p = head;

 cout << "input data." << endl;
 fflush(stdin);
 cin >> d;

 while (d)
 {
  t = (Node *)malloc(sizeof(Node));
  assert(t);
  t->data = d;
  p->next = t;
  p = p->next;

  cout << "input data." << endl;
  fflush(stdin);
  cin >> d;
 }
 p->next = NULL;
 
 return head;
}

void Release(Node *head)
{
 Node *t;
 while(head)
 {
  t = head;
  head = head->next;
  cout << t->data << " released." << endl;
  free(t);
 }
 cout << "Node list all released." << endl;
}

size_t GetListLen(Node *head)
{
 size_t length = 0;
 while (head)
 {
  length++;
  head = head->next;
 }
 return length;
}

Node *Remove(Node *head, int num)
{
 Node *pre = head;
 Node *curr = head;

 while (curr->data != num && curr->next)
 {
  pre = curr;
  curr = curr->next;
 }
 if (curr->data == num)
 {
  if (curr == head)
  {
   head = head->next;
   free(curr);
   curr = NULL;
   cout << num << " was removed." << endl;
   return head;
  }
  else
  {
   pre->next = curr->next;
   free(curr);
   curr = NULL;
   cout << num << " was removed." << endl;
   return head;
  }
 }
 else
 {
  cout << "can not find " << num << endl;
  return head;
 }
}

Node *Sort(Node *head)
{
 int tmp;
 Node *p1, *p2, *p3;
 for (p1 = head; p1 != NULL; p1 = p1->next)
 {
  p3 = p1;
  for (p2 = p1->next; p2 != NULL; p2 = p2->next)
  {
   if (p3->data > p2->data)
   {
    p3 = p2;    
   }
  }
  if (p3 != p1)
  {
   tmp = p1->data;
   p1->data = p3->data;
   p3->data = tmp;
  }
 }
 return head;
}

Node *Insert(Node *head, int num)
{
 Node *pre = head, *curr = head, *tmp;
 tmp = (Node *)malloc(sizeof(Node));
 assert(tmp);
 tmp->data = num;
 while (curr && curr->data < num)
 {
  pre = curr;
  curr = curr->next;
 }
 if (curr == head)
 {  
  head = tmp;
  head->next = curr;
  //return head;
 }
 else
 {
  pre->next = tmp;
  tmp->next = curr;
  //return head;
 }
 return head;
}

Node *Reverse(Node *head)
{
 Node *pre, *curr, *next;
 if (!head || !head->next)
 {
  return head;
 }
 pre = head;
 curr = head->next;

 while (curr)
 {  
  next = curr->next;
  curr->next = pre;  
  pre = curr;
  curr = next;
 }
 head->next = NULL;
 head = pre;

 return head;
}

int main()
{
 Node *head = Creat();
 Sort(head);
 head = Insert(head, 0);
 cout << "list length = " << GetListLen(head) << "." << endl;
 head = Remove(head, 0);
 head = Reverse(head);
 Release(head);
 head = NULL;
 
 return 0;
}

  评论这张
 
阅读(473)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018