11-> 12-> 13-> 14-> 15-> 12> 16
如果我想刪除12刪除它僅在第一次發生元件即O/p是無線本地環路
11-> 13-> 14-> 15-> 12> 16
,但我想刪除所有發生12次,該怎麼做?
任何人都可以給我一些輸入?
#include<stdio.h>
#include<stdlib.h>
void insertbeg();
void delpos();
void display();
struct node
{
int info;
struct node *link;
}*first=NULL;
struct node *create();
int item,key;
main()
{
int choice;
while(1)
{
printf("\nchoices are:\n");
printf("\n1.Insertbeg\n 2.delpos\n 3.display\n 4.exit\n");
printf("Enter U'r choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: insertbeg(); break;
case 2: delpos(); break;
case 3: display(); break;
case 4: exit(1);
default: printf("INVALID CHOICE TRY AGAIN\n");
}
}
}
struct node *create()
{
struct node *new;
new=(struct node*)malloc(sizeof(struct node));
return(new);
}
void insertbeg()
{
struct node *new;
new=create();
printf("Enter element to be inserted: ");
scanf("%d",&item);
if(first==NULL)
{
new->info=item;
new->link=NULL;
first=new;
}
else
{
new->info=item;
new->link=first;
first=new;
}
}
void delpos()
{
int key;
struct node *temp,*prev;
if(first==NULL)
{
printf("LIST IS EMPTY\n");
return;
}
else
{
temp=first;
printf("Enter the KEY element which is to be deleted: ");
scanf("%d",&key);
/* while(temp->info!=key&&temp->link!=NULL)
{
prev=temp;
temp=temp->link;
}
if(temp->info==key)
{
prev->link=temp->link;
free(temp);
}
else
printf("key element not found in the list\n");
*/
while(temp->link != NULL)
{
if(temp->info == key)
{
prev->link = temp->link;
free(temp);
temp = prev->link;
temp = temp->link;
}
else
temp = temp->link;
}
}
}
void display()
{
struct node *temp;
temp=first;
if(temp==NULL)
{
printf("LIST IS EMPTY\n");
return;
}
else
{
printf("Elements in Linked Lists: ");
while(temp!=NULL)
{
printf("%d->",temp->info);
temp=temp->link;
}
}
}
做出你告訴了變化,但有一個問題。我輸入的數據是這樣的15-> 13-> 14-> 13-> 12-> 11->這裏我刪除了13,所以O/P我得到的是這樣的15-> 12-> 11-> – raj 2011-05-02 12:03:07
@raj讓我有一個快速檢查 – 2011-05-02 15:10:46
我將它作爲一個新的答案發布,因爲它是完全不同的。 – 2011-05-02 16:49:43