當前位置:首頁 » 編程語言 » c語言鏈表插入

c語言鏈表插入

發布時間: 2022-02-23 00:28:07

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語言單鏈表插入點(插入點插到最後)問題

找找 遍歷的函數,你這太羅嗦了,頭插尾刪,尾插頭刪,基本遍歷函數多檢查

㈢ C語言鏈表的建立與插入

//C語言鏈表的建立與插入

#include <stdio.h>
#include <stdlib.h>
//使用結構體構建鏈表
struct node{
int data;
struct node *next;
};
void main()
{
int n=1;int a;
struct node *p,*head,*tail,*t;
//申請動態空間
p=(struct node *)malloc(sizeof(struct node));
//head=(struct node *)malloc(sizeof(struct node));
t=(struct node *)malloc(sizeof(struct node));
head=tail=p; //////////////////////////////////////
for(;n<=5;n++) //輸入1,3,5,7,9 {
{
p->data=2*n-1;
p->next =NULL;
tail->next=p;
tail=p;
p=(struct node *)malloc(sizeof(struct node));
}
//輸出原始數據
printf("原始鏈表如下:\n"); //輸出原始鏈表
for(p=head;p!=NULL;p=p->next)
{
printf("%d ",p->data);
}
//插入數據
printf("\n請輸入需要插入的數據\n"); //輸入所要插入的新數據
scanf("%d",&a );
for(p=head;p!=NULL;) //按順序插入相應位置
{
if(p->data <a && (p->next)->data >a)
{
t->data =a;
t->next =p->next ;
p->next=t;
}
p=p->next ;
}
printf("插入新數據後的鏈表\n"); //輸出插入新數據的鏈表
for(p=head;p!=NULL;)
{
printf("%d ",p->data);
p=p->next;
}
free(p);
free(head);
free(t);
printf("\n");
}

㈣ c語言數據結構(鏈表的插入和刪除)

下面是我的源代碼,你看看,應該可以幫上你的 :-)

/*
* singleLinkedList.cc
*
* Created on: 2010-6-1
* Author: LiuFeng
*
*/

#include <iostream>
#include <cstdlib>
#include <cstdio>

typedef struct node {
int data;
node* next;
} node;

node*
search_node(node* head, int pos){
node*p=head->next;
if(pos<0){
printf("incorrect position to search node:\n");
return NULL;
}
if(pos==0){
return head;
}
if(p==NULL){
printf("List is empty\n");
return NULL;
}

while(--pos){
if((p=p->next)==NULL){
printf("incorrect postion to search node!\n");
break;
}
}
return p;
}

node*
insert_node(node* head, int pos, int data){
node* item=NULL;
node* p;

item = (node*)malloc(sizeof(node));
item->data=data;
if(pos==0){
head->next=item;
return head;
}
p=search_node(head,pos-1);
if(p!=NULL){
item->next=p->next;
p->next=item;
}
return head;
}

node*
delete_node(node*head, int pos){
node* item=NULL;
node* p = head->next;
if(p==NULL){
printf("link is empty\n");
return NULL;
}
p=search_node(head, pos);
if(p!=NULL&& p->next!=NULL){
item=p->next;
p->next=item->next;
delete item;
}
return head;
}

int length(node * head){
int len=0;
node *p;
p=head->next;
while(p!=NULL){
++len;
p=p->next;
}
return len;
}

㈤ c語言鏈表的添加的代碼

struct node
{
int count;
node* next;
};
...............
node* first = new node();//首節點
node* temp = new node();//每次給他賦值
first.next -> temp;//把temp的地址給 first.next
for(int i = 0; i < 99; ++i)
{
temp.next = new node();//創建新node並賦值給next
temp -> temp.next;//新創建的賦值給temp,以進行下一次創建
}
很久沒寫c了,寫法應該不太對,記得當時是用malloc,但是鏈表操作大致是這個思路

㈥ 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語言鏈表插入法求解下列問題

根據題意:

一、鏈表創建:根據輸入的數字,動態創建任意多個節點插入鏈表。(題目規定n<=40,如不想使用malloc動態申請內存,需直接定義最大上限40個節點)。

二、鏈表排序:交換節點內容(不是地址),保留鏈表指針的值(*next的值)。

三、列印鏈表:利用鏈表指針遍歷鏈表。

四、對動態申請的鏈表地址空間釋放(在本程序中創建後程序就結束了,即使不寫free釋放,結束後也會釋放。但在復雜程序中調用創建,後續還有代碼,需像我代碼中寫函數動釋放不用的內存,避免浪費)。

