#include <stdio.h>
#include <stdlib.h>
#include "linklist.h"
linklist Create_List(int n){//創建n個長度的鏈表
int i=0;
linklist head,L,prep;
if(n<=0){
puts("invalid element input!");
exit(0);
}
head=prep=(linklist)malloc(sizeof(listnode));
if(head == NULL){
puts("malloc failed!");
return head;
}
for(i=1; i<=n; i++){
if((L=(linklist) malloc (sizeof(listnode))) == NULL){
puts("failure to apply new space!");
return head;
}
else{
L->data = i;
prep->next = L;
prep = L;
}
}
prep->next = NULL;
free(L);
return head;
}
int Insert_List(linklist head, int pos){//插入第pos位置的一個鏈表
linklist p;
p = head;
linklist L = (linklist)malloc(sizeof(listnode));
L ->data = 99;
L->next = NULL;
int i=0;
if (head ==NULL){
puts ("this list is null");
return -1;
}
if(L==NULL){
puts("new list is NULL");
return -1;
}
if(pos<=0){
puts("an error occurred in the location of the new list");
return -1;
}
else{
while(p->next!=NULL){
p=p->next;
i++;
if(i==pos){
printf("yes\n");
L->next=p->next;
p->next = L;
free(L);
return 0;
}
}
puts("cross-border access");
return -1;
}
}
int Is_Empty_List(linklist head){//是否為空 ,我沒用這個函式
if(head->next==NULL){
puts("this list is empty");
return -1;
}
else{
return 1;
}
}
void Show_list(linklist head){//列印鏈表
if(head == NULL){
puts("the head is null!");
return;
}
int i=0;
linklist L;
L = head;
while(L->next != NULL){
i++;
printf("the number %d -> data is %d\n",i,L->data);
L=L->next;
}
return;
}
uj5u.com熱心網友回復:
#include <stdio.h>
#include <stdlib.h>
/*
typedef struct link {
int data;
struct link *next;
}*linklist, listnode;
//*/#include "linklist.h"
linklist Create_List(int n)
{//創建n個長度的鏈表
int i=0;
linklist head,L,prep;
if(n<=0){
puts("invalid element input!");
exit(0);
}
head=prep=(linklist)malloc(sizeof(listnode));
if(head == NULL){
puts("malloc failed!");
return head;
}
for(i=1; i<=n; i++){
if((L=(linklist) malloc (sizeof(listnode))) == NULL){
puts("failure to apply new space!");
return head;
}
else{
L->data = i;
prep->next = L;
prep = L;
}
}
prep->next = NULL;
//free(L); //為什么要釋放L呢,L已經加入到head鏈表里了
return head;
}
int Insert_List(linklist head, int pos){//插入第pos位置的一個鏈表
linklist p;
p = head;
linklist L = (linklist)malloc(sizeof(listnode));
L ->data = 99;
L->next = NULL;
int i=0;
if (head ==NULL){
puts ("this list is null");
return -1;
}
if(L==NULL){
puts("new list is NULL");
return -1;
}
if(pos<=0){
puts("an error occurred in the location of the new list");
return -1;
}
//else{
while(p->next!=NULL){
p=p->next;
i++;
if(i==pos){
printf("yes\n");
L->next=p->next;
p->next = L;
//free(L);//為啥釋放呢?
return 0;
}
}
free(L);//為啥不釋放呢?
puts("cross-border access");
return -1;
// }
}
int Is_Empty_List(linklist head){//是否為空 ,我沒用這個函式
if(head->next==NULL){
puts("this list is empty");
return -1;
}
else{
return 1;
}
}
void Show_list(linklist head){//列印鏈表
if(head == NULL){
puts("the head is null!");
return;
}
int i=0;
linklist L;
L = head->next;
//while(L->next != NULL){
while(L != NULL){
i++;
printf("the number %d -> data is %d\n",i,L->data);
L=L->next;
}
return;
}
/*
int main(void)
{
linklist phead;
phead = Create_List(10);
Insert_List(phead, 5);
Show_list(phead);
}*/
供參考~
不該釋放的地方釋放了,該釋放的地方沒有釋放~
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/100335.html
標籤:C語言
上一篇:萌新求助
下一篇:新手求助
