當前位置:首頁 » 編程語言 » 鏈表插入元素c語言
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

鏈表插入元素c語言

發布時間: 2022-01-16 03:05:18

c語言單鏈表怎麼插入節點

#include"sll_node.h"
#include<stdlib.h>

#defineFALSE0
#defineTRUE1

//insertNode2:把newValue的值插入到遞增排序的鏈表中,正確返回TRUE,錯誤返回FALSE
//nextp是指向當前節點的指針,最初是頭指針
intinsertNode2(Node**nextp,intnewValue)
{
Node*newNode;//新節點指針
Node*current;//當前節點指針

current=*nextp;//最初當前節點為nextp指針指向的節點
//查找新插入節點的位置
while(current!=NULL&&current->value<newValue)
{
nextp=¤t->next;
current=current->next;
}

//為新節點分配內存
newNode=(Node*)malloc(sizeof(Node));
if(newNode==NULL)
returnFALSE;
newNode->value=newValue;

//統一了插入的步驟。即:每次插入,都是前一個指針指向新節點,新節點指向下一個節點
*nextp=newNode;
newNode->next=current;

returnTRUE;
}

main函數
[cpp]viewplain
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include"sll_node.h"

intinsertNode(Node**rootp,intnewValue);
intinsertNode2(Node**nextp,intnewValue);

intmain()
{
srand(time(0));

Node*head=(Node*)malloc(sizeof(Node));
head->next=NULL;

for(inti=0;i<5;i++)
{
inttemp=rand()%50;
printf("%d ",temp);
//insertNode(&head,temp);
insertNode2(&head,temp);
}

Node*p=head->next;
while(p!=NULL)
{
printf("%d ",p->value);
p=p->next;
}

getchar();
getchar();
return0;
}

⑵ c語言鏈表插入元素,再插入函數中總是會報錯

怎麼我試了插入沒看到出錯

⑶ c語言鏈表插入元素的問題

h代表鏈表頭指針;
&a[0]是數組首地址
5代表數組a的數組元素的個數

⑷ C語言鏈表插入

char data[4]?
結點data是字元串?

抽時間幫你寫了一個
有什麼不合要求的地方你自己改改吧

#include <stdio.h>
#include <string.h>
#include <malloc.h>

typedef struct Lnode{
char *data;
struct Lnode *next;
}Lnode, *LinkList;

void CreateList(LinkList &L, char *buff)
{
int flag=0, j=0;
int len=strlen(buff);

L=(Lnode *)malloc(sizeof(Lnode));
L->next=NULL;
LinkList q=L;
for(int i=0;i<len;i+=4)
{
j=0;
LinkList p=(Lnode *)malloc(sizeof(Lnode));
q->next=p;
p->next=NULL;
p->data=(char *)malloc(sizeof(char)*5);
while(j<4)
{
p->data[j++]=buff[flag++];
}
p->data[4]='\0';
q=q->next;
}
}//初始化鏈表L

void TraverseList(LinkList L)
{
LinkList p;
p=L->next;
while(p)
{
printf("%s",p->data);
p=p->next;
}
printf("\n");
}//遍歷鏈表L

void InsertList(LinkList &L,int i,char *ins)
{
LinkList p=L;
int temp=0;

i/=4;
while(temp<i)
{
p=p->next;
temp++;
}
if(!p) printf("Insert Failed");

LinkList s=(Lnode *)malloc(sizeof(Lnode));
s->data=(char *)malloc(sizeof(char)*5);
strcpy(s->data, ins);
s->next=p->next;
p->next=s;
}//在單鏈表L的第i個元素繼續插於入ins

void main()
{
fflush(stdin);
char buff[100],ins[4];
int m;
printf("Plz type in the string:");
gets(buff);
LinkList L;
CreateList(L, buff);

printf("The linklist L is : ");
TraverseList(L);
//printf("%d", flag);

printf("where to insert (m):");
scanf("%d", &m);
printf("what to insert:");
fflush(stdin);
scanf("%s", &ins);
//gets(ins);
InsertList(L, m, ins);
TraverseList(L);
}

⑸ 動態鏈表插入結點,c語言的!

在while語句運行後,有以下可能:
若head為0,則p2==0,p1==0
若head->next為0,則p2==0,p1==head
若head->num
next!=0
while語句的結果不夠明顯,導致if語句難以編成。
建議:先判斷head是否為空,之後再用while尋找插入點進行插入。
由於現在手頭沒有編程軟體,就沒法幫忙編了,請參考建議編吧。

⑹ C語言 單鏈表插入的代碼是

在給定的單鏈表的第i位上插入值為n的節點。
#include <stdio.h>
#include<malloc.h>
#define N 5
typedef int elemtype;
typedef struct node
{
elemtype data;
struct node *next;
}linklist;

