我的函数应该在给定位置后插入新节点,或者如果位置是尾部,则将其插入尾部并使新节点尾部
它适用于在给定位置插入节点,但在尾部后插入时不起作用。当我运行代码插入后尾部我得到一个分段错误。任何帮助将不胜感激!
ListNode *insertAfter(ListNode *argNode, int value) {
ListNode *newNode = new ListNode(value);
if(argNode != tail){
argNode->next = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
size++;
return newNode;
}
答案 0 :(得分:1)
让链接列表在下面。
Node1 -> Node2 -> NULL
Tail=Node2
如果您将Node3插入到最后。
Tail->Next = Node3
Node3->Next = NULL
So, the result is
Node1->Node2->Node3-> NULL
没关系。但是,如果将Node3插入Node1之后的位置。
ArgNode(Node1)
prev: Node1->Node2->NULL
next: Node1->Node3->NULL. Node2->NULL.
问题是Node3的下一个位置缺失。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct ListNode {
int value ;
ListNode *next ;
ListNode(int v) { value=v; next=NULL; }
};
int size=0 ;
ListNode *head = NULL;
ListNode *tail=NULL ;
ListNode *insertAfter(ListNode *argNode, int value) {
ListNode *newNode = new ListNode(value);
if ( argNode==NULL ) {
if ( head==NULL ) {
// initialize first node.
head = tail = newNode ;
newNode->next=NULL ;
} else {
// insert first node
newNode->next = head ;
head = newNode ;
}
}
else if(argNode != tail){
newNode->next = argNode->next;
argNode->next = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
size++;
return newNode;
}
void printList(ListNode *a) {
while ( a!=NULL ) {
printf("%d ", a->value) ;
a=a->next ;
}
printf(" size=%d\n", size) ;
}
int main() {
ListNode *n, *n3, *n2 ;
head =insertAfter(NULL, 10) ;
printList(head) ;
n2 = insertAfter(tail, 20) ;
printList(head) ;
n3 = insertAfter(tail, 30) ;
printList(head) ;
n = insertAfter(tail, 40) ;
printList(head) ;
n = insertAfter(n3, 35) ;
printList(head) ;
n = insertAfter(n2, 25) ;
printList(head) ;
n = insertAfter(NULL, 5) ;
printList(head) ;
return 0 ;
}
输出就是这个。
10 size=1
10 20 size=2
10 20 30 size=3
10 20 30 40 size=4
10 20 30 35 40 size=5
10 20 25 30 35 40 size=6
5 10 20 25 30 35 40 size=7
答案 1 :(得分:0)
{% load static from staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<style>
body{
background: url('{% static "img/new4.jpg" %}') no-repeat center fixed;
- webkit- background: cover;
- moz - background: cover;
- o - background: cover;
background-size: cover;
}
</style>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Personality Leading.com</title>
<!-- Bootstrap core CSS -->
<link href="{% static 'vendor/bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
<!-- <link rel="stylesheet" type="text/css" href="{% static 'vendor/bootstrap/css/Custom' %}"> -->
<link rel="stylesheet" type="text/css" href="{% static 'vendor/bootstrap/css/Custom.css' %}">
<!-- Custom fonts for this template -->
<link href="{% static 'vendor/font-awesome/css/font-awesome.min.css' %}" rel="stylesheet" type="text/css">
<link href='https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
<!-- Custom styles for this template -->
<link href="{% static 'css/clean-blog.min.css' %}" rel="stylesheet">
</head>
<body>
<!-- Navigation -->
<div class="mynav">
<div class="p-3 mb-2 bg-info text-white"><strong> PERSONALITY LEADING </strong></div>
<nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
<div class="container">
<a class="navbar-brand" href="index.html"></a>
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
Menu
<i class="fa fa-bars"></i>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="/home/">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/about/">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/services/">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/contact/">Contact</a>
</li>
</ul>
</div>
</div>
</nav>
</div>
</div>
<br>
<br>
<br>
<br>
插入argNode->next = newNode;
之前。如果不是,那么你就会破坏链表中的链,因为你忘了将argeNode-&gt;下一个值附加到链中。