我正在编写一个程序,该程序的一部分是一个必须删除LinkedList中最后一个元素的函数:
struct node *del_the_last(struct node *head) {
struct node *h1 = head;
if (head == NULL) {
return NULL;
}
while (head->next != NULL) {
head = head->next;
}
free(h1);
return head;
}
给出值:
16,7,8,12,13,19,21,12
我的程序返回
[12]
然而它应该删除它。所以我再次尝试,这是我最接近的:
struct node *del_the_last(struct node *head) {
struct node *h1 = head;
int flag = 1;
if (head == NULL) { return NULL; }
while (flag == 1) {
if (head->next->next == NULL) {
flag = 0;
head->next = NULL;
free(h1);
return head;
}
else {
head = head->next;
}
}
}
考虑到输出:
2,7,1,8
我的程序返回:
1
另外,我确认了值:
8
通过尝试返回head-> next返回错误
从LinkedList中删除我知道如何继续解决此问题,但我不确定如何将其转换为C代码。
这是我的计划需要做的事情:
我相信这是需要做的,但我不知道如何去做这些
请告诉我正确的方法
答案 0 :(得分:0)
以下是您的示例修改代码。我试着在评论中解释。
struct node *del_the_last(struct node *head) {
struct node *h1 = head,*temp = NULL;
if(h1 != NULL) { /* first check head node is NULL or not */
if(h1->next != NULL) { /* then check head->next */
while(h1->next->next != NULL) {
h1 = h1->next; /* h1 holds previous of last node when while loop terminates */
}
temp = h1->next; /*to free last node, assign h1->next to temp */
h1->next = h1->next->next; /* update the previos of last node */
free(temp);
temp = NULL;
return head;
}
else { /* if there is only one node in the linked list */
head = h1->next; /* make head as NULL */
free(h1); /* free temp ptr */
h1 = NULL; /* assign it to zero,to avoid memory leakage */
return head;
}
}
}
答案 1 :(得分:0)
正如@achal所指出的,如果head->next
在执行NULL
时head->next->next
为struct node* del_the_last(struct node* head){
struct node* curr = head;
// Check if head is NULL
if(head == NULL){
return NULL;
}
// Special case if head is the only node in the list
if(head->next == NULL){
free(head);
return NULL;
}
while(curr != NULL){
/* Check if curr->next is not NULL. If it's not,
see if curr->next->next is NULL, if so, then you
know that curr->next is the last node.
*/
if(curr->next != NULL && curr->next->next == NULL){
free(curr->next);
/* Important to NULL the link to the delete node
so as to signify that now curr->next is the last node
*/
curr->next = NULL;
}else{
curr = curr->next;
}
}
return head;
}
,则可能会出现问题。
2018-05-27T03:01:33.075084+00:00 app[web.1]: 2018-05-27 03:01:33.074 WARN 4 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.alexjamesmalcolm.persistmebaby.PersistMeBabyApplication]; nested exception is java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2SsoDefaultConfiguration due to org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurerAdapter not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)
2018-05-27T03:01:33.723561+00:00 app[web.1]: 2018-05-27 03:01:33.722 ERROR 4 --- [ main] o.s.boot.SpringApplication : Application startup failed
2018-05-27T03:01:33.723574+00:00 app[web.1]:
2018-05-27T03:01:33.723579+00:00 app[web.1]: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.alexjamesmalcolm.persistmebaby.PersistMeBabyApplication]; nested exception is java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2SsoDefaultConfiguration due to org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurerAdapter not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)
2018-05-27T03:01:33.723581+00:00 app[web.1]: at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:618) ~[spring-context-4.3.16.RELEASE.jar!/:4.3.16.RELEASE]
2018-05-27T03:01:33.723583+00:00 app[web.1]: at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:301) ~[spring-context-4.3.16.RELEASE.jar!/:4.3.16.RELEASE]
2018-05-27T03:01:33.723585+00:00 app[web.1]: at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:247) ~[spring-context-4.3.16.RELEASE.jar!/:4.3.16.RELEASE]
2018-05-27T03:01:33.723586+00:00 app[web.1]: at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:200) ~[spring-context-4.3.16.RELEASE.jar!/:4.3.16.RELEASE]
2018-05-27T03:01:33.723588+00:00 app[web.1]: at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:169) ~[spring-context-4.3.16.RELEASE.jar!/:4.3.16.RELEASE]
2018-05-27T03:01:33.723590+00:00 app[web.1]: at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:308) ~[spring-context-4.3.16.RELEASE.jar!/:4.3.16.RELEASE]
2018-05-27T03:01:33.723591+00:00 app[web.1]: at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228) ~[spring-context-4.3.16.RELEASE.jar!/:4.3.16.RELEASE]
2018-05-27T03:01:33.723593+00:00 app[web.1]: at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:272) ~[spring-context-4.3.16.RELEASE.jar!/:4.3.16.RELEASE]
2018-05-27T03:01:33.723595+00:00 app[web.1]: at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:92) ~[spring-context-4.3.16.RELEASE.jar!/:4.3.16.RELEASE]
2018-05-27T03:01:33.723596+00:00 app[web.1]: at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687) ~[spring-context-4.3.16.RELEASE.jar!/:4.3.16.RELEASE]
2018-05-27T03:01:33.723598+00:00 app[web.1]: at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:525) ~[spring-context-4.3.16.RELEASE.jar!/:4.3.16.RELEASE]
2018-05-27T03:01:33.723599+00:00 app[web.1]: at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.12.RELEASE.jar!/:1.5.12.RELEASE]
2018-05-27T03:01:33.723601+00:00 app[web.1]: at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.12.RELEASE.jar!/:1.5.12.RELEASE]
2018-05-27T03:01:33.723603+00:00 app[web.1]: at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.12.RELEASE.jar!/:1.5.12.RELEASE]
2018-05-27T03:01:33.723604+00:00 app[web.1]: at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.12.RELEASE.jar!/:1.5.12.RELEASE]
2018-05-27T03:01:33.723606+00:00 app[web.1]: at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.12.RELEASE.jar!/:1.5.12.RELEASE]
2018-05-27T03:01:33.723607+00:00 app[web.1]: at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.12.RELEASE.jar!/:1.5.12.RELEASE]
2018-05-27T03:01:33.723609+00:00 app[web.1]: at com.alexjamesmalcolm.persistmebaby.PersistMeBabyApplication.main(PersistMeBabyApplication.java:12) [classes!/:na]
2018-05-27T03:01:33.723610+00:00 app[web.1]: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171-heroku]
2018-05-27T03:01:33.723612+00:00 app[web.1]: at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_171-heroku]
2018-05-27T03:01:33.723621+00:00 app[web.1]: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_171-heroku]
2018-05-27T03:01:33.723623+00:00 app[web.1]: at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_171-heroku]
2018-05-27T03:01:33.723624+00:00 app[web.1]: at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [persist-me-baby-0.0.1-SNAPSHOT.jar:na]
2018-05-27T03:01:33.723626+00:00 app[web.1]: at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [persist-me-baby-0.0.1-SNAPSHOT.jar:na]
2018-05-27T03:01:33.723627+00:00 app[web.1]: at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [persist-me-baby-0.0.1-SNAPSHOT.jar:na]
2018-05-27T03:01:33.723629+00:00 app[web.1]: at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [persist-me-baby-0.0.1-SNAPSHOT.jar:na]
2018-05-27T03:01:33.723631+00:00 app[web.1]: Caused by: java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2SsoDefaultConfiguration due to org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurerAdapter not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)
2018-05-27T03:01:33.723632+00:00 app[web.1]: at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:55) ~[spring-boot-autoconfigure-1.5.12.RELEASE.jar!/:1.5.12.RELEASE]
2018-05-27T03:01:33.723634+00:00 app[web.1]: at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:102) ~[spring-context-4.3.16.RELEASE.jar!/:4.3.16.RELEASE]
2018-05-27T03:01:33.723635+00:00 app[web.1]: at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:219) ~[spring-context-4.3.16.RELEASE.jar!/:4.3.16.RELEASE]
2018-05-27T03:01:33.723637+00:00 app[web.1]: at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:608) ~[spring-context-4.3.16.RELEASE.jar!/:4.3.16.RELEASE]
2018-05-27T03:01:33.723639+00:00 app[web.1]: ... 25 common frames omitted
2018-05-27T03:01:33.723640+00:00 app[web.1]: Caused by: java.lang.NoClassDefFoundError: org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurerAdapter
2018-05-27T03:01:33.723642+00:00 app[web.1]: at org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2SsoCondition.getMatchOutcome(EnableOAuth2SsoCondition.java:42) ~[spring-boot-autoconfigure-1.5.12.RELEASE.jar!/:1.5.12.RELEASE]
2018-05-27T03:01:33.723644+00:00 app[web.1]: at org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2SsoDefaultConfiguration$NeedsWebSecurityCondition.getMatchOutcome(OAuth2SsoDefaultConfiguration.java:82) ~[spring-boot-autoconfigure-1.5.12.RELEASE.jar!/:1.5.12.RELEASE]
2018-05-27T03:01:33.723645+00:00 app[web.1]: at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47) ~[spring-boot-autoconfigure-1.5.12.RELEASE.jar!/:1.5.12.RELEASE]
2018-05-27T03:01:33.723647+00:00 app[web.1]: ... 28 common frames omitted
2018-05-27T03:01:33.723648+00:00 app[web.1]: Caused by: java.lang.ClassNotFoundException: org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
2018-05-27T03:01:33.723649+00:00 app[web.1]: at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_171-heroku]
2018-05-27T03:01:33.723651+00:00 app[web.1]: at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_171-heroku]
2018-05-27T03:01:33.723652+00:00 app[web.1]: at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:94) ~[persist-me-baby-0.0.1-SNAPSHOT.jar:na]
2018-05-27T03:01:33.723654+00:00 app[web.1]: at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_171-heroku]
2018-05-27T03:01:33.723655+00:00 app[web.1]: ... 31 common frames omitted
2018-05-27T03:01:33.723657+00:00 app[web.1]: