posts - 1,  comments - 1,  trackbacks - 0
   人要忍得住诱惑。
   本文主要是关于单链表的一些操作,包括创建、添加、删除、倒置,不多少了,上代码。
  1
  2/*************************************************************************
  3* Copyright (c) 2009
  4* All rights reserved.
  5*
  6* 文件名称:link.cpp
  7* 摘    要:链表操作
  8*
  9* 当前版本:1.0
 10* 作    者:fighter
 11* 完成日期:
 12*
 13***************************************************************************/

 14
 15#include <stdlib.h>
 16#include <stdio.h>
 17
 18typedef struct stdata
 19{
 20    int num;
 21    stdata* next;
 22}
STDATA;
 23
 24int showlink(STDATA * head)
 25{
 26    STDATA * next = head;
 27
 28    while( NULL != next)
 29    {
 30        printf( "%d \n", next->num);
 31        next = next->next;
 32    }

 33
 34    printf( "show end!\n");
 35    return 0;
 36}

 37
 38int createlink(    STDATA * head )
 39{
 40    int i = 1;
 41    STDATA * next = NULL;
 42    STDATA * prev = head;
 43    
 44    if( NULL == head)
 45    {
 46        return 1;
 47    }

 48    head->num = 0;
 49    head->next = NULL;
 50
 51    for( ; i < 10; i ++)
 52    {
 53        next = ( STDATA *)malloc( sizeof( STDATA));
 54        if( NULL == next)
 55        {
 56            return 1;
 57        }

 58        next->num = i;
 59        next->next = NULL;
 60            
 61        prev->next = next;
 62        prev = next;
 63    }

 64    
 65    return 0;
 66}

 67
 68int linkadd( STDATA * head, int pos, int num)
 69{
 70    int ret = 0;
 71    STDATA * prev = head;
 72    STDATA * next = head;
 73    STDATA * data = NULL;
 74
 75    while( prev->num != pos && prev->next != NULL)
 76    {
 77        prev = prev->next;
 78        next = prev->next;
 79    }

 80
 81    if( prev->num == pos)
 82    {
 83        data = ( STDATA *)malloc( sizeof( STDATA));
 84        if( NULL == data)
 85        {
 86            return 1;
 87        }

 88
 89        data->num = num;
 90
 91        prev->next = data;
 92        data->next = next;
 93    }

 94
 95    return 0;
 96}

 97
 98int linkdel( STDATA * head, int pos)
 99{
100    int ret = 0;
101    STDATA * prev = head;
102    STDATA * next = head;
103    STDATA * data = NULL;
104
105    while( next->num != pos && next->next != NULL)
106    {
107        prev = next;
108        next = next->next;
109    }

110
111    if( next->num == pos)
112    {
113        data = next;
114        next = next->next;
115        prev->next = next;
116        free( data);
117    }

118
119    return 0;
120}

121
122STDATA * linkreverse( STDATA * head)
123{
124    int ret = 0;
125    STDATA * prev = head;
126    STDATA * next = prev->next;
127    STDATA * ptmp = next->next;
128
129    prev->next = NULL;
130
131    while( NULL != ptmp)
132    {
133        next->next = prev;
134        prev = next;
135        next = ptmp;
136        ptmp = ptmp->next;
137    }

138
139    next->next = prev;
140
141    return next;
142}

143
144int main( int argc, char ** argv)
145{
146    STDATA * head = ( STDATA *)malloc( sizeof( STDATA));
147    createlink( head);
148    showlink( head);
149    
150    linkadd( head, 4100);
151    showlink( head);
152
153    linkdel( head, 100);
154    showlink( head);
155
156    head = linkreverse( head);
157    showlink( head);
158
159    return 0;
160}

161
posted on 2009-07-06 16:28 似水流年 阅读(287) 评论(0)  编辑 收藏 引用 所属分类: 练手的小例子数据结构和算法
<2026年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

新闻分类

相册

收藏夹

服务器设计相关

搜索

  •  

最新评论