#include "stdlib.h"
#include "stdio.h"
#define OVERFLOW -2
int i,j;
typedef int ElemType;
typedef struct Lnode{ ElemType elem;
struct Lnode *next;
int length;}Lnode,*LinkList;
LinkList p,q,pa,pb,pc,s,Lb,La,Lc;
ElemType temp;
void CreatList (LinkList L,int n)//生成一个带头节点的链表
{ i=0;
L=(LinkList)malloc(sizeof(Lnode));
if(!L) exit (OVERFLOW);
L->next=NULL;
while(i<n){ p=(LinkList)malloc(sizeof(Lnode));
if(!L) exit (OVERFLOW);
scanf("%c",&(*p).elem);
p->next=L->next;L->next=p;i++;
L->length=n;}
}
void Insert (LinkList L,int i,ElemType e)//在第i个节点前插入元素e
{ p=L; j=0;
while(j<i)
{p=p->next;j++;}
s=(LinkList)malloc(sizeof(Lnode));
if(!L) exit (OVERFLOW);
s->elem=e;s->next=p->next;p->next=s;}
void Delete(LinkList L,int i)//删除第i个节点
{ p=L; j=0;
while(j<i){ p=p->next; j++}
q=p->next;p->next=q->next;free(q);}
void AscendList(LinkList L)//将链表按递增序列排列
{for(i=(*L).length;i>0;i--)for(j=1;j<(*L).length;j++){p=L;p=p->next;
if( p->elem>=p->next->elem)
{ temp= p->next->elem; p->next->elem=p->elem; p->elem=temp;}
}
}
void MergeList(LinkList La,Lb,Lc)//合并La和Lb为Lc
{ Lc=(LinkList)malloc(sizeof(Lnode));
if(!Lc) exit (OVERFLOW);
pa=La;pb=Lb;pc=Lc;Lc->length=0;
pa=pa->next; pb=pb->next;
while(pa&&pb)
{if((*pa).elem<=(*pb).elem) { pc->next=pa;pa=pa->next;(*Lc).length++;}
else {pc->next=pb;pb=pb->next;(*Lc).length++;}
while(pa){ pc->next=pa;pa=pa->next;(*Lc).length++;}
while(pb){ pc->next=pb;pb=pb->next;(*Lc).length++;}
}
int main()
{ CreatList(La,7);
CreatList(Lb,6);
Insert (La,4,6);
Delete(Lb,3);
MergeList(La,Lb,Lc);
for(i=0;i<Lc->length;i++) {p=Lc;p=p->next;printf("%c",p->elem);}
return 0; }
----------------解决方案--------------------------------------------------------
这个交给 论坛 我忙点别的去
----------------解决方案--------------------------------------------------------
楼主大哥你先告诉我你这是什么啊
----------------解决方案--------------------------------------------------------
创建和删除
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *nextPtr;
}*LinkList, Lnode;
static void CreateList(LinkList *headPtr, int item);
static void DeleteElem(LinkList *headPtr, int item);
static void Traverse(LinkList headPtr);
int main(void)
{
LinkList headPtr = NULL;
int item, choice;
printf("Enter choice (0 to quit): ");
scanf("%d", &choice);
while (choice != 0)
{
switch (choice)
{
case 1:
printf("Enter create item: ");
scanf("%d", &item);
CreateList(&headPtr, item); /* creata function */
Traverse(headPtr);
break;
case 2:
printf("Enter delete item: ");
scanf("%d", &item);
DeleteElem(&headPtr, item); /* delete element function */
Traverse(headPtr);
break;
default:
printf("invalid enter!\n");
break;
}
printf("Enter choice (0 to quit): ");
scanf("%d", &choice);
}
getch();
return 0;
}
static void CreateList(LinkList *headPtr, int item)
{
LinkList newPtr, previousPtr, currentPtr;
if ((newPtr = (LinkList)malloc(sizeof(Lnode))) == NULL)
{
printf("memory malloc failure.\n");
exit(1);
}
newPtr -> data = item;
newPtr -> nextPtr = NULL;
previousPtr = NULL;
currentPtr = *headPtr;
while (currentPtr != NULL) /* search insert node position */
{
previousPtr = currentPtr;
currentPtr = currentPtr -> nextPtr;
}
if (previousPtr == NULL)
{
newPtr -> nextPtr = *headPtr;
*headPtr = newPtr;
}
else
{
previousPtr -> nextPtr = newPtr;
newPtr -> nextPtr = currentPtr;
}
}
static void DeleteElem(LinkList *headPtr, int item)
{
LinkList previousPtr, currentPtr, tempPtr;
if (headPtr != NULL)
{
previousPtr = NULL;
currentPtr = *headPtr;
if ((*headPtr) -> data == item)
{
(*headPtr) = (*headPtr) -> nextPtr;
}
else
{ /* search delete node position */
while (currentPtr != NULL && currentPtr -> data != item)
{
previousPtr = currentPtr;
currentPtr = currentPtr -> nextPtr;
}
if (currentPtr != NULL)
{
tempPtr = currentPtr;
previousPtr -> nextPtr = currentPtr -> nextPtr;
free(tempPtr);
}
else
{
printf("No found delete node.\n");
}
}
}
else
{
printf("This is list is empty.\n");
}
}
static void Traverse(LinkList headPtr)
{
while (headPtr != NULL)
{
printf("%d -> ", headPtr -> data);
headPtr = headPtr -> nextPtr;
}
printf("NULL\n");
}
----------------解决方案--------------------------------------------------------
单连表合并
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *nextPtr;
}*LinkList, Lnode;
static void CreateList(LinkList *L, int item);
static void MergeList(LinkList headPtrA, LinkList headPtrB, LinkList *headPtrC);
static void Traverse(LinkList L);
int main(void)
{
LinkList headPtrA = NULL, headPtrB = NULL, headPtrC = NULL;
int choice, item;
printf("Enter integral 1.insertA 2.insertB 3.merge (0 quit): ");
scanf("%d", &choice);
while (choice != 0)
{
switch (choice)
{
case 1:
printf("Enter insertA item: ");
scanf("%d", &item);
CreateList(&headPtrA, item);
Traverse(headPtrA);
break;
case 2:
printf("Enter insertB item: ");
scanf("%d", &item);
CreateList(&headPtrB, item);
Traverse(headPtrB);
break;
case 3: /* attention pass headPtrC address */
MergeList(headPtrA, headPtrB, &headPtrC);
Traverse(headPtrC);
break;
default:
printf("Invalid enter!\n");
break;
}
printf("Enter choice 1.insertA 2.insertB 3.merge (0 quit): ");
scanf("%d", &choice);
}
getch();
return 0;
}
void CreateList(LinkList *L, int item) /* create list function */
{
LinkList newPtr, previousPtr, currentPtr;
if ((newPtr = (LinkList)malloc(sizeof(Lnode))) == NULL)
{
printf("memory malloc failure.\n");
exit(1);
}
newPtr -> data = item;
newPtr -> nextPtr = NULL;
previousPtr = NULL;
currentPtr = *L;
while (currentPtr != NULL && currentPtr -> data < newPtr -> data)
{
previousPtr = currentPtr;
currentPtr = currentPtr -> nextPtr;
}
if (previousPtr == NULL)
{
newPtr -> nextPtr = *L;
*L = newPtr;
}
else
{
previousPtr -> nextPtr = newPtr;
newPtr -> nextPtr = currentPtr;
}
}
void MergeList(LinkList headPtrA, LinkList headPtrB, LinkList *headPtrC)
{
while (headPtrA != NULL && headPtrB != NULL)
{
if (headPtrA -> data < headPtrB -> data)
{
CreateList(&(*headPtrC), headPtrA -> data);
headPtrA = headPtrA -> nextPtr;
}
else
{
CreateList(&(*headPtrC), headPtrB -> data);
headPtrB = headPtrB -> nextPtr;
}
}
while (headPtrA != NULL)
{
CreateList(&(*headPtrC), headPtrA -> data);
headPtrA = headPtrA -> nextPtr;
}
while (headPtrB != NULL)
{
CreateList(&(*headPtrC), headPtrB -> data);
headPtrB = headPtrB -> nextPtr;
}
}
static void Traverse(LinkList L)
{
while (L != NULL)
{
printf("%d -> ", L -> data);
L = L -> nextPtr;
}
printf("NULL\n\n");
}
----------------解决方案--------------------------------------------------------
单连表逆行
#include "stdio.h"
#include "stdlib.h"
typedef struct node
{
int data;
struct node *nextPtr;
}*LinkList, Lnode;
static void CreateList(LinkList *headPtr, LinkList *tailPtr, int item);
static void Reverse(LinkList *headPtr);
static void Traverse(LinkList headPtr);
int main(void)
{
LinkList headPtr = NULL, tailPtr = NULL;
int item;
printf("Enter item (0 quit): ");
scanf("%d", &item);
while (item != 0)
{
CreateList(&headPtr, &tailPtr, item);
printf("Enter item (0 quit): ");
scanf("%d", &item);
}
Traverse(headPtr);
if (headPtr != NULL)
{
Reverse(&headPtr);
Traverse(headPtr);
}
else
{
printf("list is empty.\n");
}
getch();
return 0;
}
static void CreateList(LinkList *headPtr, LinkList *tailPtr, int item)
{
LinkList newPtr;
if ((newPtr = (LinkList)malloc(sizeof(Lnode))) == NULL)
exit(-1);
newPtr -> data = item;
newPtr -> nextPtr = NULL;
if (*headPtr == NULL)
{
newPtr -> nextPtr = *headPtr;
*headPtr = newPtr;
}
else
(*tailPtr) -> nextPtr = newPtr;
*tailPtr = newPtr;
}
static void Reverse(LinkList *headPtr)
{
LinkList previousPtr, currentPtr, tempPtr;
previousPtr = NULL;
currentPtr = *headPtr;
while (currentPtr != NULL)
{
tempPtr = currentPtr -> nextPtr;
currentPtr -> nextPtr = previousPtr;
previousPtr = currentPtr;
currentPtr = tempPtr;
}
*headPtr=previousPtr;
}
static void Traverse(LinkList headPtr)
{
while (headPtr != NULL)
{
printf("%d -> ", headPtr -> data);
headPtr = headPtr -> nextPtr;
}
printf("NULL\n");
}
----------------解决方案--------------------------------------------------------
排序
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *nextPtr;
}*LinkList, Lnode;
static void CreateList(LinkList *headPtr, LinkList *tailPtr, int item);
static void ExchangeSort(LinkList *headPtr);
static void Traverse(LinkList headPtr);
int main(void)
{
LinkList headPtr = NULL, tailPtr = NULL;
int item;
printf("Enter item (0 quit): ");
scanf("%d", &item);
while (item != 0)
{
CreateList(&headPtr, &tailPtr, item);
printf("Enter item (0 quit): ");
scanf("%d", &item);
}
Traverse(headPtr);
if (headPtr != NULL)
{
ExchangeSort(&headPtr);
Traverse(headPtr);
}
else
{
printf("list is empty.\n");
}
getch();
return 0;
}
static void CreateList(LinkList *headPtr, LinkList *tailPtr, int item)
{
LinkList newPtr;
if ((newPtr = (LinkList)malloc(sizeof(Lnode))) == NULL)
{
printf("memory malloc failure.\n");
exit(1);
}
newPtr -> data = item;
newPtr -> nextPtr = NULL;
if (*headPtr == NULL)
{
newPtr -> nextPtr = *headPtr;
*headPtr = newPtr;
}
else
{
(*tailPtr) -> nextPtr = newPtr;
}
*tailPtr = newPtr;
}
static void ExchangeSort(LinkList *headPtr)
{
LinkList currentPtrA, currentPtrB;
int temp;
for (currentPtrA = *headPtr; currentPtrA -> nextPtr != NULL; currentPtrA = currentPtrA -> nextPtr)
{
for (currentPtrB = currentPtrA -> nextPtr; currentPtrB != NULL; currentPtrB = currentPtrB -> nextPtr)
{
if (currentPtrA -> data > currentPtrB -> data)
{
temp = currentPtrA -> data;
currentPtrA -> data = currentPtrB -> data;
currentPtrB -> data = temp;
}
}
}
}
static void Traverse(LinkList headPtr)
{
while (headPtr != NULL)
{
printf("%d -> ", headPtr -> data);
headPtr = headPtr -> nextPtr;
}
printf("NULL\n\n");
}
----------------解决方案--------------------------------------------------------
大概写了下,请楼主仔细参照
[CODE]
#include "stdlib.h"
#include "stdio.h"
#define OVERFLOW -1 /*一般用-1*/
typedef int ElemType;
typedef struct Lnode{
ElemType elem;
struct Lnode *next; /*加个长度很少用没必要*/
} Lnode,*LinkList;
LinkList Creat_LinkList (LinkList L,int n)
{
int i=0;
LinkList newp;
L=(LinkList)malloc(sizeof(Lnode));
if(!L)
exit (OVERFLOW);
L->next=NULL;
while(i<n)
{
newp=(LinkList)malloc(sizeof(Lnode));
if(!L)
exit (OVERFLOW);
scanf("%d",&newp->elem); /*设置的是整型*/
newp->next=L->next;
L->next=newp;
i++;
}
return L;
}
LinkList Insert_LinkList (LinkList L,int i,ElemType e)
{
LinkList p=L,newp;
int j=0;
while(j<i-1) /*头结点不算第一个结点*/
{
p=p->next;
j++;
}
newp=(LinkList)malloc(sizeof(Lnode));
if(!L)
exit (OVERFLOW);
newp->elem=e;
newp->next=p->next;
p->next=newp;
return L;
}
LinkList Delete_LinkList(LinkList L,int i)
{
LinkList p=L,q;
int j=0;
while(j<i-1) /*同上*/
{
p=p->next;
j++;
}
q=p->next;
p->next=q->next;
free(q);
return L;
}
LinkList Merge_LinkList(LinkList La,LinkList Lb,LinkList Lc)
{
LinkList pa,pb,pc;
Lc=(LinkList)malloc(sizeof(Lnode));
if(!Lc)
exit (OVERFLOW);
pc=Lc;
pa=La->next;
pb=Lb->next;
while(pa&&pb)
if(pa->elem <= pb->elem)
{
pc->next=pa;
pc=pa; /*pc也要移动,下同*/
pa=pa->next;
}
else
{
pc->next=pb;
pc=pb;
pb=pb->next;
}
while(pa)
{
pc->next=pa;
pc=pa;
pa=pa->next;
}
while(pb)
{
pc->next=pb;
pc=pb;
pb=pb->next;
}
pc=NULL;
return Lc;
}
LinkList Ascend_LinkList(LinkList L)
{
LinkList p,q;
ElemType temp;
for(p=L->next;p->next!=NULL;p=p->next)
for(q=p->next;q!=NULL;q=q->next) /*你要的冒泡*/
if( p->elem >= q->elem)
{
temp = q->elem;
q->elem = p->elem;
p->elem = temp;
}
return L;
}
void Display_LinkList(LinkList L) /*输出*/
{
while(L=L->next)
printf("%d->",L->elem);
printf("NUll\n");
}
void Destory_LinkList(LinkList L) /*释放*/
{
LinkList p;
while(L)
{
p=L;
L=L->next;
free(p);
}
}
int main()
{
LinkList La,Lb,Lc;
int i;
La=Creat_LinkList(La,7);
La=Insert_LinkList (La,4,6);
La=Ascend_LinkList(La);
Lb=Creat_LinkList(Lb,6);
Lb=Delete_LinkList(Lb,3);
Lb=Ascend_LinkList(Lb);
Lc=Merge_LinkList(La,Lb,Lc);
Display_LinkList(Lc);
Destory_LinkList(Lc);
return 0;
}
[/CODE]
----------------解决方案--------------------------------------------------------