codepad link我試圖用雙指針插入到鏈表中,但我不明白我要去哪裏錯了我跟隨了堆棧溢出的其他鏈接,我甚至提到了幾本書,所以請幫助我.i保留代碼插入位置1.在輸出中,以前的插入丟失。如何使用雙指針插入單向鏈表?
struct node
{
int data;
node *next;
};
void insert(node **head,int k,int pos)//k refers to the element to be inserted
{
if(pos==1)
{
node *newnode=(node *)malloc(sizeof(node));
newnode->data=k;
newnode->next=*head;
*head=newnode;
}
}
void print(node **head)
{
printf("the elements are.. ");
while(*head!=NULL)
{
printf("%d ",(*head)->data);
(*head)=(*head)->next;
}
printf("\n");
}
int main()
{
insert(&head,5,1);
print(&head);
insert(&head,4,1);
print(&head);
return 0;
}
對於可憐的縮進抱歉。我是初學者請幫助我。
這不是有效的C代碼。請通過複製粘貼來發布您正在編譯和運行的* actual *代碼。此外,*告訴我們它不工作的方式*。 –
也許這解釋了你在找什麼:http://www.macs.hw.ac.uk/~rjp/Coursewww/Cwww/linklist.html – theldoria
爲什麼不把它裝入codepad.org或類似的? –