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

c語言鏈表的插入程序

發布時間: 2022-01-13 09:31:31

c語言鏈表插入程序(包括主函數)

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

typedef int elemType;
typedef struct linkedList
{
elemType data;
struct linkedList *next;
}Llist;
//插入節點函數
void Insert_Node(Llist *h,elemType d)
{
//頭插法
Llist *p,*n;
p=h->next;
n=(Llist *)malloc(sizeof(Llist));
n->data=d;
n->next=p;
h->next=n;
}
//輸出鏈表函數
void Display_Nodes(Llist *h)
{
Llist *p;
for (p=h;p->next!=NULL;p=p->next)
printf("%d\n",p->next->data);
}
//主函數
int main()
{
Llist *h;
int i;
//創建鏈表頭節點
h=(Llist *)malloc(sizeof(Llist));
h->data=0;
h->next=NULL;
//插入節點
printf("請輸入數字,輸入Ctrl+Z結束輸入:\n");
while (scanf("%d",&i)!=EOF)
Insert_Node(h,i);
//輸出鏈表
Display_Nodes(h);
getch();
}

② 你好,請問一下怎麼用C語言編寫單鏈表插入節點程序,萬分感謝!

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

#define FALSE 0
#define TRUE 1

// insertNode2:把newValue的值插入到遞增排序的鏈表中,正確返回TRUE,錯誤返回FALSE
// nextp是指向當前節點的指針,最初是頭指針
int insertNode2(Node **nextp, int newValue)
{
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)
return FALSE;
newNode->value = newValue;

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

return TRUE;
}

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

int insertNode(Node **rootp, int newValue);
int insertNode2(Node **nextp, int newValue);

int main()
{
srand(time(0));

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

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

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

getchar();
getchar();
return 0;
}

③ 用C語言編寫單鏈表的插入與刪除

放心吧,我已經運行過N次了,要知道我們也是上機調試好了的嘛,呵呵....

下面這個程序是我們上機試驗的,功能跟你想的基本上一樣了,呵呵!純屬巧合......
對了,這個程序中#define list_init_size 100是給改順序表初始化為100個元素空間,但是可以不輸滿,可以隨便改後面那個數,但是你最初輸入只能<=list_init_size個.
在後面插入時候要是滿了的話,順序表的長度可以每次加10個.你可以直接運行參考參考...
#include<stdio.h>
#include<stdlib.h>
#define list_init_size 100
#define listincrement 10
#define overflow -2
typedef int status;
typedef int elemtype;
typedef struct
{
elemtype *elem;
int length;
int listsize;
} sqlist;
status initlist_sq(sqlist &L)
{
L.elem=(elemtype *)malloc(list_init_size * sizeof(elemtype));
if(!L.elem) exit(overflow);
L.length=0;
L.listsize=list_init_size;
return 1;
}
status listinsert_sq(sqlist &L, int i ,elemtype e)
{
int * q , *p ,* newbase;
if(i<1 || i>L.length + 1) return 0;
if(L.length >= L.listsize)
{
newbase=(elemtype *)realloc(L.elem,(L.listsize+listincrement) * sizeof(elemtype));
if(!newbase) exit (overflow);
L.elem=newbase;
L.listsize+=listincrement;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]) ;p>=q ;--p)
*(p+1) = *p;
*q = e;
++L.length;
return 1;
}
status listdelete_sq(sqlist &L, int i, elemtype &e)
{
int *p ,*q ;
if((i<1) || (i>L.length)) return 0;
p=&(L.elem[i-1]);
e=* p;
q=L.elem + L.length - 1;
for(++p ;p<=q ;++p)
*(p-1)=*p;
--L.length;
return 1;
}
status locateelem_sq(sqlist &L, elemtype e)
{
for(int pass=1 ;pass<=L.length ;pass++)
{
if(L.elem[pass-1]==e)
return pass;
}
return 0;
}
int main()
{
int i, e, swich_sq, pa;
sqlist L;
initlist_sq(L);
printf("請設置字元的長度:\n");
scanf("%d",&L.length);
if(L.length<=0 || L.length>list_init_size)
{
printf("錯誤的輸入!\n");
return 0;
}
for( pa=1;pa<=L.length; pa++)
{
printf("請輸入數組第%d個數!\n",pa);
scanf("%d",&L.elem[pa-1]);
}
do{
printf("\n\n當前的數是: ");
for (pa=0;pa<L.length;pa++)
{
printf("%d ",L.elem[pa]);
}
printf("\n請輸入要執行的操作:\n 1,插入一個數\n 2,刪除一個數\n 3,查找一個數\n 0,退出程序\n");
scanf("%d",&swich_sq);
switch(swich_sq)
{
case 1:
printf("請輸入要插入數的位置:\n");
scanf("%d",&i);
if (i>=1 && i<=L.length)
{
printf("請輸入要插入的數的值:\n");
scanf("%d",&e);
listinsert_sq(L,i,e);
}
else printf("\n\n\n輸入不正確!\n");
break;
case 2:
printf("請輸入要刪除的數的序號:\n");
scanf("%d",&i);
if (i>=1 && i<=L.length)
{
listdelete_sq(L,i,e);
printf("你刪除的數是L.elem[%d] = %d",i-1,e);
}
else printf("\n\n\n你的輸入不正確!\n");
break;
case 3:
printf("請輸入要查找的數:\n");
scanf("%d",&e);
if (locateelem_sq(L,e)!=0)
{
printf("你查找的數在結構中,並且是第%d個數!\n",locateelem_sq(L,e));
}
else printf("\n\n\n你輸入的數不在結構中!\n");
break;
}
}
while (swich_sq != 0);
free (L.elem);

}

