我想使用链表创建一个队列,但我做不到! 我能知道问题出在哪里吗?因为它只让我插入两个值!
typedef struct list
{
int data;
struct list* next;
}list;
void move_forward(list* head,list* node)
{
if(head==NULL) exit(1);
while(head->next!=NULL)
head=head->next;
head->next=node;
}
list* insert(list* head,int value)
{
list* node=(list*)malloc(sizeof(list));
node->data=value;
node->next=NULL ;
if(head==NULL)
head=node;
move_forward(head,node);
return head;
}
给定指向头部的引用(指向指针的指针( 列表和整数,在末尾插入一个新节点
void insertAtEnd(struct Node** head_ref, int new_data)
{
/* 1. allocate node */
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
/* 2. put in the data */
new_node->data = new_data;
/* 3. This new node is going to be the last node, so make next
of it as NULL*/
new_node->next = NULL;
/* 4. If the Linked List is empty, then make the new node as head */
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
/* 5. Else traverse till the last node */
struct Node *last = *head_ref;
while (last->next != NULL)
last = last->next;
/* 6. Change the next of last node */
last->next = new_node;
return;
}