2011-05-19 39 views
-2

我得到了我的方式...现在正在工作...没有全局指针..检查。 。在c中写一个函数,我们可以在其中添加多个单独列表... [已解决]

#include<stdio.h> 
#include<conio.h> 

struct list 

{ 

    int n; 
    struct list *next,*prev; 
}; 

typedef struct list list; 

list *create(list *head) 

{ 

    list *tmp,*cur; 
    tmp=(list *)malloc(sizeof(list)); 

    printf("\nEnter Value.. :: "); 
    scanf("%d",&tmp->n); 

    tmp->next=NULL; 
    tmp->prev=NULL; 

    if(head==NULL) 
    { 

     head=tmp; 
     cur=tmp; 
    } 
    else 
    { 

     cur=head; 
     while(cur->next!=NULL) 
      cur=cur->next; 

     cur->next=tmp; 
     tmp->prev=cur; 
    } 
    return head; 

} 

void disp(list *head) 

{ 

    list *cur; 
    if(head==NULL) 
     printf("\n\nEmpty List..!!!"); 
    else 
    { 

     printf("\n\nLIST ::\t"); 
     cur=head; 
     while(cur!=NULL) 
     { 

      if(cur->next!=NULL) 
       printf(" %d =>",cur->n); 
      else 
       printf(" %d ",cur->n); 

      cur=cur->next; 
     } 
    } 
} 


void main() 

{ 

    list *first=NULL,*second=NULL; 
    int ch; 
    while(ch!=0) 
    { 
     clrscr(); 
     printf("\n\t********** SIMPLE DOUBLY LINKED LIST PROGRAM **********"); 
     printf("\n\n\t1. Create First List..."); 
     printf("\n\t2. Create Second List..."); 
     printf("\n\t3. Display Both Lists..."); 
     printf("\n\t0. Exit..."); 

     printf("\n\tEnter Your Choice.. :: "); 
     scanf("%d",&ch); 

     switch(ch) 
     { 

      case 1: first=create(first); 
        disp(first); 
        break; 

      case 2: second=create(second); 
        disp(second); 
        break; 

      case 3: disp(first); 
        disp(second); 
        break; 
     } 
     getch(); 
    } 
} 
+2

你的问题不是很清楚。请张贴您的代码,并更清楚地解释您的代码的确切问题:至少,输入是什么,预期的和实际的输出。 – 2011-05-19 15:28:12

+3

您的问题尚不清楚,请重新填写。 – Pepe 2011-05-19 15:28:24

+0

这个问题很不清楚。请尝试再次提问并解释你想要这个“添加”功能。 – 2011-05-19 15:29:00

回答

1

除非你使用全局变量(不使用全局变量!),你需要通过所有你想要的元素添加到列表和元素添加到该函数。

有几种可能的原型:我可能会选择通过列表的数量和清单自身的可变参数

void add(struct node *element, size_t nlists, ...); 

可能被称为

add(&mynode, 3, mainlist, worklist, templist); 
add(&mynode, 1, worklist); 

另一种方式将创建接受一定数目的列表的函数,但允许NULL值表示该参数没有列表:

void add(struct node *element, struct list *list1, struct list *list2, struct list *list3); 

可能被称为

add(&mynode, mainlist, worklist, templist); 
add(&mynode, NULL, worklist, NULL); 
相关问题