将数组元素添加到链接列表

时间:2018-10-16 11:48:50

标签: c++ c

我有数组元素ar1[i],我想将此数组的数据添加到链接列表中

struct node
{
  int data;
  struct node*next;
};


void create(struct node *head,int ar1[] int i1)
{
struct node *temp, *p, *q;
int i;


head = (struct node*)malloc(sizeof(struct node));
temp = (struct node*)malloc(sizeof(struct node));


for (i=0; i<i1; i++) //lets say i1 is no. of data in array
{
    if(head == NULL)
    {
        head->data = ar1[i];
        head->next = NULL;
    }
    else
    {
        temp->data = ar1[i];
        temp->next = NULL;

        p = head;
        while(p->next != NULL)
            p = p->next;
        p->next = temp;
    }

}

我做了这个程序,但是没有用。它正在接受输入数据,但不显示输出,也不终止程序。

编辑:正如每个人在评论中建议的那样,我尽力了并提出了解决方案。实际上我的程序是从用户那里获取最多10位数字并对其执行算术运算。

这是我的代码:

   /*
     WAP to store at most 10 digit integer in a Singly linked list and 
     perform
      arithmetic operations on it.
   */

   #include<iostream>
   using namespace std;

   struct node
   {
     int data;
    struct node*next;
   };

void create(struct node *head,int ar1[],int ar2[], int i1, int i2)
{
struct node *newNode, *temp;
int i=0;

head = (struct node *)malloc(sizeof(struct node));

if(head == NULL)
{
    cout<<"Unable to allocate memory.";
}
else
{

    head->data = ar1[i]; // Link the data field with data
    head->next = NULL; // Link the address field to NULL

    temp = head;

    for(i=1; i<i1; i++)     //Create n nodes and adds to linked list
    {
        newNode = (struct node *)malloc(sizeof(struct node));

        if(newNode == NULL)     

        {
            cout<<"Unable to allocate memory.";
            break;
        }
        else
        {


            newNode->data = ar1[i]; 
            newNode->next = NULL;

            temp->next = newNode; 
            temp = temp->next; 
        }
    }
}

temp = head;

while(temp!=NULL)
{
    cout<<temp->data<<" ";
    temp= temp->next;
}   

}

int main()
{
struct node*head = NULL ;
int n1,n2;
int i1,i2,choice;
int ar1[10],ar2[10];

head = (struct node*)malloc(sizeof(struct node));

cout<<"Enter numbers you want to perform operations on\n1). ";
cin>>n1;
cout<<"2). ";
cin>>n2;

i1=0; i2=0;

while(n1)       //getting each digit of given number
{

    ar1[i1] = n1 %10;
    n1 /= 10;
    i1= i1 + 1;     
}

while(n2)       //getting each digit of given number
{

    ar2[i2] = n2 % 10;
    n2 /= 10;
    i2++;   
}

cout<<"\nChoose what operation you want to 
 perform:\n1.Addition\n2Subtraction\n";
cin>>choice;

create(head,ar1,ar2,i1,i2);
/*if(choice == 1)  I comment out this part i.e. not important rn.
{
    add(ar1,ar2);
}
else if (choice == 2)
    sub();
else
{
    cout<<"Please chose valid data\n";
}
*/
return 0;
}

2 个答案:

答案 0 :(得分:1)

有很多问题。create函数的整个逻辑是错误的:

void create(struct node *head, int ar1[], int i1)
{
  struct node *temp, *p, *q;
  int i;

  head = (struct node*)malloc(sizeof(struct node));  // Problem 1
  temp = (struct node*)malloc(sizeof(struct node));

  for (i = 0; i < i1; i++)
  {
    if (head == NULL)                                // Problem 2
    {
      head->data = ar1[i];  
      head->next = NULL;
    }
    else
    {
      temp->data = ar1[i];
      temp->next = NULL;

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

      p->next = temp;                                // Problem 3
    }
  }
}


...
create(myhead, myarray, maysize);                    // Problem 4
...

问题1

您分配内存的时间过早,应该仅在确实需要时才分配内存。

问题2

部分是由问题1引起的:head在这里不能NULL(假设malloc不会失败,但这是另一回事),因为您已将其分配给非{{ 1}}之前的值。

问题3

由于上面的NULL循环,此处p->next总是NULL

问题4

假设您while喜欢:

create

... myhead = NULL; ... create(myhead, myarray, maysize); 在呼叫myhead之后仍将是NULL,您应该阅读this SO article

答案 1 :(得分:0)

  

只需使用纸和铅笔了解逻辑,然后   依次优化代码。

struct node
{
    int val;
    struct node *next;
};
struct node *head=NULL, *last=NULL;

void insert( int value)
{
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->val=value;
    tmp->next=NULL;
    if( head == NULL )
    {
        head=tmp;
        last=tmp;
    }
    else
    {
        last->next=tmp;
        last=tmp;
    }
}

void printlist()
{
    struct node *tmp=head;
    printf("The list is:");
    while( tmp != NULL )
    {
        printf(" %d", tmp->val);
        tmp=tmp->next;
    }
    printf("\n");
}
void search( )
{
    printf("Which value to Search?");
    int n, k=0;
    scanf("%d",&n);
    struct node *tmp=head;
    while( tmp != NULL )
    {
        if( tmp->val == n )
            k=1;
        tmp=tmp->next;
    }
    if( k )
        printf("Yes\n");
    else
        printf("No\n");
}
void deletenode( )
{
    int n, posi;
    printf("Where to Delete?\n");
    printf("1\tFirst Position\n");
    printf("2\tLast Position\n");
    printf("3\tAny Desired Position\n");
    scanf("%d",&n);
    if( n == 1 )
    {
        struct node *tmp=head;
        tmp=tmp->next;
        head=tmp;
        printlist();
    }
    else if( n == 2 )
    {
        struct node *tmp=head ;
        while( 1 )
        {
            if( tmp->next->next == NULL )
            {
                tmp->next=NULL;
                break;
            }
            else
                tmp=tmp->next;
        }
        printlist();
    }
    else if( n == 3 )
    {
        int i=1;
        printf("Enter Position: ");
        scanf("%d",&posi);
        struct node *tmp=head;
        while( tmp->next != NULL && i <= posi )
        {
            if( i+1 == posi )
            {
                tmp->next=tmp->next->next;
                break;
            }
            else
            {
                i++;
                tmp=tmp->next;
            }
        }
        printlist();
    }
}

void ins()
{
    int n, v, posi;
    printf("Where to insert?\n");
    printf("1\tFirst Position\n");
    printf("2\tLast Position\n");
    printf("3\tAny Desired Position\n");
    scanf("%d",&n);
    if( n == 1 )
    {
        printf("Enter value: ");
        scanf("%d",&v);
        struct node *tmp;
        tmp=(struct node *)malloc( sizeof(struct node));
        tmp->val=v;
        tmp->next=head;
        head=tmp;
        printlist();
    }
    else if( n == 2 )
    {
        printf("Enter value: ");
        scanf("%d",&v);
        struct node *tmp;
        tmp=(struct node *)malloc(sizeof(struct node));
        tmp->val=v;
        tmp->next=NULL;
        last->next=tmp;
        last=tmp;
        printlist();
    }
    else if(n == 3 )
    {
        int i=1;
        printf("Enter position: ");
        scanf("%d",&posi);
        printf("\nEnter value: ");
        scanf("%d",&n);
        struct node *tmp1, *tmp=head, *tmp2;
        tmp1=(struct node *)malloc(sizeof(struct node));
        tmp1->val=n;
        tmp1->next=NULL;
        while( tmp->next != NULL && i <= posi)
        {
            if( i+1 == posi )
            {
                tmp2=tmp->next;
                tmp->next=tmp1;
                tmp1->next=tmp2;
            }
            i++;
            tmp=tmp->next;
        }
        printlist();
    }
}
int main()
{
    int n;
    printf("Enter value & Enter -1 to exit.\n");
    while( 1 )
    {
        scanf("%d",&n);
        if( n < 0 )
            break;
        insert(n);
    }
    printlist();
    printf("Enter choice:\n\n");
    printf("1\tInsert\n");
    printf("2\tDelete\n");
    printf("3\tSearch\n");
    printf("4\tExit\n");
    scanf("%d",&n);
    if( n == 1 )
        ins();
    else if( n == 2 )
        deletenode();
    else if( n == 3 )
        search();
    else if( n == 4 )
        return 0;
    return 0;
}