Ⅰ c语言数据结构单链表初始化
你说的那个是逆序建立n个节点的链表,建完头结点后由于链表中没有其他节点就把头结点的next置为空,这一点如果不明白的话去看链表那一章,有介绍!!!
之后从后往前建立链表!!刚开始是L->next=NULL
要注意p是每次新建的节点,这样从后往前以此建立链表,只要明白p是每次新建的节点,和从后往前建立,就会明白的!!给你花了张图!!
Ⅱ c语言单链表初始化问题
这里q是什么,q是形参,你无论怎么变q,出了函数传进来的值还是不变。假如Node是一个int,你传入一个int* q,你要改变q是不是用*q=0;
一样的,在这个例子里,我会传入一个Node** q ,然后所有q改成*q 来实现。
表达能力不行,反正就那意思吧。应该没错,嗯。
Ⅲ c语言数据结构单链表的初始化 插入 销毁 元素的取出 删除 操作 求详细C源代码 谢谢。。。。
单链表功能大全,嘿嘿
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int nDate;
struct node *pstnext;
}Node;
//链表输出
void output(Node *head)
{
Node *p = head->pstnext;
while(NULL != p)
{
printf("%d ", p->nDate);
p = p->pstnext;
}
printf("\r\n");
}
//链表建立
Node* creat()
{
Node *head = NULL, *p = NULL, *s = NULL;
int Date = 0, cycle = 1;
head = (Node*)malloc(sizeof(Node));
if(NULL == head)
{
printf("分配内存失败\r\n");
return NULL;
}
head->pstnext = NULL;
p = head;
while(cycle)
{
printf("请输入数据且当输入数据为0时结束输入\r\n");
scanf("%d", &Date);
if(0 != Date)
{
s = (Node*)malloc(sizeof(Node));
if(NULL == s)
{
printf("分配内存失败\r\n");
return NULL;
}
s->nDate = Date;
p->pstnext = s;
p = s;
}
else
{
cycle = 0;
}
}
p->pstnext = NULL;
return(head);
}
//单链表测长
void length(Node *head)
{
Node *p = head->pstnext;
int j=0;
while(NULL != p)
{
p = p->pstnext;
j++;
}
printf("%d\r\n", j);
}
//链表按值查找
void research_Date(Node *head, int date)
{
Node *p;
int n=1;
p = head->pstnext;
while(NULL != p && date != p->nDate)
{
p = p->pstnext;
++n;
}
if(NULL == p)
{
printf("链表中没有找到该值");
}else if(date == p->nDate)
{
printf("要查找的值%d在链表中第%d个位置\r\n", date, n);
}
return;
}
//按序号查找
void research_Number(Node *head, int Num)
{
Node *p=head;
int i = 0;
while(NULL != p && i < Num)
{
p = p->pstnext;
i++;
}
if(p == NULL)
{
printf("查找位置不合法\r\n");
}else if(i == 0)
{
printf("查找位置为头结点\r\n");
}else if(i == Num)
{
printf("第%d个位置数据为%d\r\n", i, p->nDate);
}
}
//在指定元素之前插入新结点
void insert_1(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre && j < i-1)
{
pre = pre->pstnext;
j++;
}
if(NULL == pre || j > i-1)
{
printf("插入位置不存在\r\n");
}else
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
printf("分配内存失败\r\n");
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}
}
//在指定元素之后插入新结点
void insert_2(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre->pstnext && j < i)
{
pre = pre->pstnext;
j++;
}
if(j == i)
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
printf("分配内存失败\r\n");
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}else
{
printf("插入位置不存在\r\n");
}
}
//删除指定结点
void Delete_1(Node *head, int i3)
{
Node *p = head, *pre = NULL;
int j = 0;
while(NULL != p && j < i3)
{
pre = p;
p = p->pstnext;
j++;
}
if(NULL == p)
{
printf("删除位置不存在\r\n");
}else
{
pre->pstnext = p->pstnext;
free(p);
}
}
//指定删除单链表中某个数据,并统计删除此数据的个数
int Delete_2(Node *head, int Delete_date)
{
int count = 0;
Node *p = head, *q;
while(NULL != p->pstnext)
{
q = p->pstnext;
if(q->nDate == Delete_date)
{
p->pstnext = q->pstnext;
free(q);
++count;
}
else
{
p = q;
}
}
return count;
}
//链表逆置
void Reverse_list(Node *head)
{
Node *q, *s;
if(NULL == head->pstnext || NULL == head->pstnext->pstnext)
{
return;
}
q = head->pstnext->pstnext;
head->pstnext->pstnext = NULL;
while(NULL != q)
{
s = q->pstnext;
q->pstnext = head->pstnext;
head->pstnext = q;
q = s;
}
}
//单链表的连接
void connect_list(Node *head, Node *head_New)
{
Node *p = head;
while(NULL != p->pstnext)
{
p = p->pstnext;
}
p->pstnext = head_New->pstnext;
}
//单链表销毁
void destroy_list(Node* head)
{
while (NULL != head)
{
Node* temp = head;
head = head->pstnext;
free(temp);
}
}
void main()
{
int date, num; //待查找数据
int i3; //指定删除元素的位置
int i1, i2, Newdate_1, Newdate_2; //待插入的新数据
int Delete_date, k; //待删除的数据与其个数
Node *Head = NULL; //定义头结点
Node *Head_New = NULL;
//链表建立
Head = creat();
printf("输出建立的单链表\r\n");
output(Head);
//单链表测长
printf("单链表长度为\r\n");
length(Head);
//链表按值查找
printf("请输入待查找的数据\r\n");
scanf("%d", &date);
research_Date(Head, date);
//链表按序号查找
printf("请输入待查找序号\r\n");
scanf("%d", &num);
research_Number(Head, num);
//在指定第i1个元素之前插入新元素Newdate
printf("在指定第i个元素之前插入新元素Newdate");
printf("请输入i与元素且以逗号间隔\r\n");
scanf("%d,%d", &i1, &Newdate_1);
insert_1(Head, i1, Newdate_1);
printf("插入后新链表\r\n");
output(Head);
//在指定第i2个元素之后插入新元素Newdate
printf("在指定第i个元素之后插入新元素Newdate");
printf("请输入i与元素且以逗号间隔\r\n");
scanf("%d,%d", &i2, &Newdate_2);
insert_2(Head, i2, Newdate_2);
printf("插入后新链表\r\n");
output(Head);
//指定删除i3元素
printf("删除元素的位置\r\n");
scanf("%d", &i3);
Delete_1(Head, i3);
printf("删除后新链表\r\n");
output(Head);
//指定删除单链表中某个数据,并统计删除此数据的个数
printf("请输入待删除的元素\r\n");
scanf("%d", &Delete_date);
k = Delete_2(Head, Delete_date);
printf("删除后新链表\r\n");
output(Head);
printf("删除指定元素在链表中的个数为:");
printf("%d\r\n", k);
//单链表逆置
Reverse_list(Head);
printf("逆置后输出\r\n");
output(Head);
//单链表的连接
printf("建立一个新链表\r\n");
Head_New = creat();
printf("输出新链表");
output(Head);
printf("将新链表连接到原来链表的尾部并输出\r\n");
connect_list(Head, Head_New);
output(Head);
destroy_list(Head);
return;
}
Ⅳ 大侠们怎么用C语言实现初始化一个链表啊~~~~~~~~~~
单链表:
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode
{
char data;
struct LNode *next;
}LNode,*LinkList;
void InitList(LinkList *L) //初始化
{
*L = (LinkList)malloc(sizeof(LNode));
(*L)->next = NULL;
}
char GetElem(LinkList L,int i) //返回i 位置的元素
{
//L为带头结点的单链表的头指针。
LinkList p;
int j;
p = L->next; j = 1; //初始化,p指向第一个结点,j为计数器
while(p && j<i)
{
p = p->next;
++j;
}
if(!p || j>i) exit(1); //i位置元素不存在
return(p->data);
}
void ListInsert(LinkList *L,int i,char e)//插入
{
//在带头结点的单链线性表L中第i个位置前插入元素e
LinkList p,s;
int j;
p = *L; j = 0;
while(p && j<i-1)//寻找第i-1个结点
{
p = p->next;
++j;
}
if(!p || j>i-1) exit(1); //i位置不正确
s = (LinkList)malloc(sizeof(LNode)); //新结点
s->data = e;
s->next = p->next;
p->next = s;
}
void ListDelete(LinkList *L,int i)//删除i位置元素
{
LinkList p,q;
int j;
p = *L; j = 0;
while(p->next && j<i-1)//寻找第i个结点
{//这以p->next开始是为了避开头结点,头结点不可删除
//而且这样是必须的,这样是为了有被删除点前面结点的地址,这样才能有前继结点,连接删除点的后继结点.
p = p->next;
++j;
}
if(!(p->next) || j>i-1) exit(1); //删除位置不合理
q = p->next; //删除该点
p->next = q->next;
free(q);
}
void CreateList(LinkList *L,int n)
{
//逆位序输入n个元素的值,建立带表头结点的单链线性表L
int i;
LinkList p;
(*L) = (LinkList)malloc(sizeof(LNode));
(*L)->next = NULL; //先建立带头结点的单链表
for(i=n;i>0;--i)
{
p = (LinkList)malloc(sizeof(LNode)); //生成新结点
scanf("%c",&p->data);
fflush(stdin);
p->next = (*L)->next;
(*L)->next = p;
}
//*****************另一插入法**************************
q = *L = (LinkList)malloc(sizeof(LNode));
(*L)->next = NULL;
for(i=n;i>0;--i)
{
p = (LinkList)malloc(sizeof(LNode));
scanf("%c",&p->data);
fflush(stdin);
p->next = NULL;
q->next = p;
q = p;
//q->next = p;
}
//***********************************************************
}
void MergeList(LinkList *La,LinkList *Lb,LinkList *Lc)
{
//链表La,Lb按递增排列
LinkList pa,pb,pc;
pa = (*La)->next;
pb = (*Lb)->next;
(*Lc) = pc = (*La);
while(pa && pb)
{
if(pa->data <= pb->data)
{
pc->next = pa;
pc = pa;
pa = pa->next;
}
else
{
pc->next = pb;
pc = pb;
pb = pb->next;
}
}
pc->next = pa?pa:pb; //插入剩余段
//free(Lb); //释放Lb头结点
}
void display(LinkList *L)
{
LinkList di;
di = (*L)->next;
while(di!=NULL)
{
printf("%c\t",di->data);
di = di->next;
}
printf("\n");
}
void main()
{
LinkList link,link2,link3;
InitList(&link);
InitList(&link2);
CreateList(&link,5);
CreateList(&link2,5);
display(&link);
display(&link2);
MergeList(&link,&link2,&link3);
display(&link3);
}
Ⅳ c语言链表初始化中
p = head->next;??这个怎么理解
本来head->next = NULL;
现在 p = NULL?何意?
16行报错 8成是你的类型定义的有问题
typedef struct _node
{
struct _node *pPri;
struct _node *pNext;
int a;
}sNode,*psNode;
Ⅵ 怎么用C语言初始化一个链表啊~
所谓初始化链表就是把链表的数据置空,也就是所有的数据,指针都为null
举个单链表的例子
该算法的结果将单链表head置为空表,只需要将头节点的指针置为null即可。算法实现如下
void setnull(struct Lnode *head)
{
head->next=NULL;
}
双链表和循环链表依次类推·
Ⅶ C语言初始化单链表!
在单链表A中删除所有和单链表B中元素相同的结点
#include
"stdafx.h"
#include
<stdio.h>
#include
<malloc.h>
#define
SIZE
sizeof(struct
node)
struct
node{
int
data;
struct
node
*next;
};
void
init(struct
node
*LC){
int
n;
struct
node
*Str,*p;
p=(struct
node
*)malloc(SIZE);
Str=LC;
printf("请输入链表A,以小于零的数结束输入:\n");
scanf("%d",&n);
while(n>=0){
p->data=n;
Str->next=p;
Str=p;
p=(struct
node
*)malloc(SIZE);
scanf("%d",&n);
Str->next=NULL;
}
printf("您输入的序列的是:\n");
for
(Str=LC->next;Str!=NULL;){
printf("%d
",Str->data);
Str=Str->next;
}
printf("\n");
}
void
delet_LA(struct
node
*LA,struct
node
*pa){
struct
node
*p;
struct
node
*q;
p=LA;
q=p;
for
(p;p!=pa;){
q=p;
p=p->next;
}
q->next=pa->next;
}
void
delete_same(struct
node
*LA,struct
node
*LB){
struct
node
*pa,*pb;
pa=LA->next;
pb=LB->next;
for
(pb;pb!=NULL;){
for
(pa;pa!=NULL;){
if
(pb->data==pa->data){
delet_LA(LA,pa);
pa=LA->next;
}
else{
pa=pa->next;
}
}
pb=pb->next;
pa=LA->next;
}
printf("处理后的单链表A为:\n");
for
(pa=LA->next;pa!=NULL;){
printf("%d
",pa->data);
pa=pa->next;
}
}
void
main(){
struct
node
*LA;
struct
node
*LB;
LA=(struct
node
*)malloc(SIZE);
if(LA!=NULL)
LA->data=-1;
LB=(struct
node
*)malloc(SIZE);
if(LB!=NULL)
LB->data=-1;
init(LA);
init(LB);
delete_same(LA,LB);
}
Ⅷ c语言,单链表,这个p不是初始化了吗怎么改
#include<stdio.h>
#include<stdlib.h>
typedefintElemType;
typedefstructnode{
ElemTypedata;
structnode*next;
}*LinkList,*pNode,NODE;
LinkListCreatList(){
pNodep,head;
ElemTypee;
head=p=(pNode)malloc(sizeof(NODE));
while(scanf("%d",&e)==1&&e){
p->next=(pNode)malloc(sizeof(NODE));
p->next->data=e;
p=p->next;
}
p->next=NULL;
returnhead;
}
voidShowList(LinkListhead){
pNodep=head->next;
while(p){
printf("%d",p->data);
p=p->next;
}
printf("
");
}
intmain(){
LinkListhead=CreatList();
if(head)ShowList(head);
return0;
}
Ⅸ 求用c语言初始化一个单链表,然后插入删除的源代码
# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
typedef struct Node
{
int date; //数据域
struct Node * pNext; //指针域
}NODE,*PNODE;
PNODE create_list(void); //创建链表
void traverse_list(PNODE); //遍历链表
bool is_empty(PNODE); //链表是否为空
int list_len(PNODE); //求链表长度
bool insert_list(PNODE,int , int ); //插入链表结点
bool del_list(PNODE, int, int *); //删除链表结点
void sort_list(PNODE); //排序
int main(void)
{
int len;
int dval = 0;
PNODE pHead = NULL;
pHead = create_list();
// insert_list(pHead, 3, 26);
traverse_list(pHead);
del_list(pHead,2,&dval);
printf("删除的值是:%d\n",dval);
// sort_list(pHead);
traverse_list(pHead);
if ( is_empty(pHead) )
{
printf("链表是空的!\n");
}
len = list_len(pHead);
printf("链表的长度是:%d\n",len);
return 0;
}
PNODE create_list(void)
{
int i;
int len;
int val;
PNODE pHead = (PNODE)malloc(sizeof(NODE)); //创建头结点
if (NULL == pHead)
{
printf("链表创建失败!\n");
exit(-1);
}
PNODE pTail = pHead; //创建尾结点
pTail->pNext = NULL;
printf("请输入要创建的链表: ");
scanf("%d",&len);
for (i=0; i<len; i++)
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if (NULL == pNew)
{
printf("链表创建失败,可能原因:内存不足!\n");
exit(-1);
}
printf("请输入第%d个的值: ",i+1);
scanf("%d",&val);
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 ;
}
bool is_empty(PNODE pHead)
{
if ( NULL == pHead->pNext )
{
return true;
}else
{
return false;
}
}
int list_len(PNODE pHead)
{
int len = 0;
PNODE p = pHead->pNext;
while ( NULL != p )
{
len++;
p = p->pNext;
}
return len;
}
bool insert_list(PNODE pHead, int place, int val) //我自己的:
{ /*
int len = list_len(pHead);
int i;
if ( place > len)
{
printf("Error!\n");
return false;
// exit(-1);
}
PNODE p = (PNODE)malloc(sizeof(NODE));
PNODE prev;
if (NULL == p)
{
printf("内存分配失败!\n");
return false;
// exit(-1);
}
p->date = val;
PNODE temp = pHead;
for (i=0; i<place; i++)
{
prev = temp;
temp = temp->pNext;
}
p->pNext = temp;
prev->pNext = p;
*/
// 传说中,一个牛人的经典算法:
int i = 0;
PNODE p = pHead;
while (NULL != p && i < place-1)
{
p = p->pNext;
i++;
}
if (i > place-1 || NULL == p)
return false;
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if (NULL == pNew)
{
printf("分配失败了!\n");
return false;
}
pNew->date = val;
PNODE q = p->pNext;
p->pNext = pNew;
pNew->pNext = q;
return true;
}
bool del_list(PNODE pHead, int place, int * pval)
{ /* 我自己的算法:
int len = list_len(pHead);
int i;
if (place > len)
{
printf("Error! 下标越界!\n");
return -1;
}
PNODE pTem;
PNODE temp = pHead;
PNODE prev;
for (i=0; i<place; i++)
{
prev = temp;
temp = temp->pNext;
}
prev->pNext = temp->pNext;
pTem = temp;
* pval = temp->date;
free(pTem);
*/
// 牛人优化的代码
int i = 0;
PNODE p = pHead;
while ( NULL != p->pNext && i < place - 1 )
{
p = p->pNext;
i++;
}
if ( NULL == p->pNext || i >= place - 1)
{
printf("ERROR! 删除错误!!\n");
return false;
}
PNODE pLast = (PNODE)malloc(sizeof(NODE));
pLast = p->pNext;
*pval = pLast->date;
p->pNext = pLast->pNext;
free(pLast);
return true;
}
void sort_list(PNODE pHead)
{
int i,j=0,t;
int len = list_len(pHead);
PNODE a; //指向首节点
PNODE b ; //指向首节点后的位置
// PNODE t = (PNODE)malloc(sizeof(NODE));
for (i=0,a = pHead->pNext; i<len-1; i++, a = a->pNext)
{
for (j=0, b = a->pNext; j<len-i-1; j++,b = b->pNext)
{
if ( a->date < b->date )
{
t = a->date;
a->date = b->date;
b->date = t;
}
}
}
return ;
}
Ⅹ C语言初始化单链表!
定义单链表就是可以不用头指针,你的想法是对的!