linklist *Creatlist(linklist*L){
L=(linklist*)malloc(sizeof(linklist));
L->next=NULL;
return L;
}
int Judge(linklist *L){
if(L->next==NULL)
{
printf("建表成功...\n");
}
else
printf("建表失敗.\n");
return 0;
}
int Input(linklist *L,int x,linklist *r){
int i;
linklist *p;
p=(linklist*)malloc(sizeof(linklist));
p->data=x;
p->next=NULL;
r->next=p;
printf("%d ",p->data);
return 0;
}
int Insert1(linklist *L,int i){
linklist *p,*q,*r,*t;
int j=1,item;
p=L->next;
q=L;
r=L;
if(L->next==NULL)
{
printf("表空.\n");
return 0;
}
else
{
while(p!=NULL&&j<i)
{
q=p;
p=p->next;
j++;
}
if(p==NULL)
{
printf("%d不在表的范圍內.\n");
return 0;
}
else
{
t=(linklist*)malloc(sizeof(linklist));
t->next=NULL;
printf("請輸入item:");
scanf("%d",&item);
t->data=item;
t->next=p;
q->next=t;
}
printf("在第%d位上插入值為%d的節點後的所有數據為\n",i,item);
for(j=0;j<N+1;j++)
{
r=r->next;
printf("%d ",r->data);
}
printf("\n");
return 1;
}
}
int main()
{
int i,item,k;
linklist *L,*r;
printf("單向鏈表的創建(包括初始化)與輸出\n");
L=Creatlist(L);
Judge(L);
printf("表中的數據為:");
r=L;
Input(L,11,r);
r=r->next;
Input(L,32,r);
r=r->next;
Input(L,17,r);
r=r->next;
Input(L,46,r);
r=r->next;
Input(L,9,r);
r=r->next;
printf("\n");
printf("在給定的單鏈表的第i位上插入值為item的節點\n");
printf("請輸入i:");
scanf("%d",&i);
Insert1(L,i);
return 0;
}

在給定單鏈表的值為m的節點的前面插入一個值為n的節點
#include <stdio.h>
#include<malloc.h>
#define N 5
typedef int elemtype;
typedef struct node
{
elemtype data;
struct node *next;
}linklist;

linklist *Creatlist(linklist*L){
L=(linklist*)malloc(sizeof(linklist));
L->next=NULL;
return L;
}
int Judge(linklist *L){
if(L->next==NULL)
{
printf("建表成功...\n");
}
else
printf("建表失敗.\n");
return 0;
}
int Input(linklist *L,int x,linklist *r){
int i;
linklist *p;
p=(linklist*)malloc(sizeof(linklist));
p->data=x;
p->next=NULL;
r->next=p;
printf("%d ",p->data);
return 0;
}
int Insert2(linklist *L,int item){
linklist *p,*q,*r,*t;
int j=1,m;
p=L->next;
r=L;
q=L;
if(L->next==NULL)
{
printf("表空.\n");
return 0;
}
else
{
while(p!=NULL)
{
if(p->data!=item)
{
q=p;
p=p->next;
}
else
break;
}
if(p==NULL)
{
printf("%d不在表的范圍內.\n");
return 0;
}
else
{
t=(linklist *)malloc(sizeof(linklist));
t->next=NULL;
printf("請輸入m:");
scanf("%d",&m);
t->data=m;
q->next=t;
t->next=p;
}
}
printf("在值為%d的節點的前面插入一個值為%d的節點後的所有數據為\n",item,m);
for(j=0;j<N+1;j++)
{
r=r->next;
printf("%d ",r->data);
}
printf("\n");
return 1;
}
int main()
{
int i,item,k;
linklist *L,*r;
printf("單向鏈表的創建(包括初始化)與輸出\n");
L=Creatlist(L);
Judge(L);
printf("表中的數據為:");
r=L;
Input(L,11,r);
r=r->next;
Input(L,32,r);
r=r->next;
Input(L,17,r);
r=r->next;
Input(L,46,r);
r=r->next;
Input(L,9,r);
r=r->next;
printf("\n");
printf("在給定單鏈表的值為item的節點的前面插入一個值為m的節點\n");
printf("請輸入item:");
scanf("%d",&item);
Insert2(L,item);
return 0;
}

⑺ C語言代碼創建一個鏈表,並對鏈表進行插入元素,刪除元素和銷毀鏈表操作。並寫出測試用例。

已在VC6環境下調試通過.
#include<stdio.h>
#include<string.h>
#include<malloc.h>
struct student
{
long number;
char name[8];
float score;
struct student *next;
}*head=NULL;
int sum=0; //計數

