#include
"stdio.h"
struct
Node
{
Node
*pNext
int
value
}*pTop
struct
Node*
Insert(struct
Node
*pNode,int
Num)
void
Del(struct
Node
*pDelNode)
struct
Node*
Search(struct
Node
*pNode,int
Num)
void
main()
{
pTop=NULL
int
i,k,x,y
struct
Node
*pCurrentNode,*pTempNode
/*(1)建立带表头结点的单链表;*/
for(i=0i<30i++)
Insert(NULL,i)/*建立一个有30个结点的链表*/
/*(2)输出单链表中所有结点的数据域值;*/
pCurrentNode=pTop
while(pCurrentNode!=NULL)
{
printf("%d->",pCurrentNode->value)/*遍历这个链表并输出其各结点的数据域*/
pCurrentNode=pCurrentNode->pNext
}
/*(3)输入x,y在第一个数据域值为x的结点之后插入结点y,若无结点x,则在表尾插入结点y*/
printf("Input
x,y")
scanf("%d,%d",&x,&y)
pCurrentNode=Search(NULL,x)
Insert(pCurrentNode,y)
/*(4)输入k,删除单链表中所有的结点k,并输出被删除结点的个数。
*/
printf("Input
k")
scanf("%d",&k)
pCurrentNode=pTop
i=0
while(1)
{
pTempNode=Search(pCurrentNode,x)
if(pTempNode!=NULL)
{
pCurrentNode=pTempNode->pNext
Del(pTempNode)
i++
}
else
break
}
printf("%d
Nodes
was
deleted",i)
pTempNode=pTop
while(pTop!=NULL)
{
pTop=pTempNode->pNext
delete
pTempNode
}
}
Node*
Insert(struct
Node
*pNode,int
Num)
{
struct
Node
*pNewNode
pNewNode=new
Node
pNewNode->value=Num
if(pNode==NULL)/*无确定插入位置时将结点放在链表最后*/
{
if(pTop!=NULL)/*确定链表是否是空表*/
{
pNode=pTop
while(pNode->pNext!=NULL)
pNode=pNode->pNext/*找到尾结点*/
pNode->pNext=pNewNode
}
else
{
pTop=pNewNode
}
pNewNode->pNext=NULL
}
else/*有确定插入位置时将结点放在指定结点之后*/
{
pNewNode->pNext=pNode->pNext
pNode->pNext=pNewNode
}
return
pNewNode
}
void
Del(struct
Node
*pDelNode)
{
if(pDelNode==NULL
||
pTop==NULL)
return/*防错处理*/
struct
Node
*pNode
pNode=pTop
while(pNode!=NULL
&&
pNode->pNext!=pDelNode)
pNode=pNode->pNext/*找到指定结点的前导结点*/
if(pNode!=NULL)
{
pNode->pNext=pDelNode->pNext
delete
pDelNode
}
}
struct
Node*
Search(struct
Node
*pNode,int
Num)
{
struct
Node
*pSeaNode
if(pNode==NULL)
pSeaNode=pTop/*不指定搜索的起始位置,从表头开始*/
else
pSeaNode=pNode/*指定了搜索的起始位置,从指定位置开始*/
while(pSeaNode!=NULL
&&
pSeaNode->value!=Num)
pSeaNode=pSeaNode->pNext
return
pSeaNode/*没有找到结点时返回空指针*/
}
下面的代码是我刚帮别人修改了的一个程序。实现的功能是:首先输入数据建立链表,然后删除链表中数据相同的结点,最后输出剩余链表中的数据。
之前出错的地方我标注出来的。
链表的主要 *** 作程序里面都有。
#include
<stdio.h>
#include
<stdlib.h>
#include
<malloc.h>
typedef
struct
_NODE
{
char
data
_NODE
*next
}NODE
NODE*
s =
NULL
NODE*
p =
NULL
NODE*
q =
NULL
NODE*
k =
NULL
NODE*
m =
NULL
NODE*
head =
NULL
void
main()
{
char
x
int
i
head
=
(NODE*)malloc(sizeof(NODE))
head->next=NULL
p
=
head
//
向链表中添加数据
for(i=1i<=7i++)
{
scanf("%c
",&x)
q
=
(NODE*)malloc(sizeof(NODE))
q->data
=
x
p->next
=
q
p
=
q
}
p->next=NULL
q
=
head->next
m
=
q //
q和m指向链表中第一个结点
p
=
q->next //
p指向链表中第二个结点
while(m->next
!=
NULL) //
!!!!此处应该是m->next
!=
NULL
{
while(p
!=
NULL)
{
if(p->data
==
m->data)
{
q->next
=
p->next
k
=
p
p
=
p->next
free(k) //
超找到相同的,删除该结点
}
else
{
q
=
p
p
=
p->next
}
}
m
=
m->next
q
=
m
p
=
q->next //
!!!!只有while判断条件为m->next
!=
NULL时才安全!!!
}
k
=
head->next
//
打印输出剩下的所有元素
while(k!=NULL)
{
printf("%c",k->data)
k
=
k->next
}
printf("\n")
}
#include <stdlib.h>#include <memory.h>
#include <stdio.h>
#include <string.h>
struct node /*节点的数据结构*/
{
int num
char str[20]
struct node *next
}
struct node *creat(struct node *head)
struct node *insert(struct node *head, char *pstr, int n)
struct node *delet(struct node *head, char *pstr)
void print(struct node *head)
/* * * * * * * * * * * * * * * * * * * * * * * * * * * */
main()
{
/*函数声明*/
struct node *head
char str[20]
intn
head = NULL /*做空表*/
head=creat(head) /*调用函数创建以head 为头的链表*/
print(head) /*调用函数输出节点*/
printf("\n input inserted num,name:\n")
gets(str)/*输入学号*/
n = atoi(str)
gets(str)/*输入姓名*/
head = insert(head, str, n) /*将节点插入链表*/
print(head) /*调用函数输出节点*/
printf("\n input deleted name:\n")
gets(str)/*输入被删姓名*/
head=delet(head,str) /*调用函数删除节点*/
print(head) /*调用函数输出节点*/
return
}
/* * * * * * * * * * * * * * * * * * * * * */
/* * * 创建链表* * * * * * * * * * * */
struct node *creat(struct node *head)
{
char temp[30]
struct node *p1,*p2
p1 = p2 = (struct node*) malloc(sizeof(struct node))
printf ("input num, name: \n")
printf("exit:double times Enter!\n")
gets(temp)
gets(p1->str)
p1->num = atoi(temp)
p1->next = NULL
while (strlen(p1->str)>0)
{
if (head==NULL)head=p1
else p2->next = p1
p2 = p1
p1 = (struct node *)malloc(sizeof(struct node))
printf ("input num, name: \n")
printf("exit:double times Enter!\n")
gets(temp)
gets(p1->str)
p1->num=atoi(temp)
p1->next=NULL
}
return head
}
/* * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * 插入节点* * * * * * * * * */
struct node *insert(struct node *head, char *pstr, int n)
{
struct node *p1, *p2, *p3
p1=(struct node*)malloc(sizeof(struct node))
strcpy(p1->str, pstr)
p1->num = n
p2 = head
if(head == NULL)
{
head = p1
p1->next = NULL
}
else
{
while ((n >p2->num) &&(p2->next != NULL))
{
p3 = p2
p2 = p2->next
}
if (n <= p2->num)
if (head==p2)
{
head = p1
p1->next = p2
}
else
{
p3->next = p1
p1->next = p2
}
else
{
p2->next = p1
p1->next = NULL
}
}
return(head)
}
/* * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * 删除节点* * * * * * * * * * * * */
struct node *delet(struct node *head, char *pstr)
{
struct node *temp, *p
temp = head
if(head == NULL)
printf("\nList is null!\n")
else
{
temp = head
while((strcmp(temp->str, pstr) != 0) &&(temp->next != NULL))
{
p = temp
temp = temp->next
}
if(strcmp(temp->str, pstr) == 0)
{
if(temp == head)
{
head = head->next
free(temp)
}
else
{
p->next = temp->next
printf("delete string : %s\n", temp->str)
free(temp)
}
}
else printf("\nno find string!\n")
}
return(head)
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * 链表各节点的输出* * * * * * * * * */
void print (struct node *head)
{
struct node *temp
temp = head
printf("\n output strings:\n")
while(temp!=NULL)
{
printf("\n%d----%s\n", temp->num, temp->str)
temp = temp->next
}
return
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)