下面是代碼:

#include <stdio.h>

#include <malloc.h>

typedef struct numStruct

{

int num;

struct numStruct *next;

}NST;

NST *insert2List(int num);//根據數字創建節點(動態),插入鏈表(首次自動生成頭節點),成功返回頭節點,失敗返回NULL。

void showList(NST *nsthead);//列印鏈表

void px(NST *nsthead);//鏈表排序

void freeList(NST *nsthead);//釋放鏈表內存

int main()

{

NST *nsthead=NULL;

int i=0,n=50,*nums=NULL;

while(n>40)

scanf("%d",&n);

nums=(int *)malloc(sizeof(int)*n);

if(!nums) return 1;

while(i<n)

scanf("%d",&nums[i++]);

i=0;

while(i<n)

nsthead=insert2List(nums[i++]);

px(nsthead);

showList(nsthead);

freeList(nsthead);

return 0;

}

void freeList(NST *nsthead)

{

NST *temp=NULL,*nst=NULL;

if(nsthead)

{

nst=nsthead->next;

while(nst!=NULL)

{

temp=nst;

nst=nst->next;

free(temp);

}

}

free(nsthead);

}

void showList(NST *nsthead)

{

if(nsthead)

while(nsthead->next!=NULL)

{

printf("%d ",nsthead->next->num);

nsthead=nsthead->next;

}

printf(" ");

}

void px(NST *nsthead)

{

NST *nt1=NULL,*nt2=NULL,ntTemp,*nextSave=NULL;

if(nsthead)

{

nt1=nsthead->next;

while(nt1)

{

nt2=nt1->next;

while(nt2)

{

if(nt1->num>nt2->num)

{

ntTemp=*nt1;

nextSave=nt1->next;

*nt1=*nt2;

nt1->next=nextSave;

nextSave=nt2->next;

*nt2=ntTemp;

nt2->next=nextSave;

}

nt2=nt2->next;

}

nt1=nt1->next;

}

}

}

NST *insert2List(int num)

{

static NST *nsthead=NULL,*nstTail=NULL;

NST *nstNew=NULL;


nstNew=(NST *)malloc(sizeof(NST));

if(!nstNew) return NULL;

nstNew->next=NULL;

nstNew->num=num;

if(!nsthead)

{

nsthead=(NST *)malloc(sizeof(NST));

if(!nsthead) return NULL;

nsthead->next=nstNew;

}

else

nstTail->next=nstNew;

nstTail=nstNew;

return nsthead;

}

㈧ 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語言鏈表插入的問題

#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語言單鏈表鏈表如何插入多個節點

如果已知一個節點指針pre和一個節點指針cur,要把cur插入到pre節點之後,很顯然要保證鏈表不會斷開而丟失後面的節點,要先把後面的節點指針(指向lat的指針)保存下來,即有cur->next = pre->next,然後把cur連接的一串鏈表連接到pre後面,即pre->next = cur;

上面介紹了,在一個節點之後插入節點的情況。這是通常的情況。如果要向一個鏈表的頭部插入節點,就只需要將新節點的下一個指針指向鏈表的頭指針即可。

在這種情況下,有兩點要注意:

1,鏈表是否為空鏈表

2,要插入的節點是不是空指針。

代碼實現:

//向單鏈表中插入一個節點(插入在鏈開始處)
//輸入參數:單鏈表的頭指針和要插入的節點指針
//輸出參數:無
//返回值:指向單鏈表的頭指針
SingleList* Insert(SingleList *head,SingleList *node)
{
if(node == NULL)
{
return head;
}
else if(head == NULL)
{
return node;
}

node->next = head;
head = node;
return head;
}

熱點內容
紅點角標演算法 發布:2025-07-12 12:11:16 瀏覽:843
開心消消樂伺服器繁忙什麼情況 發布:2025-07-12 12:11:14 瀏覽:238
資料庫的封鎖協議 發布:2025-07-12 12:10:35 瀏覽:724
如何配置一台長久耐用的電腦 發布:2025-07-12 11:43:03 瀏覽:601
昆明桃源碼頭 發布:2025-07-12 11:38:45 瀏覽:568
大司馬腳本掛機 發布:2025-07-12 11:38:35 瀏覽:459
資料庫實時監控 發布:2025-07-12 11:31:33 瀏覽:743
vb6反編譯精靈 發布:2025-07-12 11:23:12 瀏覽:997
模擬存儲示波器 發布:2025-07-12 11:10:58 瀏覽:814
怎麼查看安卓真實運行內存 發布:2025-07-12 11:08:39 瀏覽:883