School of Information and Electrical Engineering
Linked Lists
School of Information and Electrical Engineering
p
bat
cat
typedef struct Node
{
ElemType data;
struct Node * next;
}Node, *LinkList;
Node* p;
is similar to
LinkList p;
eat
mat
∧
School of Information and Electrical Engineering
p
Node* p;
p=malloc(sizeof(Node));
p->data=4;
p->next=null;
p
p
4
∧
School of Information and Electrical Engineering
p
p
p
p
p
p
School of Information and Electrical Engineering
main()
{
int a=1, b=3;
exchange1(&a, &b);
printf("a=%d,b=%d\n", a, b);
exchange2(&a, &b);
printf("a=%d,b=%d\n", a, b);
}
School of Information and Electrical Engineering
void exchange1(int *p1, int *p2)
{
int temp;
temp=*p1, *p1=*p2, *p2=temp;
}
void exchange2(int *p1, int *p2)
{
int* p_temp;
p_temp=p1, p1=p2, p2=p_temp;
}
School of Information and Electrical Engineering
main()
{
int a=1, b=3;
a
b
1
3
exchange1(&a, &b);
void exchange1(int *p1, int *p2)
p1
a
1
p2
b
3
School of Information and Electrical Engineering
a
p1
1
void exchange1(int *p1, int *p2)
{
temp
int temp;
temp=*p1;
temp
1
*p1=*p2;
p1
*p2=temp;
p2
a
3
b
1
}
//a and b are exchanged
p2
b
3
School of Information and Electrical Engineering
printf("a=%d,b=%d\n", a, b);
exchange2(&a, &b);
void exchange2(int *p1, int *p2)
a
p1
{
p2
3
int* p_temp;
p_temp=p1;
1
p_temp
p1
a
3
p_temp
b
School of Information and Electrical Engineering
p1=p2;
b
p2
1
p1
p2=p_temp;
p_temp
3
p2
}
a
//a and b are not exchanged
}
printf("a=%d,b=%d\n", a, b);
School of Information and Electrical Engineering
Linklist CreateFromHead()
{
L
LinkList L;
s
Node *s;
flag
int flag=1;
1
L=(Linklist)malloc(sizeof(Node));
L->next=NULL;
L
∧
L
School of Information and Electrical Engineering
while(flag) //flag=1
{
char c=getchar();
//If the user input ”ab$”
if(c!=’$’) //‘a’<>‘$’
{
s=(Node*)malloc(sizeof(Node));
s
s->data=c;
s
‘a’
c
‘a’
School of Information and Electrical Engineering
s->next=L->next;
L->next=s;
s
‘a’
∧
L
‘a’
}
while(flag)
{
char c=getchar();
//read ‘b’
if(c!=’$’)
{ //‘b’<>‘$’
c
‘b’
∧
School of Information and Electrical Engineering
s=(Node*)malloc(sizeof(Node));
s->data=c;
s
s
s->next=L->next;
‘b’
L
∧
‘a’
s
‘b’
L->next=s;
}
L
‘a’
s
‘b’
∧
School of Information and Electrical Engineering
while(flag)
c
{
‘$’
char c=getchar();
//read‘$’
if(c!=’$’)
else
flag
flag=0;
0
}
while(flag)
//flag=0
return L;//the result is :
L
‘a’
s
‘b’
∧
School of Information and Electrical Engineering
Linklist CreateFromTail() 的末尾*/
{
LinkList L;
Node *r, *s;
L=(Node *)malloc(sizeof(Node));
L->next=NULL;
r=L;
c=getchar();
while(c!=’$’)
{
School of Information and Electrical Engineering
s=(Node*)malloc(sizeof(Node));
s->data=c;
r->next=s;
r=s;
c=getchar();
}
r->next=NULL;
}
School of Information and Electrical Engineering
Node * Get(LinkList L, int i)
{
//Assume the initial situation is:
L
Node *p;
‘a’
‘b’
p
p=L;
L
‘a’
p
j=0;
j
0
‘b’
∧
∧
i
3
School of Information and Electrical Engineering
while((p->next!=NULL)&&(j<i))
L
‘a’
j
i
0
3
‘b’
∧
p
{
p=p->next;
j++;
L
‘a’
p
j
1
2.10 链表的运算——查找结点
‘b’
∧
School of Information and Electrical Engineering
while((p->next!=NULL)&&(j<i))
L
‘a’
j
i
1
3
‘b’
∧
p
{
p=p->next;
j++;
j
2
2.10 链表的运算——查找结点
L
‘a’
‘b’
p
∧
School of Information and Electrical Engineering
while((p->next!=NULL)&&(j<i))
L
‘a’
2
i
3
Else
return NULL; / * failure* /
}
i
2
3
‘b’
p
// p->next =NULL
if(i= =j)
j
j
∧
School of Information and Electrical Engineering
Node *Locate(LinkList L,ElemType key)
{
Node *p;
p=L->next;
while(p!=NULL)
{
if(p->data!=key)
p=p->next;
else
break;
}
return p;
}
School of Information and Electrical Engineering
void DelList(LinkList L,int i,ElemType *e)
{
//the initial situation is:
L
‘a’
i
‘b’
‘c’
∧
‘b’
‘c’
∧
e
2
p
r
Node *p,*r;
p=L;
L
‘a’
p
School of Information and Electrical Engineering
k
L
‘a’
‘b’
int k =0;
0
while(p->next!=NULL&&k<i-1)
{
p=p->next;
‘c’
∧
p
L
‘a’
k
p
k=k+1; 1
while(p->next!=NULL&&k<i-1)
//k<i-1 is false
if(k!=i-1) //false
2.11 链表的运算——插入或删除结点
i
2
‘b’
‘c’
∧
School of Information and Electrical Engineering
r=p->next;
L
‘a’
‘b’
p
‘c’
∧
r
p->next=p->next->next
L
*e=r->data;
L
free(r);
‘a’
p
‘c’
∧
r
‘a’
p
}
‘b’
‘b’
r
‘c’
∧
School of Information and Electrical Engineering
void InsList(LinkList L,int i,ElemType e)
{
Node *pre,*s;
pre=L;
int k=0;
while(pre!=NULL&&k<i-1)
{
pre=pre->next;
k=k+1;
}
School of Information and Electrical Engineering
}
if(k!=i-1)
{
printf(“wrong place!”);
return;
}
s=(Node*)malloc(sizeof(Node));
s->data=e;
s->next=pre->next;
pre->next=s;
School of Information and Electrical Engineering
Int ListLength(LinkList L)
{
L
Node *p;
p=L->next;
j=0;
while(p!=NULL)
{
p=p->next;
j++;
}
return j;
}
2
2
3
∧
School of Information and Electrical Engineering
LinkList MergeLinkList(LinkList LA, LinkList LB)
{ LA
LB
2
2
3
1
3
3
Node *pa,*pb;
LinkList LC;
pa
pb
LC
∧
4
∧
School of Information and Electrical Engineering
pa=LA->next;
pb=LB->next;
pa
LA
2
2
3
LB
1
3
3
∧
4
∧
pb
LC=LA;
LC->next=NULL;
LC
r=LC;
LA
pa
∧
r
2
2
3
∧
School of Information and Electrical Engineering
while(pa!=NULL&&pb!=NULL)
{
if(pa->data<=pb->data)//2 is greater than 1,so false
else
{
r->next=pb;
LB
1
LC
LA
r
pb
3
3
4
∧
School of Information and Electrical Engineering
r=pb;
pb=pb->next;
LB
1
3
pb
LC
r
LA
}
}
while(pa!=NULL&&pb!=NULL)//true
{
3
4
∧
School of Information and Electrical Engineering
if(pa->data<=pb->data)//2 is less than 3,so true
{
r->next=pa;
pa
LB
1
LC
LA
2
r
2
3
∧
School of Information and Electrical Engineering
r=pa;
pa=pa->next;
LB
1
LC
2
2
r
LA
}
}
pa
3
∧
School of Information and Electrical Engineering
//while(pa!=NULL&&pb!=NULL),finished
LB
1
2
LC
2
pa
LA
pb
3
∧
r
∧
3
3
4
∧
School of Information and Electrical Engineering
if(pa) //false
else
r->next=pb;
LB
1
LC
2
3
2
r
pa
∧
LA
3
free(LB);
return(LC);
}
pb
3
4
∧
© Copyright 2026 Paperzz