④ 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語言鏈表的添加的代碼

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
#include
#include

typedef struct Node
{
int date;
struct Node * pNext;
}NODE, *PNODE;

//函數聲明
PNODE creat_list();//創建鏈表
void traverse_list(PNODE pHead); //輸出鏈表
void Insert(PNODE pHead,int pos,int e); //pos 為插入位置 e為插入的數據
void Delete(PNODE pHead,int pos,int *e);
//pos 為刪除鏈表第pos個數 e存放被刪除的數據

int main()
{

int e;
PNODE pHead = NULL;
pHead = creat_list();
traverse_list(pHead);
Insert(pHead,2,99);
traverse_list(pHead);
Delete(pHead,3,&e);
printf("被刪除的數據為 %d\n",e);
traverse_list(pHead);

return 0;
}

PNODE creat_list()
{
int len;//用來存放有效結點的個數
int i;
int val;//用來臨時存放有效結點的數據

PNODE pHead = (PNODE)malloc(sizeof(NODE));
//創建一個不存放數據的結點,pHead 為頭指針

if (pHead == NULL)
{
printf("內存分配失敗,程序終止!\n");
exit(-1);
}

PNODE pTail = pHead;
pTail->pNext = NULL;

printf("請輸入你需要生成的鏈表的個數:len = ");
scanf("%d",&len);

for (i = 0; i < len; i++)
{
printf("請輸入第%d個節點的值:",i+1);
scanf("%d",&val);

PNODE pNew = (PNODE)malloc(sizeof(NODE));
//當i = 0 時,創建的第一個結點是首結 每創建一個新結點
if (pNew == NULL)
{
printf("內存分配失敗,程序終止!\n");
exit(-1);
}
pNew->date = val;
pTail->pNext = pNew;
pNew->pNext = NULL;
pTail = pNew;
}
return pHead;
}

void traverse_list(PNODE pHead)
{
PNODE p = pHead->pNext;
while(NULL != p)
{
printf("%d ",p->date);
p = p->pNext;
}
printf("\n");
return;
}

void Insert(PNODE pHead,int pos,int e)
{
int j = 0;
PNODE p, s;
p = pHead;
while(p && j < pos - 1)
{
p = p->pNext;
j++;
}
if(!p || j > pos - 1)
exit(-1);
s = (PNODE)malloc(sizeof(NODE));
s->date = e;
s->pNext = p->pNext;
p->pNext = s;
}

void Delete(PNODE pHead,int pos,int *e)
{
int j = 0;
PNODE p, r;
p = pHead;
while(p && j < pos - 1)
{
p = p->pNext;
j++;
}
if(!p->pNext || j > pos - 1)
exit(-1);
r = p->pNext;
p->pNext = r->pNext;
*e = r->date;
free(r);
}

⑦ c語言單鏈表插入源代碼

lz看看這個代碼吧:lz的代碼有太多的錯誤,需要慢慢的練練自己的編程的能力,
http://..com/team/view/我love編程
我的團隊,可以交流一下:
代碼:
#include<stdio.h>
#include<iostream.h>
#include<malloc.h>
#define N 10
struct list
{ int max;
int n;
int *a;

};
typedef struct list pseqlist;
pseqlist *creatmulllist_seq()//創建空表
{ struct list *palist;
palist=(pseqlist *)malloc(sizeof(pseqlist ));
if(palist!=NULL)
{ palist->a=(int *)malloc(sizeof(int)*N);
if(palist->a)
{
palist->n=0;
return palist;
}
}

}
pseqlist *insert_sep(pseqlist *p,int m)//輸入數組
{ int i,k;
p->n=m;
printf("輸入數組:\n");
for(i=0;i<m;i++)
{ scanf("%d",&k);
p->a[i]=k;

}
return p;

}
int insertPost_sep(pseqlist *q,int p,int x)//插入x
{ int i;
if(p<0||p>q->n-1)return 0;
else { q->n++;
for(i=q->n-1;i>p;i--)
q->a[i]=q->a[i-1];
q->a[p]=x;
return 1;
}
}

