2011-09-04 113 views
0

考虑包含五个元素的链接列表。 1,2,3,4,5在两个之后插入一个不是'7'。我们将会有一个头指向链表和ptr的最后一个元素。当在3之前插入一个元素时,我们将从头到尾循环遍历链表,并且我们将引入另一个指针(prev)来保存先前的指针address.ptr将指向当前节点,并且如果找到匹配的数据(3 ),那么我们必须包含2到3之间的新节点。 我们可以这样做,因为我们有先前的指针。如何做到这一点而不使用以前的指针。在链接列表中插入元素

编辑:

#include<stdio.h> 
#include<stdlib.h> 
struct list 
{ 
    int data; 
    struct list* link; 
}; 

struct list *head=NULL; 
struct list *tail=NULL; 

void createList(int value); 
void displayList(struct list* head_node); 
void insertNewNode(); 
int value; 


int main() 
{ 
    int i; 
    for(i=0;i<5;i++) 
    { 
    printf("\nEnter the data to be added into the list:\n"); 
    scanf("%d",&value); 
    createList(value); 
    } 
    printf("\nCreated Linked list is\n"); 
    //displayList(head); 
    printf("\nInsert a node\n"); 
    insertNewNode(); 
    displayList(head); 
    return 0; 
} 
void insertNewNode() 
{ 
    int val; 
    struct list* ptr=NULL,*new_node,*prev=NULL; 
    new_node = (struct list*)malloc(sizeof(struct list)); 
    printf("Enter the data to be inserted!"); 
    scanf("%d",&val); 

    for(ptr=head;ptr;ptr=ptr->link) 
    { 
     if(ptr->data == 3) 
     { 
      printf("Found"); 
      new_node->data = val; 
      prev->link=new_node; 
      new_node->link = ptr; 
     } 
     prev = ptr; 
    } 
} 
void createList(int value) 
{ 
    struct list *newNode; 
    newNode = (struct list*)malloc(sizeof(struct list)); 
    //tail = (struct list*)malloc(sizeof(struct list)); 
    newNode->data = value; 
    if(head == NULL) 
    { 
     head = newNode; 
    } 
    else 
    { 
     tail->link = newNode; 
    } 
    tail = newNode; 
    tail->link = NULL; 
} 
void displayList(struct list *head_node) 
{ 
    struct list *i; 
    for(i=head;i;i=i->link) 
    { 
     printf("%d",i->data); 
     printf(" "); 
    } 
    printf("\n"); 
} 
+1

而不是试图描述**你的当前代码,为什么不只是发布一些**实际**代码? –

+0

@Oli:我在这里发布了代码。发布的代码不符合标准。 – Angus

回答

1
void insertNewNode() 
{ 
    int val; 
    struct list* ptr=NULL,*new_node; 
    new_node = (struct list*)malloc(sizeof(struct list)); 
    printf("Enter the data to be inserted!"); 
    scanf("%d",&val); 

    for(ptr=head;ptr;ptr=ptr->link) 
    { 
     if(ptr->data == 2) 
     { 
      printf("Found"); 
      new_node->data = val; 
      new_node->link = ptr->link; 
      ptr->link = new_node; 
     } 
    } 
} 

更新: 这可能是你想要什么:

void insertNewNode() 
{ 
    int val; 
    struct list* ptr=NULL,*new_node; 
    new_node = (struct list*)malloc(sizeof(struct list)); 
    printf("Enter the data to be inserted!"); 
    scanf("%d",&val); 

    for(ptr=head;ptr->link;ptr=ptr->link) 
    { 
     if(ptr->link->data == 3) 
     { 
      printf("Found"); 
      new_node->data = val; 
      new_node->link = ptr->link; 
      ptr->link = new_node; 
     } 
    } 
} 

这里:

if(ptr->link->data == 3) 

您只需向前看,以检查下一个节点是否具有您需要的值。

+0

谢谢develerx,但这是一个搜索插入后2.但如何做一个搜索插入3之前,而不使用prev指针。 – Angus

+0

我做了更新。 – develerx

+0

谢谢develrex.It是我绝对想要的。我打破了我的头,以找出这个简单的概念。谢谢。 – Angus

0

我们称之为curr指针在当前元素,next指针下一个单元,并存储value数量。

遍历列表,直到curr.value == 2,现在只是创建new_node.value = 7一个new_node并设置new_node.next = curr.nextcurr.next = new_node