首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

C語言關於linkedlist的編程

2012-02-12 
求助 C語言關於linkedlist的編程麻煩大大幫個忙我實在是做不出來可以紀錄50組按1增加紀錄按2可刪除所選擇

求助 C語言關於linkedlist的編程
麻煩大大幫個忙
我實在是做不出來

可以紀錄50組
按1增加紀錄
按2可刪除所選擇的紀錄
按3可看之前所有的紀錄
按ESC結束


#include   <stdio.h>
#include   <stdlib.h>
#include   "linkedlist.h "

int   main(int   argc,   char   *argv[])
{
        int   i;
        struct   Info   info;
        char   selection;
       
     
        printf( "Welcome   to   the   storage   room.\n\n ");
       
       
     
                printf( "Please   select   an   option   that   you   wish. ");
                printf( "\n*************************************\n ");    
                printf( "Press   1   to   add   a   record.\n ");  
                printf( "Press   2   to   delete   a   record.\n ");  
                printf( "Press   3   to   view   all   records.\n ");  
                printf( "Press   'ESC '   to   exit. ");  
                printf( "\n*************************************\n ");                                  
                printf( "\n ");
               
      selection   =   getch();
      switch(   selection   )  
                {  
                     
                        case   '1 ':
                                                                                                         
   
                                        if   (isEmptyList())
                                        {
                                                printf( "The   list   is   empty.\n ");
                                        }        
                                                       


                                                   
       
                                               
                                                for   (i=0;   i <50;   i++)
                                                {        
                                                                                                                                                                                                                           
                                                          printf( "record   number:   ");
                                                          scanf( "   %d ",   &info.record);
                                                         
                                                          printf( "Name   of   compenent   type:   ");
                                                          fflush(stdin);
                                                          gets(info.type);
                                                          printf( "The   value   of   component:   ");
                                                          fflush(stdin);
                                                          gets(info.value);


                                                         
                                                          printf( "How   many   in   the   stock:   ");
                                                          scanf( "   %d ",   &info.number);
                                                         
                                                   
                                                          insertNode(&info);
                                                }
                        case   27:
                        printf( "Bye.\n\n ");
                        break;
                           
                        default:
                        printf( "Your   selection   is   not   valid.\n ");
                        printf( "Please   try   again\n ");  
                        printf( "\n ");
                }
       
 
        rewindList();
        retrieveNode(&info);
        printf( "record   number:   %d\t   compenent   type:   %s\t   component   value:   %s\t   stock:   %3d\n\n "
        ,   info.record,   info.type,   info.value,   info.number);
       
                        do
                        {
                             
                                  retrieveNode(&info);
                                  printf( "record   number\t%d\n ",   info.record);


                                  printf( "compenent   type:\t%s\n ",   info.type);
                                  printf( "component   value:\t%s\n ",   info.value);
                                  printf( "stock:\t%3d\n ",   info.number);
                        }   while   (traversalList());
   
   
        rewindList();
        traversalList();
        deleteNode();
       
   
        rewindList();
                        do
                        {
                             
                                  retrieveNode(&info);
                                  printf( "record   number\t%d\n ",   info.record);
                                  printf( "compenent   type:\t%s\n ",   info.type);
                                  printf( "component   value:\t%s\n ",   info.value);
                                  printf( "stock:\t%3d\n ",   info.number);
                        }   while   (traversalList());
       

        while(traversalList());
        deleteNode();
       
   
        rewindList();
                        do
                        {
                           
                                  retrieveNode(&info);
                                  printf( "record   number\t%d\n ",   info.record);
                                printf( "compenent   type:\t%s\n ",   info.type);
                                  printf( "component   value:\t%s\n ",   info.value);
                                  printf( "stock:\t%3d\n ",   info.number);


                        }   while   (traversalList());
       
    system( "PAUSE ");
    return   0;
}


以下是linkedlist的編程(這是正確的)
#include   <stdio.h>
#include   <stdlib.h>
#include   "linkedlist.h "


void   insert();
void   create();
void   assign(struct   Info   *info);


struct   Node   *head=NULL,   *current=NULL,   *temp=NULL;


int   isEmptyList()
{
        return   (head==NULL);
}


void   rewindList()
{
          current   =   head;
}


int   traversalList()
{
        if   (current==NULL)   return   0;
        if   (current-> link==NULL)   return   0;
        current   =   current-> link;
        return   1;
}

void   insert()          
{
          if   (head   ==   NULL)
          {
                head   =   temp;
                current   =   head;
          }
          else
          {
                  temp-> link   =   current-> link;
                  current-> link   =   temp;
                  current   =   current-> link;
          }
}


void   deleteNode()
{
          if   (head==NULL)   return;
          if   (current==head)
          {
                    head   =   current-> link;
                    free(current);
                    current   =   head;
          }
          else
          {
                  temp   =   head;
                  while(1)
                  {
                            if   (temp-> link==current)   break;
                            temp   =   temp-> link;
                  }
                  temp-> link   =   current-> link;
                  free(current);
                  current   =   temp-> link;


                  if   (current==NULL)   current   =   head;
          }
}


void   create()
{
          temp   =   (struct   Node   *)malloc(sizeof(struct   Node));
          temp-> link   =   NULL;
}


void   assign(struct   Info   *info)
{
          if   (temp==NULL)   return;
          memcpy(&temp-> info,info,sizeof(struct   Info));
}


void   retrieveNode(struct   Info   *info)
{
          if   (head==NULL   ||   current==NULL)   return;
          memcpy(info,&current-> info,sizeof(struct   Info));
}


void   insertNode(struct   Info   *info)
{
          create();
          assign(info);
          insert();
}


[解决办法]
deleteNode(); 的调用非常不合理 ...
[解决办法]
#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h "

int main(int argc, char *argv[])
{
int i;
struct Info info;
char selection;

printf( "Welcome to the storage room.\n\n ");
printf( "Please select an option that you wish. ");
printf( "\n*************************************\n ");
printf( "Press 1 to add a record.\n ");
printf( "Press 2 to delete a record.\n ");
printf( "Press 3 to view all records.\n ");
printf( "Press 'ESC ' to exit. ");
printf( "\n*************************************\n ");
printf( "\n ");

selection = getch();
switch( selection )
{
case '1 ':
if (isEmptyList())
{
printf( "The list is empty.\n ");
}

for (i=0; i <50; i++)
{
printf( "record number: ");
scanf( " %d ", &info.record);
printf( "Name of compenent type: ");
fflush(stdin);
gets(info.type);
printf( "The value of component: ");
fflush(stdin);
gets(info.value);
printf( "How many in the stock: ");
scanf( " %d ", &info.number);
insertNode(&info);
}
case 27:
printf( "Bye.\n\n ");
break;

case '2 ': //调用 deleteNode() 释放节点,current 赋值为节点指针
//while 查找要删除的记录, 找到后调用 deleteNode()
while(traversalList()-> value != ??); //?? 是你要删除的记录的 value,或者可以是其他成员
deleteNode();
break;

case '3 ':
rewindList();
do
{
retrieveNode(&info);
printf( "record number\t%d\n ", info.record);
printf( "compenent type:\t%s\n ", info.type);
printf( "component value:\t%s\n ", info.value);
printf( "stock:\t%3d\n ", info.number);
} while (traversalList());
break;

default:
printf( "Your selection is not valid.\n ");
printf( "Please try again\n ");
printf( "\n ");
break;
}

system( "PAUSE ");
return 0;
}

热点排行