void main ()
{ int m,p,x,i,k;

printf("輸入數組的長度:\n");
scanf("%d",&m);

pseqlist *palist;
palist=creatmulllist_seq();//創建空表
insert_sep(palist,m);//輸入數組
printf("輸入要插入的位置:\n");
scanf("%d",&p);
printf("輸入要插入的數:\n");
scanf("%d",&x);
k=insertPost_sep(palist,p,x);//插入
if(k)for(i=0;i<palist->n;i++)
printf("%3d",palist->a[i]);
else printf("沒有插入成功");
printf("\n");

}
測試:

輸入數組的長度:
10
輸入數組:
1 2 3 4 5 6 7 8 9 10
輸入要插入的位置:
3
輸入要插入的數:
20
1 2 3 20 4 5 6 7 8 9 10
Press any key to continue

⑧ 編寫一個簡單的C語言鏈表插入程序!

我們可以用實際的值來看看程序到底是怎麼執行的。
假設你創建的鏈表裡已經有學號為1,2,4,5的四個學生的信息。
現在我們要插入學號為3的學生的信息。

首先:
struct student * insert(struct student *head,struct student *stu)
實參被傳遞過來,*head是已經創建的學生信息,*stu是要插入的學生信息。

struct student *p0,*p1,*p2;

p1 = head; //讓p1指向頭節點
p0 = stu; //p0指向要插入的節點

if(head == NULL) //這個是檢查鏈表是不是空的
{
head = p0; //如果是空的就把要插入的結點,設置為頭指針。
p0->next = NULL;
}
else
{

while((p0->num > p1->num) && (p1->next != NULL))
{ 在這個循環里,第一次進循環的時候p0->num等於3,p1->num等於1,即3>1,而且p1後面(即p1->next)還有結點。滿足條件進入第二次循環。

在進第二次循環之前需要把p1指向第2個結點,即指向學號為2的學生信息。
p2 = p1; //p2指向p1的結點 ,即頭指針。
p1 = p2->next; //p1移到下一個結點,現在指向的是第2個結點。

第二次進循環的時候p0->num等於3,p1->num等於2,即3>2,而且p1後面還有結點。滿足條件進入第三次循環

再次把p1移到下一個結點。
p2 = p1; 這時p2指向的是第2個結點。
p1 = p2->next; p1指向的是第3個結點。

第三次進循環的時候p0->num等於3,p1->num等於4,即3>4。不能滿足條件。退出循環。
}
現在p2指向的是第2個結點,即學號為2的學生。
p1指向的是第3個結點,即學號為4的學生。
p0始終是要插入的學生。

if(p0->num <= p1->num)
{ 這里p0->num等於3,p1->num等於4,即3<4滿足條件。
if(head == p1)
{ 這里p1指向的是第三個結點,所以不能滿足條件。程序到下面else語句。
head = p0;
}
else
{ p2->next = p0;
到了這里p2的指針域指的是要插入的結點,即把學號為3的學生放到學號為2的學生後面。
}

p0->next = p1;
//然後這里把p1(學號為4)放到p0(學號為3)的後面。這樣就連在一起了。
}
else
{
p1->next = p0;
p0->next = NULL; //把要插入的結點,設置為尾結點。
}
}
n=n+1;
return (head);
}

⑨ C語言實現的雙向鏈表插入程序

//雙向鏈表
#include<stdio.h>
#include <malloc.h>
typedef struct node
{
int i;
struct node *next,*prior;
}node;

node* create_list()
{
int a[]={1,2,3,8,9};
int j;
node *head,*p1,*p2;
p2=head=(node *)malloc(sizeof(node));
head->i=a[0];
head->next=head->prior=NULL;
for(j=1;j<5;j++)
{
p1=(node *)malloc(sizeof(node));
p1->i=a[j];
p1->prior=p2;
p1->next=NULL;

p2->next=p1;
p2=p1;
}
return head;
}

node * insert_list(node *head,int i,int num)
{
node *p,*q;
int j;

for(j=0,p=head;j<i&&p->next;j++)
{
p=p->next;
}

q=(node *)malloc(sizeof(node));
q->i=num;

q->prior=p->prior;
q->next=p;
if(i==0)//插入結點作為表頭
{
head=q;
}
else
{
p->prior->next=q;
}
p->prior=q;

return head;
}

void printf_list(node *head)
{
node *p;
for(p=head;p;p=p->next)
{
printf("%d\t",p->i);
}
printf("\n");
}

void main()
{
struct node *head;

int i,num;
head=create_list();
printf_list(head);

scanf("%d",&i);
scanf("%d",&num);
head=insert_list(head,i,num);
printf_list(head);
}

⑩ 「單鏈表的插入與刪除」的c語言程序是什麼啊

用C語言編寫如下程序,並上機驗證通過:
設自線性表LA=(3,5,8,11)和LB=(2,6,9,11,15,20)
1.若LA和LB分別是兩個集合A和B,求新集合A=AUB(\'並\'操作,相同元素不得留)預測輸出LA=(3,5,8,11,2,6,8,9,11,15,20)
2.將LA和LB表歸並,要求仍有序(相同元素要保留)預測輸出LC=(2,3,5,6,8,8,9,11,11,15,20)
分別用數組和指針實現,其4個子程序.