| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 573 人关注过本帖
标题:初学链表,有关单链表的程序有几处错,求高手们改错或完善
取消只看楼主 加入收藏
wfjimy
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2014-5-15
结帖率:100%
收藏
已结贴  问题点数:6 回复次数:2 
初学链表,有关单链表的程序有几处错,求高手们改错或完善
建立一个结构体可存储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
wfjimy
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2014-5-15
收藏
得分:0 
回复 2 楼 embed_xuel
我不是很明白链表,所以能否帮我改一下程序,谢谢啦
2014-07-23 21:10
wfjimy
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2014-5-15
收藏
得分:0 
回复 4 楼 funyh250
是自己写的,怎么着把各个function 连起来,不是很明白链表
2014-07-24 00:42
快速回复:初学链表,有关单链表的程序有几处错,求高手们改错或完善
数据加载中...
 
   



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

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