| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 573 人关注过本帖
标题:初学链表,有关单链表的程序有几处错,求高手们改错或完善
只看楼主 加入收藏
wfjimy
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2014-5-15
结帖率:100%
收藏
已结贴  问题点数:6 回复次数:6 
初学链表,有关单链表的程序有几处错,求高手们改错或完善
建立一个结构体可存储7个人数据(姓名,电话),然后用链表编写,使用户可以从第一个位置添加及删除,从最后一个位置添加及删除,也可以删除任意制定位置的信息,并且显示出来




程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>



typedef struct Contact
{
struct Contact* next;
char name[7];
char tel;
}Contact;


Contact*start=NULL;


void printList(Contact*a)
{
    if(a==NULL){
printf("no contact given");
}
else{
    printf("\nName:%s \nTel:%f", a->name,a->tel);
}
}


void addFirst(Contact*a){

struct Contact*newA;
newA=malloc(sizeof(Contact));
printf("Name:");
scanf("%6s",&(newA->name));
printf("Tel:");
scanf("%f",&(newA->tel));
return newA;

void push(Contact*newA){
if(start==NULL){
    start=newA;
    start->next=NULL;
    }else{
    newA->next=start;
    start=newA;
    }
    }
}


void addLast(Contact*a){

struct Contact*newA;
newA=malloc(sizeof(Contact));
printf("Name:");
scanf("%6s",&(newA->name));
printf("Tel:");
scanf("%s",&(newA->tel));
return newA;
}



void removeByName(char*removeByName){
    printf("0");
    Contact*ptr=start;
    Contact*last_ptr=start;
    if(start==NULL) return;
    if(strcmp(start->name,removeByName)==0){
        ptr=start;
        start=start->next;
        }
        else
        {
            while(ptr!=NULL){
                if(strcmp(ptr->name,removeByName)==0){
                    break;
                }
                last_ptr=ptr;
                ptr=ptr->next;
            }
       last_ptr->next=ptr->next;
        }
free(ptr);
}





void removeFirst()
{
Contact*pop(){
   if(start==NULL) treurn NULL;
   Contact*help=start;
   start=start->next;
   return help;
   }
}



void removeLast()
{
    void push(Contact*newA){
   if(start==NULL){
    start=newA;
    start->next=NULL;
   } else{

    newA->next=start;
    start=newA;
   }
   }
   Contact*pop(){
   if(start==NULL) treurn NULL;
   Contact*help=start;
   start=start->next;
   return help;
   }
   }





void builtList(Contact contacts[])
{
    Contact *ptr=NULL;
    int i;
    for(i=0; i<=6; i++)
    {
        ptr=&contacts[i];
        addLast(ptr);
    }

}


