| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 311 人关注过本帖
标题:链表问题!!!!
只看楼主 加入收藏
djczlp520
Rank: 1
等 级:新手上路
帖 子:6
专家分:4
注 册:2010-3-19
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
链表问题!!!!
建立一个单链表,要求实现以下功能:查找元素为X的节点,求单链表的长度,插入一个元素值为X的结点,删除元素值为X的结点!!!!
搜索更多相关主题的帖子: 链表 
2010-03-29 14:24
ldg628
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:3
帖 子:526
专家分:3036
注 册:2009-6-23
收藏
得分:20 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct _node{
    int val;
    char name[20];
    struct _node *next;
}node;

void insertnode(node **head, int data, char *name)
{
    node *tmp;
    node *p = (node *)malloc(sizeof(node));
    p->val = data;
    strcpy(p->name, name);
    p->next = *head;
    *head = p;
}

node *findnode(node *head, int key)
{
    while(head)
    {
        if (head->val == key)
        {
            return head;
        }
        head = head->next;
    }
    return NULL;
}
int getnodelen(node *head)
{                                                                                                                 
    int ret = 0;                                                                                                  
    while(head)                                                                                                   
    {                                                                                                            
        ret ++;                                                                                                   
        head = head->next;                                                                                       
    }                                                                                                            
    return ret;                                                                                                   
}         
int deletenode(node **head, int data)                                                                             
{                                                                                                                 
    node *cur = *head, *pre = *head;                                                                              
    while(cur)                                                                                                   
    {                                                                                                            
        if (cur->val == data)                                                                                    
        {                                                                                                         
            if(cur == *head)                                                                                      
            {                                                                                                     
                *head = cur->next;                                                                                
            }else                                                                                                
            {                                                                                                     
                pre->next = cur->next;                                                                           
            }                                                                                                     
            free(cur);                                                                                            
            printf("delet successful!\n");                                                                        
            return 1;                                                                                             
        }                                                                                                         
        pre = cur;                                                                                                
        cur = cur->next;                                                                                          
    }                                                                                                            
    printf("didn't node, delet faile!\n");                                                                        
    return 0;                                                                                                     
}                     
void deleteall(node *head)                                                                                       
{                                                                                                                 
    node *p;                                                                                                      
    while(head)                                                                                                   
    {                                                                                                            
        p = head->next;                                                                                          
        free(head);                                                                                               
        head = p;                                                                                                
    }                                                                                                            
}                                                                                                                 
void print(node *head)                                                                                            
{                                                                                                                 
    while(head)                                                                                                   
    {                                                                                                            
        printf("name : %s val : %d\n", head->name, head->val);                                                   
        head = head->next;                                                                                       
    }                                                                                                            
}                 
int main(void)                                                                                                   
{                                                                                                                 
    int cnt, val;                                                                                                      
    char name[20];                                                                                                
    node *head = NULL, *p;                                                                                       
                                                                                                                  
    while(scanf("%d", &val) == 1)                                                                                 
    {                                                                                                            
        scanf("%s", name);                                                                                       
        insertnode(&head, val, name);                                                                             
    }                                                                                                            
    while(getchar() != '\n');                                                                                    
    cnt = getnodelen(head);                                                                                       
    printf("cnt = %d\n", cnt);                                                                                    
                                                                                                                  
    printf("input elem val : ");                                                                                 
    scanf("%d", &val);                                                                                            
    if ((p = findnode(head, val)) != NULL)                                                                        
    {                                                                                                            
        printf("name : %s\n", p->name);                                                                           
    }                                                                                                            
    printf("input delete elem val : ");                                                                           
    scanf("%d", &val);                                                                                            
                                                                                                                  
    printf("before delete : \n");                                                                                 
    print(head);                                                                                                  
    deletenode(&head, val);                                                                                       
                                                                                                                  
    printf("after delete : \n");                                                                                 
    print(head);                                                                                                  
    deleteall(head);                                                                                             
}   
小写了一下,测一下吧,希望对你有帮助~~~囧。。。好像又有种帮人做作业的感觉呀。。。汗                 
2010-03-29 15:39
快速回复:链表问题!!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025371 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved