/************************************************************************/
/* name:insertNode.c */
/* func:向链表中插入结点 */
/* time:2013-04-01 */
/************************************************************************/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#define NODESIZE 5
typedef struct test{
char cword;
struct test *next;
}LNode,*Linklist;
//创建链表
Linklist creat(char *str,int n)
{
LNode *p1,*p2;
LNode *head;
int i;
head = NULL;
for(i=0; i<n; i++)
{
p1=(LNode*)malloc(sizeof(LNode));
p1->cword = str[i];
if(head == NULL)
{
head=p1;
}else{
p2->next=p1;
}
p2=p1;
}
p2->next=NULL;//最后结点指针域为空
return head;//返回头指针
}
//插入结点
//想象成小朋友手拉手,要加入一位新的小朋友,需要在插入位置使两学生手脱开
//前面同学拉新同学手,新同学拉后面同学手,这样就完成了插入,成为新的队列
Linklist insert(Linklist head,char c,int n)
{
Linklist p1=head,s;
int j=0,i=n;//初始化
while (p1 && j<i-1)
{
p1=p1->next;
j++;
}
if(!p1 || j>i-1)
exit(0);
s=(LNode*)malloc(sizeof(LNode));
s->cword=c;
s->next=p1->next;
p1->next=s;
return head;
}
int main()
{
Linklist head,p;
int i;
char str[NODESIZE];
int len=NODESIZE;
printf("输入%d个链表结点(字符串形式输入):",NODESIZE);
for (i=0; i<len; i++)
{
scanf("%c",&str[i]);
}
head=creat(str,len);
if(head == NULL)//创建链表失败
return -1;
/插入前
printf("插入前\n");
p = head;
while (p!=NULL)
{
printf("%c ",p->cword);
p = p->next;
}
printf("\n");
//
char c;//需要插入的字符
printf("输入要插入结点的位置:");
scanf("%d",&i);
getchar();//获取回车字符
printf("输入要插入的结点内容:");
scanf("%c",&c);
head=insert(head,c,i);
//插入后//
printf("插入后\n");
p = head;
while (p!=NULL)
{
printf("%c ",p->cword);
p = p->next;
}
printf("\n");
//
return 0;
}
/************************************************************************/
/* name:deleteNode.c */
/* func:删除链表中的结点 */
/* time:2013-04-02 */
/************************************************************************/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#define NODESIZE 5
typedef struct test{
char cword;
struct test *next;
}LNode,*Linklist;
//创建链表
Linklist creat(char *str,int n)
{
LNode *p1,*p2;
LNode *head;
int i;
head = NULL;
for(i=0; i<n; i++)
{
p1=(LNode*)malloc(sizeof(LNode));
p1->cword = str[i];
if(head == NULL)
{
head=p1;
}else{
p2->next=p1;
}
p2=p1;
}
p2->next=NULL;//最后结点指针域为空
return head;//返回头指针
}
//删除结点,记得释放删除的结点空间
Linklist deleteNode(Linklist head,int n)
{
Linklist p=head,q=head;
int i=n;
int j=0;
while(p && j<i-1)
{
p=p->next;
j++;
}
while(j &&(--j))//使指针q指向p所指向的结点
{
q=q->next;
}
if(!p || j>i-1)
exit(0);
if(p->next==NULL)//删除元素为链表末尾元素
{
q->next=NULL;
free(p);
return head;
}
if(i==1)//删除元素为链表首部节点(无头结点链表)
{
head=head->next;
free(p);
return head;
}
q->next=p->next;
free(p);
return head;
}
int main()
{
Linklist head,p;
int i;
char str[NODESIZE];
int len=NODESIZE;
printf("输入%d个链表结点(字符串形式输入):",NODESIZE);
for (i=0; i<len; i++)
{
scanf("%c",&str[i]);
}
head=creat(str,len);
if(head == NULL)//创建链表失败
return -1;
/删除前
printf("删除前\n");
p = head;
while (p!=NULL)
{
printf("%c ",p->cword);
p = p->next;
}
printf("\n");
//删除后//
int pos;//要删除元素位置
printf("输入要删除元素的位置:");
scanf("%d",&pos);
p=deleteNode(head,pos);
printf("删除后\n");
while (p!=NULL)
{
printf("%c ",p->cword);
p = p->next;
}
printf("\n");
return 0;
}
/************************************************************************/
/* name:updateNode.c */
/* func:修改链表结点内容 */
/* time:2013-04-02 */
/************************************************************************/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#define NODESIZE 5
typedef struct test{
char cword;
struct test *next;
}LNode,*Linklist;
//创建链表
Linklist creat(char *str,int n)
{
LNode *p1,*p2;
LNode *head;
int i;
head = NULL;
for(i=0; i<n; i++)
{
p1=(LNode*)malloc(sizeof(LNode));
p1->cword = str[i];
if(head == NULL)
{
head=p1;
}else{
p2->next=p1;
}
p2=p1;
}
p2->next=NULL;//最后结点指针域为空
return head;//返回头指针
}
//修改链表结点内容
Linklist updateNode(Linklist head,char c,int n)
{
Linklist p=head,q=head;
int i=n;
int j=0;
while(p && j<i-1)
{
p=p->next;
j++;
}
if(!p || j>i-1)
exit(0);
p->cword=c;//修改元素的值
return head;
}
int main()
{
Linklist head,p;
int i;
char str[NODESIZE];
int len=NODESIZE;
printf("输入%d个链表结点(字符串形式输入):",NODESIZE);
for (i=0; i<len; i++)
{
scanf("%c",&str[i]);
}
head=creat(str,len);
if(head == NULL)//创建链表失败
return -1;
/修改前
printf("修改前\n");
p = head;
while (p!=NULL)
{
printf("%c ",p->cword);
p = p->next;
}
printf("\n");
//修改后//
int pos;//要修改元素位置
char newc;//修改元素的值
printf("输入要修改元素的位置:");
scanf("%d",&pos);
getchar();//获取回车字符
printf("输入要修改元素的值:");
scanf("%c",&newc);
p=updateNode(head,newc,pos);
printf("修改后\n");
while (p!=NULL)
{
printf("%c ",p->cword);
p = p->next;
}
printf("\n");
return 0;
}
/************************************************************************/
/* name:searchNode.c */
/* func:查找链表中的结点 */
/* time:2013-04-01 */
/************************************************************************/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#define NODESIZE 5
typedef struct test{
char cword;
struct test *next;
}LNode,*Linklist;
//创建链表
Linklist creat(char *str,int n)
{
LNode *p1,*p2;
LNode *head;
int i;
head = NULL;
for(i=0; i<n; i++)
{
p1=(LNode*)malloc(sizeof(LNode));
p1->cword = str[i];
if(head == NULL)
{
head=p1;
}else{
p2->next=p1;
}
p2=p1;
}
p2->next=NULL;//最后结点指针域为空
return head;//返回头指针
}
//查找结点
Linklist search(Linklist head,char &c,int n)
{
Linklist p1=head;
int j=0,i=n;//初始化
while (p1 && j<i-1)
{
p1=p1->next;
j++;
}
if(!p1 || j>i-1)
exit(0);
c = p1->cword;
return head;
}
int main()
{
Linklist head,p;
int i;
char str[NODESIZE];
int len=NODESIZE;
printf("输入%d个链表结点(字符串形式输入):",NODESIZE);
for (i=0; i<len; i++)
{
scanf("%c",&str[i]);
}
head=creat(str,len);
if(head == NULL)//创建链表失败
return -1;
/查找前
printf("查找前\n");
p = head;
while (p!=NULL)
{
printf("%c ",p->cword);
p = p->next;
}
printf("\n");
//
char c;
printf("输入要查找结点的位置:");
scanf("%d",&i);
head=search(head,c,i);
printf("查找位置的元素为:%c\n",c);
return 0;
}