int main()
{Contact*a;
printStack();

    Contact contacts[7]=
    {
        {NULL,"Dennis","0203/123456"},
        {NULL,"Chantal","0177/3123345"},
        {NULL,"Robert","0163/9295986"},
        {NULL,"Bjoern","040 - 123232345"},
        {NULL,"Andreas","+49 178 11903123"},
        {NULL,"Jenny","+41 119 34544345"},
        {NULL,"Zeuss","0162-123-4531698"},
    };

    int choose;

    do{
    printf("1.Removing name Andreas\n");
    printf("2.Removing first\n");
    printf("3.Removing last\n");
    printf("4.Exit\n");
    printf("Input:");
        scanf("%i",&choose);
        printf("\n");

switch(choose)
{
    case 1: removeByName(char*removeByName);  //此处程序报错,不知道怎么改//
            printList(Contact*a);
            break;

    case 2: removeFirst();
            printList(Contact*a);
            break;

    case 3:removeLast();
           printList(Contact*a);
            break;

    }while(choose!=4);

    return 0;
}

搜索更多相关主题的帖子: Contact 结构体 电话 信息 
2014-07-23 06:03
embed_xuel
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:58
帖 子:3845
专家分:11385
注 册:2011-9-13
收藏
得分:0 
基本没有对的,给你改还不如重写一个

总有那身价贱的人给作业贴回复完整的代码
2014-07-23 08:10
wfjimy
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2014-5-15
收藏
得分:0 
回复 2 楼 embed_xuel
我不是很明白链表,所以能否帮我改一下程序,谢谢啦
2014-07-23 21:10
funyh250
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:26
帖 子:290
专家分:1573
注 册:2013-12-25
收藏
得分:0 
不是自己写的吧

学习是大事   吃喝拉撒睡是小事   其他的那都不是事
2014-07-23 23:52
wfjimy
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2014-5-15
收藏
得分:0 
回复 4 楼 funyh250
是自己写的,怎么着把各个function 连起来,不是很明白链表
2014-07-24 00:42
ditg
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:16
帖 子:852
专家分:1937
注 册:2014-4-10
收藏
得分:0 
教楼主一个简单的办法:把程序中每一句的注释完整的写出来,估计你自己就能发现问题了,而不仅仅只是“此处程序报错,不知道怎么改”。

梦想拥有一台龙芯3A-4000
2014-07-24 01:21
xufan
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:6
帖 子:232
专家分:804
注 册:2008-10-20
收藏
得分:6 
程序代码:
/*

 * File:   main.cpp

 * Author: Administrator

 *

 * Created on 2014年7月23日, 下午9:29

 */
#include "CContact.hpp"
int main(int argc, char *argv[])
{
    CContact *pObj = new CContact();
    if (NULL == pObj)
    {
        std::cerr<<"New Contact Object Failed..."<<std::endl;
        return -1;
    }
    pObj->InitLinkList();
    /*@TEST*/
    int Index = 9;
    static SContactEntry _ContactObject("guwenjie", "15918763209", NULL);
    pObj->AddNode2LinkList(&_ContactObject, Index);
    /*@TEST*/
    pObj->PrintLinkList();
    if (NULL != pObj)
    {
        delete pObj;
        pObj = NULL;
    }
    return 0;
}
***************************
/*

 * File:   CContact.hpp

 * Author: Administrator

 *

 * Created on 2014年7月23日, 下午9:29

 */
#ifndef CCONTACT_HPP
#define    CCONTACT_HPP
/*INCLUDE FILES*/
#include <iostream>
#include <string.h>
#include <string>
/*MACRO DEFINE*/
const int MAX_NAME_LEN = 32;
const int MAX_PHONE_LEN = 32;
const int MAX_CONTACT_SIZE = 7;
/*DATA STRUCTURE ENTRY*/
struct SContactEntry
{
    char m_strName[MAX_NAME_LEN];
    char m_strPhone[MAX_PHONE_LEN];
    SContactEntry *next;
  
    SContactEntry()
    {
        memset(m_strName, '\0', MAX_NAME_LEN);
        memset(m_strPhone, '\0', MAX_PHONE_LEN);
        next = NULL;
    }
    SContactEntry(const SContactEntry& l)
    {
        memcpy(m_strName, l.m_strName, MAX_NAME_LEN);
        m_strName[MAX_NAME_LEN] = '\0';
        memcpy(m_strPhone, l.m_strPhone, MAX_PHONE_LEN);
        m_strPhone[MAX_PHONE_LEN] = '\0';
        next = l.next;
    }
    SContactEntry(const std::string& strName, const std::string& strPhone, \
                    SContactEntry* pNext)
    {
        memcpy(m_strName, strName.c_str(), strName.size());
        m_strName[strName.size()] = '\0';
        memcpy(m_strPhone, strPhone.c_str(), strPhone.size());
        m_strPhone[strPhone.size()] = '\0';
        next = pNext;
    }
};
class CContact {
public:
    CContact();
    ~CContact();
    void InitLinkList(void) const;
    void AddNode2LinkList(SContactEntry *pNode, int& nIndex) const;
    void PrintLinkList(void) const;
private:
    SContactEntry *m_pHead;
};
#endif    /* CCONTACT_HPP */

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

 * File:   CContact.cpp

 * Author: Administrator

 *

 * Created on 2014年7月23日, 下午9:29

 */
#include "CContact.hpp"
#include <iostream>
CContact::CContact()
{
    /*INIT HEAD*/
    m_pHead = new SContactEntry();
    if (NULL == m_pHead)
    {
        std::cerr<<"Init head failed..."<<std::endl;
    }
}
CContact::~CContact()
{
    if (NULL != m_pHead)
    {
        delete m_pHead;
        m_pHead = NULL;
    }
}
void CContact::InitLinkList(void) const
{
    static SContactEntry ContactObject[MAX_CONTACT_SIZE] = {
    SContactEntry("Dennis","0203/123456",NULL), \
    SContactEntry("Chantal","0177/3123345",NULL), \
    SContactEntry("Robert","0163/9295986",NULL), \
    SContactEntry("Bjoern","040 - 123232345",NULL), \
    SContactEntry("Andreas","+49 178 11903123",NULL), \
    SContactEntry("Jenny","+41 119 34544345",NULL), \
    SContactEntry("Zeuss","0162-123-4531698",NULL) \
    };
  
    SContactEntry *p = m_pHead;
    for (int nIndex = 0; nIndex < MAX_CONTACT_SIZE; ++nIndex)
    {
        p->next = &ContactObject[nIndex];
        p       = &ContactObject[nIndex];
    }
}
void CContact::AddNode2LinkList(SContactEntry* pNode, int& nIndex) const
{
    assert(pNode != NULL);
    if (nIndex < 0)
    {
        std::cerr<<"Unkown position...."<<std::endl;
        return;
    }
    //
    SContactEntry *p = m_pHead->next;
    SContactEntry *q = m_pHead;
    int nPos = 0;
  
    while (p)
    {
        ++nPos;
        if (nPos == nIndex)
        {
            q->next     = NULL;
            pNode->next = p;
            q->next     = pNode;
            break;
        }
        else
        {
            q = p;
            p = p->next;  
        }
        if (NULL == p)
        {
            q->next = pNode;
            q       = pNode;
        }
    }
}
void CContact::PrintLinkList(void) const
{
    SContactEntry *p = m_pHead->next;
  
    //
    std::cout<<"Name\tPhone"<<std::endl;
    while (p)
    {
        std::cout<<p->m_strName<<"\t"<<p->m_strPhone<<std::endl;
        p = p->next;
    }}
我写了一个测试程序,仅仅只有插入的部分,你可以参考下:



~~~~~~我的明天我知道~~~。
2014-07-24 09:08
快速回复:初学链表,有关单链表的程序有几处错,求高手们改错或完善
数据加载中...
 
   



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

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