#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct Node{
int data; //資料域
struct Node *next; //指標域
}node, *PNODE;
//函式宣告
PNODE create_list();
void show_list(PNODE);
bool empty_list(PNODE);
void length_list(PNODE);
bool insert_list(PNODE,int,int);
int main(){
PNODE L = NULL; //
L = create_list(); //創建非回圈單鏈表,把鏈表頭節點地址賦給head 動態記憶體分配
show_list(L);
empty_list(L);
insert_list(L,3,5);
show_list(L);
length_list(L);
return 0;}PNODE create_list() //創建函式
{
int len,val,i;
PNODE phead =(PNODE)malloc(sizeof(node));
if (phead == NULL)
{
printf("記憶體分配失敗");
exit(-1);
}
PNODE p=phead;
p->next =NULL;
printf("請輸入鏈表個數:");
scanf("%d",&len);
for(i=0;i<len;i++)
{
printf("第%d個結點的值為:",i+1);
scanf("%d",&val);
PNODE pnew =(PNODE)malloc(sizeof(node));
if (pnew == NULL)
{
printf("記憶體分配失敗");
exit(-1);
}
pnew->data = val;
p->next = pnew;
pnew->next = NULL;
p = pnew;
}
return phead;}void show_list(PNODE phead) //遍歷輸出
{
PNODE p = phead->next;
while(p != NULL)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
bool empty_list(PNODE phead) //判斷鏈表是否為空
{ if(phead->next=NULL)
return true;
}
void length_list(PNODE phead)
{
int i=0;
PNODE p = phead->next;
while(p !=NULL)
{
i++;
p=p->next;
}
printf("鏈表長度為 %d",i);
}
bool insert_list(PNODE phead,int n,int m) //插入第n個元素m
{
int i = 0;
PNODE p = phead,s;
if(n < 1) return false;
while(i < n-1 && p!= 0)
{
i++;
p = p-> next;
}
if(p = NULL) return false;
else{
s=(PNODE)malloc(sizeof(node));
s->data=https://bbs.csdn.net/topics/m;
s->next=p->next;
p->next=s;
return true;
}
}
uj5u.com熱心網友回復:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct Node{
int data; //資料域
struct Node *next; //指標域
}node, *PNODE;
//函式宣告
PNODE create_list();
void show_list(PNODE);
bool empty_list(PNODE);
void length_list(PNODE);
bool insert_list(PNODE,int,int);
int main()
{
PNODE L = NULL; //
L = create_list(); //創建非回圈單鏈表,把鏈表頭節點地址賦給head 動態記憶體分配
show_list(L);
empty_list(L);
insert_list(L,3,5);
show_list(L);
length_list(L);
return 0;
}
PNODE create_list() //創建函式
{
int len,val,i;
PNODE phead =(PNODE)malloc(sizeof(node));
if (phead == NULL)
{
printf("記憶體分配失敗");
exit(-1);
}
PNODE p=phead;
p->next = NULL;
printf("請輸入鏈表個數:");
scanf("%d",&len);
for(i=0;i<len;i++)
{
printf("第%d個結點的值為:",i+1);
scanf("%d",&val);
PNODE pnew =(PNODE)malloc(sizeof(node));
if (pnew == NULL)
{
printf("記憶體分配失敗");
exit(-1);
}
pnew->data = val;
p->next = pnew;
pnew->next = NULL;
p = pnew;
}
return phead;
}
void show_list(PNODE phead) //遍歷輸出
{
PNODE p = phead->next;
while(p != NULL)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
bool empty_list(PNODE phead) //判斷鏈表是否為空
{
//if(phead->next=NULL)
if(phead->next==NULL)
return true;
else
return false;
}
void length_list(PNODE phead)
{
int i=0;
PNODE p = phead->next;
while(p !=NULL)
{
i++;
p=p->next;
}
printf("鏈表長度為 %d",i);
}
bool insert_list(PNODE phead,int n,int m) //插入第n個元素m
{
int i = 0;
PNODE p = phead,s;
if(n < 1)
return false;
while(i < n-1 && p!= 0)
{
i++;
p = p-> next;
}
//if(p = NULL)
if(p == NULL)
return false;
else{
s=(PNODE)malloc(sizeof(node));
s->data=https://bbs.csdn.net/topics/m;
s->next=p->next;
p->next=s;
return true;
}
}
供參考~
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/231230.html
標籤:C語言
上一篇:CMD 計劃任務 使用最高權限運行方法 schtasks
下一篇:這個程式該怎么解釋?
