2011-09-04 225 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