C语言网

 找回密码
 加入社区!

QQ登录

只需一步,快速开始

查看: 584|回复: 5

C语言链表示例代码 [复制链接]

Rank: 6Rank: 6

主题
68
帖子
348
C币
578 枚
在线时间
57 小时
发表于 2010-7-12 09:03:52 |显示全部楼层
看了本论坛一个求助帖后, 写的一段代码, 因为原帖已有回答,因此就贴在这里了,  有兴趣的可以比较一下这里贴出来的代码,与原帖贴出来的代码的异同。 原帖链接:http://bbs.cyuyan.com.cn/thread-691-1-1.html
       下面为我写的代码:
    /**************************************************************************
                           C语言链表操作代码

     1、先建立一若干结点(结点数据域的值由键盘输入)构成的单链表,
        再实现下列操作:(每一功能用一函数实现)
     2  从链表的第i个结点开始连删len个结点,若不够len个结点,
        则从第i个结点开始删到表尾。
     3、在第i个结点之后插入一值为x的结点,并返回成功与否的标志。



**************************************************************************/

#include <stdio.h>
struct  link
      {
           int  x;
           struct link  *next;
      };
// int   creat(struct link *add);
int   insert(struct link *head, int i,int data);
int   delete(struct link *head,int i,int len);

int main(int argc, char **argv)
  {  
     int flag;  //flag 为标志位
     int i=0;
     int len=0;
     struct link *data;
     struct link *head;
   
     
     head=data=(struct link *)malloc(sizeof(struct link));   //记录链表的head
     head->next=NULL;
     data->next=NULL;
     printf("Input the data what you wanna to insert!");
     printf("\nInput the character : to complete you data.\n")
     scanf("%d",&flag);
   
     while(':'!=(char)flag)
     {   
         data.x=flag;
         struct link *temp;
         temp=(struct link *)malloc(sizeof(struct link));
         data.next=temp;
         data=temp;
         data->next=NULL;
         free(temp);
         printf("Input the data what you wanna to insert!\n");
         printf("Input the character : to complete.\n");
         scanf("%d",&flag);
     }
     
     printf("\nInput the location where you want to insert a new data.");
     scanf("%d",&i);
     printf("\nInput the data you wanna to insert into the database.")
     scanf("%d",&flag);
     flag=insert(head,i,flag);
     if(!flag)   //flag=1 表示插入成功
        {  
           printf("can not insert a new data into the database\n");
        }
      else
           printf("You have succeed insert a new data to the database.\n");
      
      printf("Input the location you want delete the node of the database.\n");
      scanf("%d",&i);
      printf("Pelease input how much node do you wanna delete.\n");
      scanf("%d",&len);
      if(!len) return 1;
      if(!i)
         {
           printf("Input a wrong number,then will quit.\n");
           getch();
           return 1;
         }
      flag=delete(head,i,len);  //flag=1表示删除成功。
      (1==flag)?{printf("Have delete successfully.\n");}:{printf("\nCan not delete, then will quit.\n");}
      free(data);
      free(head);
      getch();
      return 1;
  }

/*************************************************************
   int insert(struct link *head,int i,int data)函数中
     
        head为链表表头指针
        i表示要插入的位置
        data表示要插入的数据

*************************************************************/
int insert(struct link *head,int i,int data)
{
   struct link *temp;
   int j;
   if(NULL==head->next )  
            if(1<i) return 0;  //当为空链表,且要插入的地方不是第1个位置的时候
   if(i==1)  //原链表为非空,并且要插入到表头的位置的时候
    {
       temp=(struct link *)malloc(sizeof(struct link));
       (NULL==temp) ? {return 0;}:{temp->next=head; head=temp; free(temp); return 1;}
    }
   for(j=0;j<i,j++)  //原链表非空,且不是要插入到表头位置。
    {
        temp=(struct link *)malloc(sizeof(struct link));
        (NULL==temp) ? {return 0;}:{temp->x=data;temp->next=head-next;head->next=temp;head=temp->next;free(temp); return 1;}
    }
}
     
/******************************************************************
     int delete(struct link *head, int i,int len)

          head表示原链表表头
          i表示删除开始的位置
          len表示要删除的元素的个数

******************************************************************/

int delete(struct link *head, int i,int len)
{
  struct link *temp,
              *lenhead;
  int j;
  int k;
  int length;
  whie(!head->next)
     {
        length++;
        temp=head->next;
        head=temp;
        head->next=temp->next;
     }
  if(1==i && 1==len)
      {
        temp=head->next;
        head->next=temp->next;
        head=temp;
        return 1;
      }
   if(length>=i+len)   //当链表长度足够长时
      {
         for(j=0;j<i;j++)
            {
              temp=head->next;
              head->next=temp->next;
              head=temp;
            }
        lenhead=head;
        for(k=0;k<len;K++)
           {
             temp=head->next;
             head->next=temp->next;
             head=temp;
           }
       lenhead->next=head;
      }
    else    //当要删除的节点超过原链表长度时
      {
        for(j=0;j<i;j++)
           {
             temp=head->next;
              head->next=temp->next;
              head=temp;
           }
         head->next=NULL;
      }
   return 1;
}
1

查看全部评分

Rank: 8Rank: 8

主题
24
帖子
636
C币
758 枚
在线时间
504 小时
发表于 2010-7-12 10:19:35 |显示全部楼层
呵呵!我要好好看看
因为执着,所以成功

Rank: 8Rank: 8

主题
24
帖子
636
C币
758 枚
在线时间
504 小时
发表于 2010-7-14 22:28:44 |显示全部楼层
楼主编程风格果然标准值的学习,但是这段里的while语句里的表达式不是很明白!麻烦你给讲讲哈
while(':'!=(char)flag)
     {   
         data.x=flag;
         struct link *temp;
         temp=(struct link *)malloc(sizeof(struct link));
         data.next=temp;
         data=temp;
         data->next=NULL;
         free(temp);
         printf("Input the data what you wanna to insert!\n");
         printf("Input the character : to complete.\n");
         scanf("%d",&flag);
     }
因为执着,所以成功

Rank: 6Rank: 6

主题
68
帖子
348
C币
578 枚
在线时间
57 小时
发表于 2010-7-15 17:02:48 |显示全部楼层
3# 傲天鹰

就说一句While的条件吧:
      
     while(':'!=(char)flag)

            我们用的是int格式输入:的,那么为了与char型的 ':' 比较就来了一下强制类型转换。

Rank: 2

主题
2
帖子
80
C币
87 枚
在线时间
16 小时
发表于 2010-9-2 12:38:52 |显示全部楼层
感觉用  typedef来修饰结构体方便些

Rank: 6Rank: 6

主题
68
帖子
348
C币
578 枚
在线时间
57 小时
发表于 2010-9-5 17:39:48 |显示全部楼层
同意  用typedef 可以减少代码量
您需要登录后才可以回帖 登录 | 加入社区!

C语言 ( 粤ICP备11042647号-2 )

GMT+8, 2012-2-7 22:28

©2009-2011 cyuyan.com.cn

回顶部