1.单向链表代码
注意创建的中间节点pNew必须要每次循环创建一遍,因为那是一个指针,只创建一遍而重复赋值的话,会导致链表里面的节点也会改变
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
#define true 1
#define false 0
typedef int status;
typedef int ElemType;
typedef struct Node{
ElemType elem;
struct Node *next;
}Node,*pNode;
//基本操作
//创建一个长度为n的链表
pNode CreateList(int n){
ElemType elem;
pNode pHead,pTail;
pHead = (pNode)malloc(sizeof(Node));
pTail = pHead;
pTail->next=NULL;
for (int i=0; i<n; ++i) {
pNode pNew = (pNode)malloc(sizeof(Node));
scanf("%d", &elem);
pNew->elem=elem;
pNew->next=NULL;
pTail->next=pNew;
pTail=pNew;
}
return pHead;
}
//在第n个位置之后加入elem节点
ElemType InsertNode(pNode p,ElemType elem,int n){
pNode temp = p->next;
pNode pNew = (pNode)malloc(sizeof(Node));
pNew->elem=elem;
//第一个位置,头插
if (n==0) {
pNew->next=temp;
p->next=pNew;
return elem;
}
//把temp指向要插入的位置
for(int i=1;i<=n-1;i++){
temp=temp->next;
//如果在插入位置在最后,不存在节点,尾插
if(temp->next==NULL){
pNew->next=NULL;
temp->next=pNew;
return elem;
}
}
//在某节点位置插入
pNew->next=temp->next;
temp->next=pNew;
return elem;
}
//删除元素值为elem的节点
ElemType DeleteNode(pNode p,ElemType elem){
pNode temp=p->next;
pNode pNew;
if (temp->elem==elem) {
p->next=temp->next;
free(temp);
return elem;
}
while (temp!=NULL) {
if(temp->next->elem==elem){
pNew = temp->next;
temp->next=pNew->next;
free(pNew);
return elem;
}
temp=temp->next;
}
printf("删除失败\n");
return false;
}
//遍历
void TraverseList(pNode p){
pNode temp = p->next;
while (temp!=NULL) {
printf("%d ",temp->elem);
temp=temp->next;
}
printf("\n");
}
int main(){
pNode pHead=CreateList(3);
TraverseList(pHead);
InsertNode(pHead, 10, 0);
TraverseList(pHead);
InsertNode(pHead, 1000, 4);
TraverseList(pHead);
InsertNode(pHead, 10000, 6);
TraverseList(pHead);
DeleteNode(pHead, 10000);
TraverseList(pHead);
}
2.实例:
3.应用:多项式相加
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
//系数
int coef;
//指数
int index;
struct Node *next;
}Node,*pNode;
pNode CreateList(int len){
pNode head,tail,new;
head = (pNode)malloc(sizeof(Node));
tail=head;
tail->next=NULL;
int coef,index;
for (int i =0; i<len; i++) {
new = (pNode)malloc(sizeof(Node));
printf("input coef:");
scanf("%d",&coef);
printf("input index:");
scanf("%d",&index);
new->coef=coef;new->index=index;
new->next=NULL;
tail->next=new;
tail=new;
}
return head;
}
pNode MergeList(pNode L1,pNode L2){
pNode head,tail,new;
pNode temp1,temp2;
head=(pNode)malloc(sizeof(Node));
head->next=NULL;
tail=head;
temp1=L1->next;
temp2=L2->next;
while (temp1!=NULL && temp2!=NULL) {
new=(pNode)malloc(sizeof(Node));
if(temp1->index== temp2->index){
new->index=temp1->index;
new->coef=temp1->coef+temp2->coef;
temp1=temp1->next;
temp2=temp2->next;
new->next=NULL;
tail->next=new;
tail=new;
continue;
}
if(temp1->index>temp2->index){
new->index=temp1->index;
new->coef=temp1->coef;
temp1=temp1->next;
}else{
new->index=temp2->index;
new->coef=temp2->coef;
temp2=temp2->next;
}
new->next=NULL;
tail->next=new;
tail=new;
}
while (temp1!=NULL) {
new=(pNode)malloc(sizeof(Node));
new->index=temp1->index;
new->coef=temp1->coef;
new->next=NULL;
tail->next=new;
tail=new;
temp1=temp1->next;
}
while (temp2!=NULL) {
new=(pNode)malloc(sizeof(Node));
new->index=temp1->index;
new->coef=temp1->coef;
new->next=NULL;
tail->next=new;
tail=new;
temp2=temp2->next;
}
return head;
}
void TraverseList(pNode L){
pNode p=L->next;
while(p!=NULL){
printf("%d--%d ",p->coef,p->index);
p=p->next;
}
printf("\n");
}
int main(){
int len1,len2;
pNode L1,L2,L3;
printf("input L1 len:");
scanf("%d",&len1);
L1=CreateList(len1);
printf("input L2 len:");
scanf("%d",&len2);
L2=CreateList(len2);
TraverseList(L1);
TraverseList(L2);
L3=MergeList(L1, L2);
TraverseList(L3);
}