void main()
{
void show();
void insert();
void del();
char command[6];
printf("***********************************************************\n");
printf(" 鏈表綜合練習程序\n\n");
printf("***********************************************************\n");
while(1)
{ printf("請輸入操作命令,(輸入help查看幫助):\n");
scanf("%s",command);
if(strcmp(command,"exit")==0) break; //輸入exit結束循環
else if(strcmp(command,"insert")==0) insert();
else if(strcmp(command,"show")==0) show();
else if(strcmp(command,"insert")==0) insert();
else if(strcmp(command,"del")==0) del();
else if(strcmp(command,"help")==0)
{ printf("命令名\t\t\t命令說明\n\n");
printf("help \t\t\t查看幫助\n");
printf("show \t\t\t顯示內容\n");
printf("insert\t\t\t插入內容\n");
printf("del \t\t\t刪除信息\n");
printf("exit \t\t\t退出程序\n\n");
}
else printf("命令無效,請重新輸入.(輸入help查看幫助)\n");
}
printf("Bye\n");
}
void show()
{
if(head==NULL)printf("沒有任何記錄\n\n");
else
{ struct student *i;
printf("學號\t姓名\t分數\n");
for(i=head;i!=NULL;i=i->next)
printf("%ld\t%s\t%.2f\n",i->number,i->name,i->score);
printf("共有%d條學生記錄\n\n",sum);
}
}
void insert()
{
printf("請按以下格式依次軟每項,各項用空格分開\n");
printf("學號\t姓名\t分數\n");
struct student *i,*p,*j;
p=(struct student *)malloc(sizeof(struct student));
scanf("%ld%s%f",&p->number,p->name,&p->score);
if(head==NULL){head=p;head->next=NULL;} //鏈表為空時
else
{ for(i=head;i->next!=NULL && p->number>i->number;j=i,i=i->next);
if(p->number<=i->number)
{ if(i==head)head=p; //插到最前面
else j->next=p; //插到中間
p->next=i;
}
else
{ p->next=NULL;
i->next=p;
}
}
sum++;
printf("操作成功\n\n");
}
void del()
{
if(head==NULL)printf("錯誤:尚未輸入任何記錄\n"); //鏈表為空時
else
{ long number;
struct student *i,*j;
printf("請輸入要刪除的學生的學號:");
scanf("%ld",&number);
for(i=head;i->next!=NULL && i->number!=number;j=i,i=i->next);
if(i->number==number)
{ if(i==head)head=i->next;
else j->next=i->next;
sum--;
free(i);
printf("刪除成功\n\n");
}
else printf("未找到輸入的學號,請核對後輸入\n");
}
}

⑻ 在鏈表中間刪除/插入一個元素 c語言編程//幫我完成

int insert(LinkList *p,DataType x)
{
LinkList *p1=p;
p=(LinkList *)malloc(sizeof(LinkList));
p->data=x;
p->next=p1;
}

int deletelist(LinkList *p)
{
LinkList *p1=p->next;
p->next=p1;
}

⑼ c語言鏈表插入的問題

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct
{
int num;
char name[10];
int grade;
}student;

typedef struct node
{
student date;
struct node *next;
}*link;

link head = NULL;
student creat(void)
{
student p;
printf("請輸入學生學號:");
setbuf(stdin,(char *)0);
scanf("%d",&p.num);
printf("請輸入學生名字:");
setbuf(stdin,(char *)0);
scanf("%s",p.name);
printf("請輸入學生成績:");
setbuf(stdin,(char *)0);
scanf("%d",&p.grade);
return p;
}

void insert(student date)
{
link p = (struct node*)malloc(sizeof *p);
p->date=date;
p->next=head;
head=p;
}

void output(void)
{
link p = head;
printf("|----學號-----姓名-------成績--|\n");
while(p!=NULL)
{
printf("| %-9d%-11s%-6d|\n",p->date.num,p->date.name,p->date.grade);
p=p->next;
}
printf("|------------------------------|\n");
}
void init(void)
{
puts("*************歡迎使用信息查詢系統*************");
puts("*\t+-------------------------+ *");
puts("*\t| 1.添加記錄 | *");
puts("*\t| 2.顯示所有記錄 | *");
puts("*\t| 0.保存退出 | *");
puts("*\t+-------------------------+ *");
puts("********************************************");
printf("\t請輸入您的選擇:");
}

int main()
{
int n;
char cmd[10];
do{
//system("cls");
men: init();
setbuf(stdin,(char *)0);//清空輸入流
scanf("%[^\n]",cmd);//接受除換行以外的所有字元存入cmd中,並加上'\0'標志
sscanf(cmd,"%d",&n);
/*處理當命令不符合條件的情況*/
if(strlen(cmd) != 1 || n < 0 || n >2 || !(*cmd >= '0' && *cmd <= '2'))
{
printf("\t輸入錯誤或沒有這個選項!");
getchar();
getchar();
goto men;
}
switch(n)
{
case 1:insert(creat());puts("\t添加成功!");break;
case 2:output();break;
case 0: return 0;
default :break;
}
printf("\tPress Enter To Continue!");
getchar();
getchar();
}while(n != 0);
return 0;
}

⑽ C語言插入鏈表

你這個也是對的。
其實這個時候p就是q->next。
你這樣做,不太好,因為要注意一個原則:
插入指針要先讓新指針的next指向插入點的next,再讓插入點的next指向新指針。
這樣保險。
你的方法在復雜情況下會容易出錯。