c语言链表插入
㈠ 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&¤t->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;
}