6-5 linked list operation set (20 points)
This problem requires to realize the operation set of linked list.
Function interface definition:
Position Find( List L, ElementType X ); List Insert( List L, ElementType X, Position P ); List Delete( List L, Position P );
The List structure is defined as follows:
typedef struct LNode *PtrToLNode; struct LNode { ElementType Data; PtrToLNode Next; }; typedef PtrToLNode Position; typedef PtrToLNode List;
Each operation function is defined as:
Position find (List L, ElementType x): returns the first occurrence of X in a linear table. If not found, ERROR will be returned;
List insert (List L, ElementType X, position P): insert X before the node pointed to by position P, and return the header of the linked list. If the parameter p points to an illegal position, print "Wrong Position for Insertion" and return ERROR;
List delete (List L, position P): delete the element of position P and return the header of the linked list. If parameter p points to an illegal location, print "Wrong Position for Deletion" and return ERROR.
Sample referee test procedure:
#include <stdio.h> #include <stdlib.h> #define ERROR NULL typedef int ElementType; typedef struct LNode *PtrToLNode; struct LNode { ElementType Data; PtrToLNode Next; }; typedef PtrToLNode Position; typedef PtrToLNode List; Position Find( List L, ElementType X ); List Insert( List L, ElementType X, Position P ); List Delete( List L, Position P ); int main() { List L; ElementType X; Position P, tmp; int N; L = NULL; scanf("%d", &N); while ( N-- ) { scanf("%d", &X); L = Insert(L, X, L); if ( L==ERROR ) printf("Wrong Answer\n"); } scanf("%d", &N); while ( N-- ) { scanf("%d", &X); P = Find(L, X); if ( P == ERROR ) printf("Finding Error: %d is not in.\n", X); else { L = Delete(L, P); printf("%d is found and deleted.\n", X); if ( L==ERROR ) printf("Wrong Answer or Empty List.\n"); } } L = Insert(L, X, NULL); if ( L==ERROR ) printf("Wrong Answer\n"); else printf("%d is inserted as the last element.\n", X); P = (Position)malloc(sizeof(struct LNode)); tmp = Insert(L, X, P); if ( tmp!=ERROR ) printf("Wrong Answer\n"); tmp = Delete(L, P); if ( tmp!=ERROR ) printf("Wrong Answer\n"); for ( P=L; P; P = P->Next ) printf("%d ", P->Data); return 0; } /* Your code will be embedded here */
Input example:
6 12 2 4 87 10 2 4 2 12 87 5
Output example:
2 is found and deleted. 12 is found and deleted. 87 is found and deleted. Finding Error: 5 is not in. 5 is inserted as the last element. Wrong Position for Insertion Wrong Position for Deletion 10 4 2 5
ANS:
Position Find( List L, ElementType X ) { List l=L; for(; l!=NULL; l=l->Next) { if(l->Data==X) return l; } return ERROR; } List Insert( List L, ElementType X, Position P ) { if(L==NULL) { L=(List) malloc(sizeof(struct LNode)); L->Data=X; L->Next=NULL; return L; } /* if(P==NULL) { printf("Wrong Position for Insertion"); return ERROR; }*/ List t=(List) malloc(sizeof(struct LNode)),y=L; t->Data=X; if(L==P) { t->Next=L; return t; } while(y->Next!=P) { y=y->Next; if(y==NULL) { printf("Wrong Position for Insertion\n"); return ERROR; } } y->Next=t; t->Next=P; return L; } List Delete( List L, Position P ) { List t=L; if(t==P) { L=L->Next; return L; } while(t->Next!=P) { t=t->Next; if(t==NULL) { printf("Wrong Position for Deletion\n"); return ERROR; } } t->Next=P->Next; free(P); return L; }